Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount google drive to R notebook in colab?

I have an R notebook in colab where I want to read a file which is saved in my google drive.

I only seem to find python code such as "from google.colab import drive drive.mount('/content/drive')" to mount the drive.

However, is there code for R to do this or another alternative? I am really struggling and would very much appreciate the help!

like image 288
Jaco Vos Avatar asked Jun 20 '19 05:06

Jaco Vos


People also ask

Can you use Google colab with R?

There are two ways to run R in Colab The first way is to use the rpy2 package in the Python runtime. This method allows you to execute R and Python syntax together. The second way is to actually start the notebook in the R runtime.

Can colab access Google Drive?

You can search Colab notebooks using Google Drive. Clicking on the Colab logo at the top left of the notebook view will show all notebooks in Drive. You can also search for notebooks that you have opened recently using File > Open notebook. Code is executed in a virtual machine private to your account.


2 Answers

To mount google drive in an R kernel:

install.packages("googledrive")
library("googledrive")

if (file.exists("/usr/local/lib/python3.6/dist-packages/google/colab_ipython.py")){
  install.packages("R.utils")
  library("R.utils")
  library("httr")
  my_check <- function() {return(TRUE)}
  reassignInPackage("is_interactive", pkgName = "httr", my_check)
  options(rlang_interactive=TRUE)
}                                                                                    

And authenticate google drive

drive_auth(use_oob = TRUE, cache = TRUE)
like image 190
Nosey Avatar answered Sep 28 '22 03:09

Nosey


Start using python:

from google.colab import drive
drive.mount('/content/drive')

Then load the R Magic:

%load_ext rpy2.ipython

and then activate the R Magic and load your data:

%%R
url = ('/content/drive/myDrive/folder1/myfile.csv')
dataset = read.csv(url)
like image 33
Thiago Ribas Avatar answered Sep 28 '22 05:09

Thiago Ribas