I've been very pleased with knitr when writing dynamic documents for the web or in LaTeX. As I'm both an R and Python fan, I often found myself wanting to write Python code in knitr. However, the Python engine in knitr is quite basic and doesn't support workspace-like environments such that variables declared in one chunk are available in the next. Plus it doesn't support matplotlib either.

Hence, I've started working on a new addon project to knitr named knitron which provides both of these features to the former.

Knitron works by lazily starting a global IPython kernel the first time a code chunk gets rendered by knitr and this kernel is reused for all consecutive chunks. This way all the computation done in any previous chunk is available in the current chunk, providing R-like behaviour for Python. This basically allows you to do something like

x = 5

in one chunk and access it in the next:

x
## 5

All this is achieved by a Python wrapper that sends the chunk to the existing IPython kernel and returns the output in JSON, which in turn is then passed back to knitr.

As far as matplotlib is concerned, the wrapper script checks the pylab state machine for any plots that the user might have created in a chunk. If this check turns out positive, the images are written to files and passed back to knitr. This works for both markdown and latex, as matplotlib supports either output format.

So, here's a nice plot:

import numpy as np

x = np.linspace(0, 2 * np.pi, 100)
y1 = np.sin(x)
y2 = np.sin(3 * x)
plt.fill(x, y1, 'b', x, y2, 'r', alpha=0.3)

plot of chunk unnamed-chunk-4

Thus far most of the image functions that knitr provided are supported, including fig.width and fig.height.

In order to install knitron, the only thing to do is:

library(devtools)
install_github("knitron", "fhirschmann")

Knitron is not on CRAN yet, but hopefully will be soon! It can be found on Github.


Comments

comments powered by Disqus