How to Use R with Google Colaboratory

How to use R with Google Colaboratory?

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.

How to open a new Google Colab notebook for R?

I'm not sure if Google Colab still allows R, but you can try this link.
Save a copy in your Google Drive, and make any changes you need.

Personally, I recommend using Kaggle R notebook which supports R and Rstan by default.

Using R library sf in Google Colab

Thanks to a GitHub user, I have found the solution (Calling sf on google colab throws errors)

You should install sf without adding ubuntugis repository in a fresh collab notebook :

system("apt-get -y update")
system("apt-get install -y libudunits2-dev libgdal-dev libgeos-dev libproj-dev")
install.packages("sf")
library("sf")

No error arises then:

Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)

also installing the dependencies ‘proxy’, ‘e1071’, ‘wk’, ‘classInt’, ‘Rcpp’, ‘s2’, ‘units’


Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 7.0.0; sf_use_s2() is TRUE

Running RStudio on Google Colab

I am using Google Colab and RStudio from time to time. To make the set up easier, I usually copy & paste the following setup script (and use alternative 2).

After creating a new Google Colab Notebook, execute the following commands:

# Add new user called "rstudio" and define password (here "password123")
!sudo useradd -m -s /bin/bash rstudio
!echo rstudio:password123 | chpasswd

# Install R and RStudio Server (Don't forget to update version to latest version)
!apt-get update
!apt-get install r-base
!apt-get install gdebi-core
!wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-1.4.1103-amd64.deb
!gdebi -n rstudio-server-1.4.1103-amd64.deb

#===============================================================================
# ALTERNATIVE 1: Use ngrok
#===============================================================================
# Advantage: Runs in the background
# Disadvantage: Not so stable
# (often 429 errors during RStudio usage due to max 20 connections without account)
# Optionally register for a free accoount which gets this number up to 40 connections:
# https://ngrok.com/pricing

# Install ngrok (https://ngrok.com/)
!wget -c https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip -o ngrok-stable-linux-amd64.zip

# Run ngrok to tunnel RStudio app port 8787 to the outside world.
# This command runs in the background.
get_ipython().system_raw('./ngrok http 8787 &')

# Get the public URL where you can access the Dash app. Copy this URL.
! printf "\n\nClick on the following link: "
! curl -s http://localhost:4040/api/tunnels | python3 -c \
"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"

# ==> To access to the RStudio server
# - click on this link and
# - use the username "rstudio" and
# - the password you defined at the first cell ("password123" in this example).

#===============================================================================
# ALTERNATIVE 2 (preferred): Use localtunnel
#===============================================================================
# (see also: https://github.com/naru-T/RstudioServer_on_Colab/blob/master/Rstudio_server.ipynb)
# Advantage: Stable usage of RStudio
# Disadvantage: Does not run in the background (i.e. Colab blocked)

# Install localtunnel
!npm install -g npm
!npm install -g localtunnel

# Run localtunnel to tunnel RStudio app port 8787 to the outside world.
# This command runs in the background.
!lt --port 8787

# ==> To access to the RStudio server
# - click on this link,
# - click button "Click to Continue" on the "friendly reminder" page,
# - use the username "rstudio" and
# - the password you defined at the first cell ("password123" in this example).

How to access the shell in google Colab when running the R kernel

There is the base R function system which allows you to call the shell behind the Notebook. Since colab suppresses the stdout, one needs to set the option intern = TRUE to see the result as an R character vector. To properly display line breaks, I defined a function called shell_call which is analog to ! in ipython:

shell_call <- function(command, ...) {
result <- system(command, intern = TRUE, ...)
cat(paste0(result, collapse = "\n"))
}

shell_call("ls -lah / | head")

Sample Image



Related Topics



Leave a reply



Submit