102

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?

7 Answers 7

129

Yes.

For a new R-notebook, use this link.

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:

See more details in IRkernel Github.

Sign up to request clarification or add additional context in comments.

7 Comments

So 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?
and how to install R libraries?
Just call install.packages("wordcloud") to install wordcloud library, for example.
Do you have to re-install the packages each time you load the notebook?
@Nathan Exactly. 😁
|
50

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

5 Comments

you do not need to run import rpy2 running the load_ext magic is sufficient
OK, I'll take it out then
I 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.
This 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.
Also 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
22

Open this link in your browser to create a new notebook with R Kernel

https://colab.research.google.com/notebook#create=true&language=r

Comments

13

*****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.

enter image description here

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)

3 Comments

I 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
Hi, @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...
@ 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.
6

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:

enter image description here

As of now if you click at Runtime on menu bar then choose Change Runtime Type, you can choose between R or Python. Changing runtime in Colab

3 Comments

When we click on change runtime type in a new notebook, we just can change the Hardware accelerator option. Any suggestions?
It looks like google removed the option to change kernel. Let me dig around and get back to it.
Thank you so much. It seems Google is forcing us to use python in google colab.
4

To expand on a previous answer, here's how you can move dataframes between the R and Python kernels so you can work with both in the same notebook (for example, if you want to load data in with Pandas, process it with an R package, and then plot it with Bokeh).

# Pandas dataframe to R data frame
!pip3 install rpy2
%load_ext rpy2.ipython
%R -i df
# R data frame to Pandas dataframe
%R seq.data <- read.delim('sequence.index', header=TRUE, stringsAsFactors=FALSE)
seq_data = %R seq.data

Comments

0

Another quick way is to replace postix, .ipynb in colab title to .r
Example: change name of Untitled.ipynb to Untitled.r, and everything works perfectly!

1 Comment

I've just tried to do so with working .ipynb notebook, and it didn't help... Does the runtime have to change automatically?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.