Jaco Vos
Jaco Vos

Reputation: 171

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!

Upvotes: 17

Views: 13043

Answers (4)

R Colab in 2022:

    install.packages("googledrive")
    library("googledrive")
    
    if (file.exists("/usr/local/lib/python3.7/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)

    }

Upvotes: 1

Nosey
Nosey

Reputation: 724

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)

Upvotes: 3

Thiago Ribas
Thiago Ribas

Reputation: 74

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)

Upvotes: 3

moch77
moch77

Reputation: 54

It seems there is no mechanism as of now to mount google drive in colab notebook with R kernel. Although a workaround can be used to have google drive mounted normally as in pyhton kernel and use both python and r based on the needs. See this answer which explains how r and python can be run together.

# activate R magic
%load_ext rpy2.ipython

%%R
x <- 42
print(x)

Upvotes: 1

Related Questions