# Wiki

# rnumpy / IPython_integration

## IPython integration

IPython integration is provided by the ipy_rnumpy module.

To use, start IPython, and simply import this module:

$ ipython [...] In [2]: import ipy_rnumpy In [3]:

This has two effects. First, R GUI/plotting functions will now work (of course, you can get the same effect in the normal python interpreter by calling rnumpy.set_interactive, which is what ipy_rnumpy does behind the scenes).

Secondly, a magic word **R** is added, which allows you to enter R code directly.

For short snippets you can simply prefix the expression with **R**:

In [4]: R c("a", "b") Out[4]: [1] "a" "b"

Multi-line expressions work as well:

In [5]: R c("a", "b", ....: "c", "d") Out[5]: [1] "a" "b" "c" "d"

IPython's normal history mechanisms continue to work:

In [5]: type(Out[4]) Out[5]: <class 'rnumpy.RWrapper'>

And it's a bit hard to show on a web page, but of course there's full support for tab completion. Try typing:

In [6]: R summ<tab> summary summary.data.frame summary.matrix summary.Date summary.default summary.mlm summary.POSIXct summary.factor summary.stepfun summary.POSIXlt summary.glm summary.table summary.aov summary.infl summaryRprof summary.aovlist summary.lm summary.connection summary.manova In [6]: R summary

For longer interactions, type **R on** or just **R** on a line by itself to enter "persistent R mode". Let's generate some fake data from a linear model, and then plot it:

In [7]: R Entered R mode (use 'R' or 'R off' to switch back). Tab completion enabled. In [8] R> x <- 1:20 Out[8] R> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 In [9] R> y <- 1 + 2 * x + rnorm(20) Out[9] R> [1] 1.585116 4.192806 7.407136 8.346777 10.992404 12.789048 18.272418 [8] 17.230247 18.053606 21.727818 24.427297 26.304289 27.066721 28.809142 [15] 29.106337 34.080334 33.626247 37.849710 41.036069 41.147020 In [10] R> plot(y ~ x) Out[10] R> NULL In [11] R> R Returned to Python mode (use 'R' or 'R on' to switch back). In [12]:

Note that one returns to Python mode by simply entering 'R' or 'R off'.

If you gave the -pylab option to ipython, to enable matplotlib support, then you can compare R's plotting to Matplotlib's, either by pulling x and y out of the output history:

In [13]: plot(_8, _9, "o") Out[13]: [<matplotlib.lines.Line2D object at 0x2aaaab59a310>]

Or through the rnumpy.r object:

In [14]: from rnumpy import * In [15]: plot(r.x, r.y, "o") Out[15]: [<matplotlib.lines.Line2D object at 0x2aaaab596890>]

There are, of course, some known limitations, and you'll probably discover more -- let us know at Limitations.

Updated