Reputation: 127
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.
Upvotes: 7
Views: 13101
Reputation: 1
I just add my .sav file to the session memory (click on the left to folder icon and the click "send to session memory" button). And then make sure that is already sended and display exact name of my file by code: system("ls", TRUE). And then from "haven" library use function "read_sav" (as a parametr you have to just paste name of your file). That's it.
Upvotes: -3
Reputation: 31
I'm using the R runtime within Colab, and initially had some challenges getting my files into the runtime, caused by the dependencies of the googledrive package. The easiest way I found to load files is to write a small helper function and make my files available unrestricted on google when creating the link (note this example uses the read.spss function from the foreign package to actually load the data into a dataframe):
load_google_drive_data <- function(google_file_url){
g_link = google_file_url
file_id = substr(g_link, regexpr("/d/", g_link) + 3 , regexpr("/view", g_link) -1 )
url = paste("https://drive.google.com/uc?export=download&id=", file_id, sep="")
download.file(url, "file.sav")
df <- read.spss("file.sav", use.value.label=TRUE, to.data.frame=TRUE)
return(df)
}
You call it like this (insert your own link):
load_google_drive_data("https://drive.google.com/file/d/<fileid>/view?usp=sharing")
Upvotes: 3
Reputation: 124
I am doing it this way in a Python Google colab using rpy2 to use R in Python.
It works but unfortunately, you have to use %%R
at the beginning of each cell to use R.
from google.colab import drive
drive.mount('/content/drive')
%load_ext rpy2.ipython
# <%%R> has to be used in each cell with R-Code, with <%R> R-Code can be mixed with Python
%%R
load("file.RData")
Upvotes: 0
Reputation: 171
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.
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.
Now, you need to import R into the workspace, by using a block:
%reload_ext rpy2.ipython
from google.colab import drive
drive.mount('/content/malaria-drive')
%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)
%%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.
Upvotes: 1
Reputation: 40838
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.
Upvotes: 5
Reputation: 4993
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!
Upvotes: 1