How to use R and Python with Google Colaboratory Notebook?

Mohamed Ezzaouia
2 min readJun 21, 2019

--

In case you want to use R and Python together using Google Colab, below are the steps to follow. Please use pip rather than pip3 if you are using Python2.

Setup instructions

Step 1: Install Jupyter

Install Jupyter on your local machine.

Step 2: Install R

Install R on your local machine.

Step 3: Install and enable jupyter_http_over_ws

pip3 install jupyter_http_over_ws
jupyter serverextension enable — py jupyter_http_over_ws

Step 4: Install rpy2

pip3 install rpy2

Step 4: Start Jupyter server and authenticate

jupyter notebook \
— NotebookApp.allow_origin=’https://colab.research.google.com' \
— port=8888 \
— NotebookApp.port_retries=0

Step 5: Connect to Jupyter with Google Colab

Connect to Jupyter
Connect to Jupyter cont.

Execute/Share R and Python code

Step 1: Load R (exp. Google Colab cell)

%reload_ext rpy2.ipython

Step 2: Run R (exp. Google Colab cell)

%%R
# r imports …
library(ggplot2)

Step 3: Import data from R into Python (exp. Google Colab cell)

%R -o df1 -o df2

Step 4: Import data from Python into R (exp. Google Colab cell)

%R -i df3 -i df4

--

--