Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
1999 views
Kernel: Unknown Kernel

Rmagic in IPython

This files is part of the examples collection of the Sagemath Cloud.

Rmagic is an extension for IPython. Check out the full IPython notebook for additional details! It is based on RPy2 and allows to seamlessly talk to an underlying R session via an IPython notebook.

To activate it, see the cell below. There are basically only a few core commands:

  • %R runs a line of R code, return values can be assigned via var = %R ....

  • %%R -i <input> -o <output> ... runs the entire cell in R and -i and -o specify the variables for input and output.

  • %Rpush ... sends the data of a given variable to R.

  • %Rpull ... retrieves the variable (namespace is populated) and the data of a variable inside R.

  • %Rget ... is similar to Rpull, but only retrieves the actual data.

%pylab inline %load_ext rmagic
Populating the interactive namespace from numpy and matplotlib
import numpy as np
%R print(seq(10)) %R print(summary(factor(c("a", "b", "b", "a", "c", "a", "c"))))
%%R v <- 5.5 a <- seq(10) + v print(summary(a)) print(sd(a))

a only exists in R, hence the following error:

a
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-5-60b725f10c9c> in <module>() ----> 1 a NameError: name 'a' is not defined

%Rget pulls and converts the data into Python:

a = %Rget a a
array([ 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5])
type(a)
numpy.ndarray

%Rpull is similar, and defines the variable, too:

%Rpull v
v
array([ 5.5])

%%R runs the given cell in R and -o [variable] "outputs" it to Python

%%R -o b b <- c(2,3,4,3,5,6,5,6,7,8) print(paste(length(a), "==", length(b), "?"))
print(b)
[ 2. 3. 4. 3. 5. 6. 5. 6. 7. 8.]

Plots

Basically, they are stright forward. Multiple simultaneous plots are displayed accordingly.

%R plot(a, b, 'b-')
%%R -o lmod lmod <- lm(b ~ a) print(lmod)

Interested in the coefficients?

Use R's slot accessor $ via a call to R in %R to retrieve the coefficients as a NumPy array.

coeffs = %R lmod$coefficients coeffs
array([-1.7, 0.6])
%R print(summary(lmod))

Plotting in a 2x2 grid via R's par command and setting the output canvas size to 800x600 pixels.

%%R -w 800 -h 600 par(mfrow=c(2,2)) plot(lmod)
%%R -o faithful library(datasets) print(summary(faithful))
%%R library(lattice) print( wireframe(volcano, shade = TRUE, zlab = "", aspect = c(61.0/87, 0.5), light_source = c(10,0,10)))

Advanced Example: PCA

%%R -o pca_usarrest library("stats") pca_usarrest <- princomp(USArrests, cor=TRUE) print(summary(pca_usarrest))
%%R print(summary(pca_usarrest))
%%R biplot(pca_usarrest)

The pca_usarrest variable references a datastructure from R. Applying R functions via RPy2 directly is no problem.

from rpy2 import robjects as ro print(ro.r.summary(pca_usarrest))
%R print(help(sum))