##// END OF EJS Templates
reworking docstring
Jonathan Taylor -
Show More
@@ -151,10 +151,61 b' class RMagics(Magics):'
151 @line_cell_magic
151 @line_cell_magic
152 def R(self, line, cell=None):
152 def R(self, line, cell=None):
153 '''
153 '''
154 A line_cell_magic for R that executes
154 Execute code in R, and pull some of the results back into the Python namespace.
155 some code in R (evaluating it with rpy2) and
155
156 stores some of the results
156 In line mode, this will evaluate an expression and convert the returned value to a Python object.
157 in the ipython shell.
157 The return value is determined by rpy2's behaviour of returning the result of evaluating the
158 final line. Multiple R lines can be executed by joining them with semicolons.
159
160 In [9]: %R X=c(1,4,5,7); sd(X); mean(X)
161 Out[9]: array([ 4.25])
162
163 As a cell, this will run a block of R code, without bringing anything back by default::
164
165 In [10]: %%R
166 ....: Y = c(2,4,3,9)
167 ....: print(summary(lm(Y~X)))
168 ....:
169
170 Call:
171 lm(formula = Y ~ X)
172
173 Residuals:
174 1 2 3 4
175 0.88 -0.24 -2.28 1.64
176
177 Coefficients:
178 Estimate Std. Error t value Pr(>|t|)
179 (Intercept) 0.0800 2.3000 0.035 0.975
180 X 1.0400 0.4822 2.157 0.164
181
182 Residual standard error: 2.088 on 2 degrees of freedom
183 Multiple R-squared: 0.6993,Adjusted R-squared: 0.549
184 F-statistic: 4.651 on 1 and 2 DF, p-value: 0.1638
185
186 In the notebook, plots are published as the output of the cell.
187
188 %R plot(X, Y)
189
190 will create a scatter plot of X bs Y.
191
192 If cell is not None and line has some R code, it is prepended to
193 the R code in cell.
194
195 Objects can be passed back and forth between rpy2 and python via the -i -o flags in line
196
197
198 In [14]: Z = np.array([1,4,5,10])
199
200 In [15]: %R -i Z mean(Z)
201 Out[15]: array([ 5.])
202
203
204 In [16]: %R -o W W=Z*mean(Z)
205 Out[16]: array([ 5., 20., 25., 50.])
206
207 In [17]: W
208 Out[17]: array([ 5., 20., 25., 50.])
158
209
159 If the cell is None, the resulting value is returned,
210 If the cell is None, the resulting value is returned,
160 after conversion with self.Rconverter
211 after conversion with self.Rconverter
@@ -194,7 +245,7 b' class RMagics(Magics):'
194
245
195 # read out all the saved .png files
246 # read out all the saved .png files
196
247
197 images = [file(imgfile).read() for imgfile in glob("%s/Rplots*png" % tmpd)]
248 images = [open(imgfile, 'rb').read() for imgfile in glob("%s/Rplots*png" % tmpd)]
198
249
199 # now publish the images
250 # now publish the images
200 # mimicking IPython/zmq/pylab/backend_inline.py
251 # mimicking IPython/zmq/pylab/backend_inline.py
General Comments 0
You need to be logged in to leave comments. Login now