How to use R with Google Colaboratory?

82,589

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:

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.

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)

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:

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

Share:
82,589
korakot
Author by

korakot

Always love to learn new things.

Updated on January 29, 2021

Comments

  • korakot
    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
    Buthetleon almost 5 years
    you do not need to run import rpy2 running the load_ext magic is sufficient
  • korakot
    korakot almost 5 years
    OK, I'll take it out then
  • NelsonGon
    NelsonGon over 4 years
    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?
  • chopin_is_the_best
    chopin_is_the_best over 4 years
    and how to install R libraries?
  • korakot
    korakot over 4 years
    Just call install.packages("wordcloud") to install wordcloud library, for example.
  • Btibert3
    Btibert3 over 4 years
    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.
  • korakot
    korakot over 4 years
    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.
  • Paloha
    Paloha about 4 years
    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
  • Jas
    Jas about 4 years
    Do you have to re-install the packages each time you load the notebook?
  • Nathan
    Nathan almost 4 years
    Does 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
    korakot almost 4 years
    @Nathan Exactly. 😁
  • Luis
    Luis almost 4 years
    When we click on change runtime type in a new notebook, we just can change the Hardware accelerator option. Any suggestions?
  • x85ms16
    x85ms16 almost 4 years
    It looks like google removed the option to change kernel. Let me dig around and get back to it.
  • Luis
    Luis almost 4 years
    Thank you so much. It seems Google is forcing us to use python in google colab.
  • M Terry
    M Terry over 3 years
    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
  • hamagust
    hamagust about 3 years
    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...
  • Nosey
    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
    TopCoder2000 about 3 years
    I've just tried to do so with working .ipynb notebook, and it didn't help... Does the runtime have to change automatically?