Looking for an iPython Notebook equivalent for r: amazed by knitr so far
That was me about 3 weeks ago. iPython and not IPython, r and not R. As you can tell, ignorant wannabe. By that time, I had discovered the rmagic
extension—contains some magic functions for working with R via rpy2
—but there was this strong feeling that there had to be more. So I turned to the community.
Fortunately, the Python community is patient and forgiving. As such, guidance came from none other than Fernando Perez, the IPython creator himself:
Native R kernel for IPython
Meet IRkernel: the native interface to the R kernel from the IPython notebook.
Seems too good to be true, right? Plus, the warning was clear—hic sunt dracones:
This is still experimental and unreliable.
So a couple of weeks in and with an approaching Data Science Specialization deadline, I finally gave it a shot. Since for the Data Science Specialization courses I'm using a VM built using the DataScienceToolbox, the following steps will assume an Ubuntu/Debian installation running IPython 2.3. If you're using IPython 3.0.dev instead or if you'd rather go the homebrew or macports path on OS X, make sure to directly check the docs.
Installing IRkernel
First step is to install both the
zmq
development headers (to compilerzmq
), and thecurl
headers (for Rdevtools
):$ sudo apt-get install libzmq3-dev libcurl4-openssl-dev
Then switch to the R console and ensure you have the
devtools
R package available; otherwise install it using:> install.packages("devtools")
Next step is to install the necessary development dependencies
# Need RCurl for install_github > install.packages("RCurl") > library(devtools) > install_github("armstrtw/rzmq") > install_github("takluyver/IRdisplay") > install_github("takluyver/IRkernel")
Running R code, natively in IPython notebook
Once everything is set up, start the Notebook with this command:
$ sudo ipython notebook \
--profile=dst \
--KernelManager.kernel_cmd="['R', '-e', 'IRkernel::main()', '--args', '{connection_file}']"
Note: The dst
profile is DataScienceToolbox-specific.
All that's left now is to start writing your favorite R code in your favorite IPython Notebook:
...and enjoy!