Access Google Drive Using Google Colab Running an R Kernel | by Kate Wall | Towards Data Science

This article describes a step-by-step approach for using Google Colaboratory with an R kernel.
As an engineer and biostatistician, I code primarily in python and R. Within the last six months, I have decided, for small projects, it is fastest to share code with others through Google Drive and Google Colaboratory. The convenient access to shared cloud storage cannot be beat for small, informal projects. Avoiding issues of shared server access, working directory updates, and Git confusion saves time, particularly when collaborators have varying levels of comfort/fluency with data science and programming language.
This approach builds upon that of a previous Medium article, attached here. It served as an inspiration and initial guide; however, for me, it threw errors, which seem to be Colab versioning artifacts. After working out a few kinks, I was able to successfully access my Google Drive file storage from a Google Colaboratory notebook file via an R kernel. The full source code is uploaded to Github.
Keep in mind, R can be run in individual cells using rmagic functions. As such, the R kernel is not the only way to use R in Colab. But, if you would like the notebook to be suitable for later local use or to ensure others can replicate your R code, you may want to just change the whole kernel to R but still use a browser-based cloud instance.
Why one would want to use R in Colab:
Numerous specialized statistical packages are built in R but not python.R has easy syntax for pretty visuals.For many data science roles, you need to know both R and python.
Numerous specialized statistical packages are built in R but not python.
R has easy syntax for pretty visuals.
For many data science roles, you need to know both R and python.
What would give one problems when running R in Colab:
Mounting your Drive only works in python runtimes.Mounting your Drive is one of the key drivers for using Google Colab.The mounted Drive provides endless functionality, and it drove me nuts trying to figure this out.
Mounting your Drive only works in python runtimes.
Mounting your Drive is one of the key drivers for using Google Colab.
The mounted Drive provides endless functionality, and it drove me nuts trying to figure this out.
Using the default setup and default python kernel, mounting your Google Drive in Colab is no problem. Easy peasy. Read how to do it several ways here. Or one way here. Or just click the button that I show in the screenshot below.
However, after you change the kernel, this function ceases to work. Hence, you may find yourself needing to follow my proposed three step plan for maintaining access to your Google Drive files. Please note, this took some finagling and a couple minutes runtime. It is slightly slower to run the setup chunks but worth the wait. Luckily, there are only three of them.
Open your browser and go to https://colab.to/r. You’ll see by going to Runtime > Change Runtime Type, the R kernel has already been selected.
Install the Httpuv library. It allows R code to interact with HTTP and WebSocket clients to serve web traffic from an R process.
install.packages(“googledrive”) #only need to install occasionally install.packages(“httpuv”) library(“googledrive”) library(“httpuv”)
Also, change the local path. This changes during occasional updates, so this exact line may fall out of date.
if (file.exists("/usr/local/lib/python3.7/dist-packages/google/colab/_ipython.py")) { #may update python version                                         #occasionally  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)}
Run this chunk SEPARATELY. Do not combine with the previous chunk because, though it may seem unproblematic, combining the two will throw errors like you would not believe.
drive_auth(use_oob = TRUE, cache = FALSE)
Now, you have initialized the connection between your Google Drive and your Google Colab session. Now you have two options.
If you would like to download a file directly from a web URL to the local Google Colab session, then go to step II.
If you would like to download a file from your Google Drive folders to the local Colab session, then go to step III.
First check the working directory of your local session.
getwd()>>>'/content'
The working directory is the content folder.
Next, download the file, and unzip the file. This particular example is a public dataset from the CDC on health-related behaviors that contribute to the leading causes of death and disability among US youth and adults. The unzip will yield a warning message, but if you check the files directory listing, your downloaded, unzipped file will appear.
download.file('https://data.cdc.gov/api/views/vba9-s8jp/rows.csv?           accessType=DOWNLOAD','/content/dnld_direct_from_web.csv')unzip(zipfile="/content/downloaded.zip", exdir=".")>>>Warning message in unzip(zipfile = "/content/downloaded.zip", >>>exdir = "."):>>>“error 1 in extracting from zip file”
Now that it is local, you can read in the csv or other file type using your typical R import functions.
library(readr) a <- read.csv(‘/content/dnld_direct_from_web.csv’) head(a)
This employs the googledrive package. You can get the file using the drive_get function. Next, you have to explicitly download the file using the drive_download function.
x <- drive_get("~/Folder1/Folder2/Nutrition__Physical_Activity__              and_Obesity_-_Youth_Risk_Behavior_              Surveillance_System.csv")drive_download(x)z <- read.csv("/content/Nutrition__Physical_Activity__and_Obesity_-              _Youth_Risk_Behavior_Surveillance_System.csv")head(z)
Similarly, this file has now landed in your file directory. Now that it is local, you can read in the csv or other file type using your typical R import functions.
Ta Da! It is my hope this tutorial saves you the headache I incurred. Please let me know if you find a better implementation of this functionality! The source code is uploaded to Github.