How to use R with Google Colaboratory?
Solution 1
Yes.
For a new R-notebook, use this link. (shorthand is colab.to/r )
You can learn from IRkernel demos, e.g. demo.ipynb
Save a copy in your Google Drive, and make any changes you need.
2 more demos:
- Display.ipynb shows how to display HTML, images.
- Comm_Demo.ipynb shows how to communicate between R and JavaScript.
See more details in IRkernel Github.
Solution 2
In case you want to use Python and R together, you can use R magic for some cells.
# activate R magic
%load_ext rpy2.ipython
Then, whenever you want to use R, you begin the cell with %%R
%%R
x <- 42
print(x)
More details in rpy2 documentation
Solution 3
Open this link in your browser to create a new notebook with R Kernel
https://colab.research.google.com/notebook#create=true&language=r
Solution 4
*****Working as of Friday 13th November 2020
Go this URL https://colab.to/r whilst signed into colab and that should do it.
You can check if R in Runtime -> Change runtime type, but it should already be setup.
To mount google drive:
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)
Solution 5
Update: this doesn't work anymore (july 2020).
The above link on answers above takes directly to R notebook, there you have an option change between R or python. It is strange that Google is changing services just like this. Hence stackoverflow not a great platform to promote tools created by profit mongering/data-selling companies.
Old answer:
As of now if you click at Runtime
on menu bar then choose Change Runtime Type
, you can choose between R or Python.
Comments
-
korakot over 3 years
Google Colaboratory supports Python version 2.7 and 3.6
I saw an example how to use Swift in Colab a while ago.
Today, I happened to run
!jupyter-kernelspec list
And found a new kernel: IRkernel
Available kernels: ir /usr/local/share/jupyter/kernels/ir python2 /usr/local/share/jupyter/kernels/python2 python3 /usr/local/share/jupyter/kernels/python3 swift /usr/local/share/jupyter/kernels/swift
Is it now possible to use R in Colab as well? No hassle in installing R kernel?
-
Buthetleon almost 5 yearsyou do not need to run
import rpy2
running theload_ext
magic is sufficient -
korakot almost 5 yearsOK, I'll take it out then
-
NelsonGon over 4 yearsSo one should use this each time one wants to run R on Google Colab?! Running it does return some warning(about non Google authorised). Is it safe?
-
chopin_is_the_best over 4 yearsand how to install R libraries?
-
korakot over 4 yearsJust call
install.packages("wordcloud")
to install wordcloud library, for example. -
Btibert3 over 4 yearsI totally understand that using the magic commands is better than no option at all, but you have to admit that if you wanted to R throughout the entire notebook, is kinda nuts that we would have to add %%R to the top over every single code cell one by one.
-
korakot over 4 yearsThis answer is only good if you want to mix some R into a Python notebook. If you write mostly R, please use my first answer above.
-
Paloha about 4 yearsAlso I have troubles installing packages using this method in Colab. I wanted to install one package, call one function from that package and then work with the results in Python. For some packages it works, for some it does not. I get errors with dependencies e.g.
installation of package ‘Rmpfr’ had non-zero exit status
-
Jas about 4 yearsDo you have to re-install the packages each time you load the notebook?
-
Nathan almost 4 yearsDoes this answer imply that there is no way to launch an R notebook through an actual Google-built UI, but only a way that involves a secret query-string incantation?
-
korakot almost 4 years@Nathan Exactly. 😁
-
Luis almost 4 yearsWhen we click on change runtime type in a new notebook, we just can change the Hardware accelerator option. Any suggestions?
-
x85ms16 almost 4 yearsIt looks like google removed the option to change kernel. Let me dig around and get back to it.
-
Luis almost 4 yearsThank you so much. It seems Google is forcing us to use python in google colab.
-
M Terry over 3 yearsI just tried it and it works. However, I still cannot mount my drive. After implementing the code, the “mount drive” drive icon appears on the left panel, but clicking it displays a notification at the bottom left corner that “mounting your google drive is only available on hosted python runtimes”. I don’t know if there’s a workaround, it will be very helpful
-
hamagust about 3 yearsHi, @Nosey, can you explain a little more about the stage to mount the drive? Are you still able to get the url to get the authentication code? it returns "Error: Can't get Google credentials. Are you running googledrive in a non-interactive session? ". I do not see any "mount drive" icon mentioned by MTerry...
-
Nosey about 3 years@ hamagust, perhaps see if this file exists? My python 3.6 directory is empty, python 3.7 has this path. It installs the libraries if I change 3.6 to 3.7 but I can't find my files. I think the answer lies here. I will return when I have time. Please let us know if you solve.
-
TopCoder2000 about 3 yearsI've just tried to do so with working .ipynb notebook, and it didn't help... Does the runtime have to change automatically?