Intro

This notebook was created using vanilla IPython notebook with Python extension rpy2 installed. Once installed and set up, this interface is invoked like so:

ipython notebook

What follows is a short notebook that can be used to test your own setup.

Load the extension for Rmagic

Former extension rmagic is part of rpy2 as of rpy2 2.4. Many online tutorials still use the old way of loading rmagic (%load_ext rmagic), but the current correct method is used below (%load_ext rpy2.ipython). The functionality is still referred to in this document as Rmagic because, well, it's snazzier than "rpy2 IPython magic integration" and it's more easily searched.


In [1]:
%load_ext rpy2.ipython

Run some R code


In [2]:
%R library(ggplot2)


Out[2]:
<StrVector - Python:0x1026411b8 / R:0x106a9e000>
[str, str, str, ..., str, str, str]

In [3]:
%R print(head(iris))


  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

In [4]:
%%R -w 800 -h 600 -u px
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) + geom_point() + theme_bw()