Lets start with a typical R command. This will throw an exception.
X=c(1,4,5,7);
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-96271f542f68> in <module>() ----> 1 X=c(1,4,5,7); NameError: name 'c' is not defined
To enable the magics below, execute %load_ext rpy2.ipython.
%load_ext rpy2.ipython
%R X=c(1,4,5,7); sd(X); mean(X)
<FloatVector - Python:0x10cd94170 / R:0x101c6b358> [4.250000]
X
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-5-253bcac7dd80> in <module>() ----> 1 X NameError: name 'X' is not defined
Again X is only defined in R.
%%R
Y = c(2,4,3,9)
summary(lm(Y~X))
Call: lm(formula = Y ~ X) Residuals: 1 2 3 4 0.88 -0.24 -2.28 1.64 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.0800 2.3000 0.035 0.975 X 1.0400 0.4822 2.157 0.164 Residual standard error: 2.088 on 2 degrees of freedom Multiple R-squared: 0.6993, Adjusted R-squared: 0.549 F-statistic: 4.651 on 1 and 2 DF, p-value: 0.1638
%R plot(X, Y)
The plot above is a R plot.
From ipython to R.
Z = [3, 1, 8, 2]
%R -i Z mean(Z)
<FloatVector - Python:0x10e08f440 / R:0x1099339a8> [3.500000]
from R to ipython
%R -o Zm Zm=mean(Z)
<FloatVector - Python:0x10776c638 / R:0x109a8ea98> [3.500000]
Zm[0]
3.5