How to mount google drive to R notebook in colab?

筅森魡賤 提交于 2020-11-27 08:13:57

问题


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!


回答1:


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)



回答2:


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)



回答3:


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)


来源:https://stackoverflow.com/questions/56679549/how-to-mount-google-drive-to-r-notebook-in-colab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!