octavemagic

storemagic

# rmagic¶

Magic command interface for interactive work with R via rpy2

## Usage¶

%R

%R [-i INPUT] [-o OUTPUT] [-w WIDTH] [-h HEIGHT] [-d DATAFRAME]
[-u UNITS] [-p POINTSIZE] [-b BG] [-n] [code [code ...]]

Execute code in R, and pull some of the results back into the Python namespace.

In line mode, this will evaluate an expression and convert the returned value to a Python object. The return value is determined by rpy2’s behaviour of returning the result of evaluating the final line.

Multiple R lines can be executed by joining them with semicolons:

```In : %R X=c(1,4,5,7); sd(X); mean(X)
Out: array([ 4.25])```

As a cell, this will run a block of R code, without bringing anything back by default:

```In : %%R
....: Y = c(2,4,3,9)
....: print(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
F-statistic: 4.651 on 1 and 2 DF,  p-value: 0.1638```

In the notebook, plots are published as the output of the cell.

%R plot(X, Y)

will create a scatter plot of X bs Y.

If cell is not None and line has some R code, it is prepended to the R code in cell.

Objects can be passed back and forth between rpy2 and python via the -i -o flags in line:

```In : Z = np.array([1,4,5,10])

In : %R -i Z mean(Z)
Out: array([ 5.])

In : %R -o W W=Z*mean(Z)
Out: array([  5.,  20.,  25.,  50.])

In : W
Out: array([  5.,  20.,  25.,  50.])```

The return value is determined by these rules:

• If the cell is not None, the magic returns None.
• If the cell evaluates as False, the resulting value is returned

unless the final line prints something to the console, in which case None is returned.

• If the final line results in a NULL value when evaluated

by rpy2, then None is returned.

• No attempt is made to convert the final value to a structured array.

Use the –dataframe flag or %Rget to push / return a structured array.

• If the -n flag is present, there is no return value.
• A trailing ‘;’ will also result in no return value as the last

value in the line is an empty string.

The –dataframe argument will attempt to return structured arrays. This is useful for dataframes with mixed data types. Note also that for a data.frame, if it is returned as an ndarray, it is transposed:

```In : dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|S1')]

In : datapy = np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c'), (4, 5, 'e')], dtype=dtype)

In : %%R -o datar
datar = datapy
....:

In : datar
Out:
array([['1', '2', '3', '4'],
['2', '3', '2', '5'],
['a', 'b', 'c', 'e']],
dtype='|S1')

In : %%R -d datar
datar = datapy
....:

In : datar
Out:
array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c'), (4, 5.0, 'e')],
dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|S1')])```

The –dataframe argument first tries colnames, then names. If both are NULL, it returns an ndarray (i.e. unstructured):

```In : %R mydata=c(4,6,8.3); NULL

In : %R -d mydata

In : mydata
Out: array([ 4. ,  6. ,  8.3])

In : %R names(mydata) = c('a','b','c'); NULL

In : %R -d mydata

In : mydata
Out:
array((4.0, 6.0, 8.3),
dtype=[('a', '<f8'), ('b', '<f8'), ('c', '<f8')])

In : %R -o mydata

In : mydata
Out: array([ 4. ,  6. ,  8.3])```
positional arguments:
code
optional arguments:
 -i INPUT, --input INPUT Names of input variable from shell.user_ns to be assigned to R variables of the same names after calling self.pyconverter. Multiple names can be passed separated only by commas with no whitespace. -o OUTPUT, --output OUTPUT Names of variables to be pushed from rpy2 to shell.user_ns after executing cell body and applying self.Rconverter. Multiple names can be passed separated only by commas with no whitespace. -w WIDTH, --width WIDTH Width of png plotting device sent as an argument to png in R. -h HEIGHT, --height HEIGHT Height of png plotting device sent as an argument to png in R. -d DATAFRAME, --dataframe DATAFRAME Convert these objects to data.frames and return as structured arrays. -u UNITS, --units UNITS Units of png plotting device sent as an argument to png in R. One of [“px”, “in”, “cm”, “mm”]. -p POINTSIZE, --pointsize POINTSIZE Pointsize of png plotting device sent as an argument to png in R. -b BG, --bg BG Background of png plotting device sent as an argument to png in R. -n, --noreturn Force the magic to not return anything.

%Rpush

A line-level magic for R that pushes variables from python to rpy2. The line should be made up of whitespace separated variable names in the IPython namespace:

```In : import numpy as np

In : X = np.array([4.5,6.3,7.9])

In : X.mean()
Out: 6.2333333333333343

In : %Rpush X

In : %R mean(X)
Out: array([ 6.23333333])```

%Rpull

%Rpull [-d] [outputs [outputs ...]]

A line-level magic for R that pulls variables from python to rpy2:

```In : _ = %R x = c(3,4,6.7); y = c(4,6,7); z = c('a',3,4)

In : %Rpull x  y z

In : x
Out: array([ 3. ,  4. ,  6.7])

In : y
Out: array([ 4.,  6.,  7.])

In : z
Out:
array(['a', '3', '4'],
dtype='|S1')```

If –as_dataframe, then each object is returned as a structured array after first passed through “as.data.frame” in R before being calling self.Rconverter. This is useful when a structured array is desired as output, or when the object in R has mixed data types. See the %%R docstring for more examples.