Using R and Python together in a notebook

Installation

$ sudo apt-get install python-rpy2
$ conda install -c r r-essentials
$ conda install -c r r-rjson
$ pip install rpy2

Load the magic

In [1]:
%load_ext rpy2.ipython

Create a dataframe

Use Python and pandas to create a dataframe.

In [2]:
import pandas as pd
df_from_python = pd.DataFrame({'A': [4, 3, 5, 2, 1, 7, 7, 5, 9],
                               'B': [0, 4, 3, 6, 7, 10,11, 9, 13],
                               'C': [1, 2, 3, 1, 2, 3, 1, 2, 3]})
df_from_python
Out[2]:
A B C
0 4 0 1
1 3 4 2
2 5 3 3
3 2 6 1
4 1 7 2
5 7 10 3
6 7 11 1
7 5 9 2
8 9 13 3

Create plot in R

Import the dataframe df with the -i argument.

In [3]:
%%R -i df_from_python
require(ggplot2)
# Plot the DataFrame df
ggplot(data=df_from_python) + geom_point(aes(x=A, y=B, color=C)) + ggtitle('R scatter!')
/home/jitsejan/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:186: RRuntimeWarning: Loading required package: ggplot2

  warnings.warn(x, RRuntimeWarning)

Create a dataframe using R

Create a dataframe and export it using -o.

In [4]:
%%R -o df_from_r
d <- c(2, 6, 6, 8, 4, 7, 5, 9, 3)
e <- c(3, 1, 4, 3, 6, 7, 10,11, 9)
f <- c(3, 1, 2, 3, 1, 2, 3, 1, 2)

df_from_r <- data.frame(d, e, f)

Now the variable is available for Python.

In [5]:
df_from_r
Out[5]:
d e f
1 2.0 3.0 3.0
2 6.0 1.0 1.0
3 6.0 4.0 2.0
4 8.0 3.0 3.0
5 4.0 6.0 1.0
6 7.0 7.0 2.0
7 5.0 10.0 3.0
8 9.0 11.0 1.0
9 3.0 9.0 2.0

Create plot with Python

In [6]:
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
matplotlib.style.use('ggplot')

plt.scatter(x=df_from_r['d'], y=df_from_r['e'], c=df_from_r['f'])
plt.title('Python scatter!')
plt.xlabel('A')
plt.ylabel('B')
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(10, 10)