How to read data from google drive using R in colab?

一个人想着一个人 提交于 2020-06-26 04:12:26

问题


I have to read data in csv format from google drive using R in colab. I know how to do it using python, however I am not getting relevant ways to do it in R.


回答1:


You can use two R packages to accomplish this depending on how you want to open your google drive up to the world.

I use this at work to grab data from shared files coworkers want me to analyze. The most base way to use it

require(googlesheets4)
require(googledrive)
gs_file<- drive_get('name_of_sheet_on_google')
gs_data <- read_sheet(gs_file)

You will need to be signed into your google account when you do this, and a request for auth will come up when you run the code, you will agree to allow access and the data will magically appear in a dataframe.

I never run my code unattended, so I do not store my username, and passwords for security sake, but both packages have those capabilities and if you read the PDF's and vignettes on CRAN for the two, you should be able to build something secure.

It does not matter that you are working in an online Jupyter environment, R is R. These two packages and that code (with appropriate access, should get you headed in the right direction!




回答2:


If you can make your data public, you can use gdown.

system("gdown --id 12uRyLU-aAdInBtkVubhI4l3PmbYIo5aE")
data = read.csv("country_culture.csv")

Here's an example notebook.




回答3:


The way I found to make this work was to run my R code as part of a Python notebook. It is a bit of a hassle, but I try to write and test my code in RStudio first, and then I port it to Colab in order to share with my team. Here's how to do it.

  1. First, you need to load a colab notebook in Python, which shouldn't be hard, since it's the default setting. But anyways, you can do it at Runtime >> Change Runtime Type.

  2. Now, you need to import R into the workspace, by using a block:

%reload_ext rpy2.ipython
  1. Then, you need to mount your google drive through Python code. This snippet came from Google Colab's tutorial. Note that it is necessary to go through the authentication process, by which you click a link generated in the code and then navigate to your Drive account and then copy the passcode back into the interactive box.
from google.colab import drive
drive.mount('/content/malaria-drive')
  1. You should be all set to start working with R code, which you can with %R for inline responses or %%R for running the entire cell in R (my preference). Here's a snippet from a code I'm working on.
%%R
# Pkgs/opts ----
t_ini <- Sys.time()

install.packages("devtools")
devtools::install_github("lucasmation/microdadosBrasil")

xfun::pkg_attach2(
  "microdadosBrasil",
  "data.table",
  "questionr",
  "forcats",
  "stringr",
  "readr")

options(datatable.print.topn = 50,
        scipen = 999)
  1. When you're done with drive, the aforementioned tutorial recommends you flush and demount the drive (done in Python, so no %%R).
drive.flush_and_unmount()

Yeah, it's kind of annoying to have to type %%R into every code block, but it's the solution I've found so far. Hopefully Colab can provide a direct method from within R in the future.



来源:https://stackoverflow.com/questions/59746036/how-to-read-data-from-google-drive-using-r-in-colab

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