Performing R magic with IPython
You have probably heard of, and maybe used, the IPython Notebook. If not, then I strongly recommend you try it as it's becoming the standard for reproducible science. Among many other features, IPython provides a framework of extensible commands called magics, which allows you to extend the language in many useful ways.
There are magic functions to deal with R. As you will see in our example, it makes R interfacing much more declarative and easy. This recipe will not introduce any new R functionalities, but hopefully, it will make clear how IPython can be an important productivity boost for scientific computing in this regard.
Getting ready
You will need to follow the previous getting ready steps of the rpy2 recipe. You will also need IPython. You can use the standard command line or any of the IPython consoles, but the recommended environment is the notebook.
If you are using our notebooks, open the 00_Intro/R_magic.ipynb
notebook. A notebook is more complete than the recipe presented here with more chart examples. For brevity here, we concentrate only on the fundamental constructs to interact with R using magics.
How to do it…
This recipe is an aggressive simplification of the previous one because it illustrates the conciseness and elegance of R magics:
- The first thing you need to do is load R magics and ggplot2:
import rpy2.robjects.lib.ggplot2 as ggplot2 %load_ext rpy2.ipython
- Note that the
%
starts an IPython-specific directive. - Just as a simple example, you can write on your IPython prompt:
%R print(c(1, 2))
- See how easy it's to execute the R code without using the
robjects
package. Actually, rpy2 is being used to look under the hood, but it has been made transparent.
- Note that the
- Let's read the
sequence.index
file that was downloaded in the previous recipe:%%R seq.data <- read.delim('sequence.index', header=TRUE, stringsAsFactors=FALSE) seq.data$READ_COUNT <- as.integer(seq.data$READ_COUNT) seq.data$BASE_COUNT <- as.integer(seq.data$BASE_COUNT)
- Note that you can specify that the whole IPython cell should be interpreted as R code (note the double
%%
). As you can see, there is no need for a function parameter name translation or (alternatively) explicitly call therobjects.r
to execute a code.
- Note that you can specify that the whole IPython cell should be interpreted as R code (note the double
- We can now transfer a variable to the Python namespace (where we could have done Python-based operations):
seq_data = %R seq.data
- Let's put this data frame back in the R namespace, as follows:
%R -i seq_data %R print(colnames(seq_data))
- The
-i
argument informs the magic system that the variable that follows on the Python space is to be copied in the R namespace. The second line just shows that the data frame is indeed available in R. We actually did not do anything with the data frame in the Python namespace, but this serves as an example on how to inject an object back into R.
- The
- The R magic system also allows you to reduce code as it changes the behavior of the interaction of R with IPython. For example, in the ggplot2 code of the previous recipe, you do not need to use the
.png
anddev.off
R functions, as the magic system will take care of this for you. When you tell R to print a chart, it will magically appear in your notebook or graphical console. For example, the histogram plotting code from the previous recipe is now simply:%%R bar <- ggplot(seq_data) + aes(factor(CENTER_NAME)) + geom_bar() + theme(axis.text.x = element_text(angle = 90, hjust = 1)) print(bar)
R magics makes interaction with R particularly easy. This is true if you think about how cumbersome multiple language integration tends to be.
The notebook has a few more examples, especially with chart printing, but the core of R-magic interaction is explained before.
See also
- For basic instructions on IPython magics, see this notebook at http://nbviewer.ipython.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb
- A list of default extensions is available at http://ipython.org/ipython-doc/dev/config/extensions/
- A list of third-party magic extensions can be found at https://github.com/ipython/ipython/wiki/Extensions-Index
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.