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!
There are two ways to run R in Colab The first way is to use the rpy2 package in the Python runtime. This method allows you to execute R and Python syntax together. The second way is to actually start the notebook in the R runtime.
You can search Colab notebooks using Google Drive. Clicking on the Colab logo at the top left of the notebook view will show all notebooks in Drive. You can also search for notebooks that you have opened recently using File > Open notebook. Code is executed in a virtual machine private to your account.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With