##// END OF EJS Templates
added use cases for rpush/rpull, tried fixing indents for examples in R docstring
Jonathan Taylor -
Show More
@@ -1,3 +1,4 b''
1
1 # -*- coding: utf-8 -*-
2 # -*- coding: utf-8 -*-
2 """
3 """
3 R related magics.
4 R related magics.
@@ -64,21 +65,26 b' class RMagics(Magics):'
64 self.output = []
65 self.output = []
65 return value
66 return value
66
67
68 @skip_doctest
67 @line_magic
69 @line_magic
68 def Rpush(self, line):
70 def Rpush(self, line):
69 '''
71 '''
70 A line-level magic for R that pushes
72 A line-level magic for R that pushes
71 variables from python to rpy2.
73 variables from python to rpy2. The line should be made up
74 of whitespace separated variable names in the IPython
75 namespace.
76
77 In [7]: import numpy as np
78
79 In [8]: X = np.array([4.5,6.3,7.9])
72
80
73 Parameters
81 In [9]: X.mean()
74 ----------
82 Out[9]: 6.2333333333333343
75
83
76 line: input
84 In [10]: %Rpush X
77
85
78 A white space separated string of
86 In [11]: %R mean(X)
79 names of objects in the python name space to be
87 Out[11]: array([ 6.23333333])
80 assigned to objects of the same name in the
81 R name space.
82
88
83 '''
89 '''
84
90
@@ -86,21 +92,30 b' class RMagics(Magics):'
86 for input in inputs:
92 for input in inputs:
87 self.r.assign(input, self.pyconverter(self.shell.user_ns[input]))
93 self.r.assign(input, self.pyconverter(self.shell.user_ns[input]))
88
94
95 @skip_doctest
89 @line_magic
96 @line_magic
90 def Rpull(self, line):
97 def Rpull(self, line):
91 '''
98 '''
92 A line-level magic for R that pulls
99 A line-level magic for R that pulls
93 variables from python to rpy2.
100 variables from python to rpy2::
101
102 In [18]: _ = %R x = c(3,4,6.7); y = c(4,6,7); z = c('a',3,4)
103
104 In [19]: %Rp
105 %Rpull %Rpush
106
107 In [19]: %Rpull x y z
94
108
95 Parameters
109 In [20]: x
96 ----------
110 Out[20]: array([ 3. , 4. , 6.7])
97
111
98 line: output
112 In [21]: y
113 Out[21]: array([ 4., 6., 7.])
99
114
100 A white space separated string of
115 In [22]: z
101 names of objects in the R name space to be
116 Out[22]:
102 assigned to objects of the same name in the
117 array(['a', '3', '4'],
103 python name space.
118 dtype='|S1')
104
119
105 Notes
120 Notes
106 -----
121 -----
@@ -114,6 +129,7 b' class RMagics(Magics):'
114 self.shell.push({output:self.Rconverter(self.r(output))})
129 self.shell.push({output:self.Rconverter(self.r(output))})
115
130
116
131
132 @skip_doctest
117 @magic_arguments()
133 @magic_arguments()
118 @argument(
134 @argument(
119 '-i', '--input', action='append',
135 '-i', '--input', action='append',
@@ -155,33 +171,33 b' class RMagics(Magics):'
155
171
156 In line mode, this will evaluate an expression and convert the returned value to a Python object.
172 In line mode, this will evaluate an expression and convert the returned value to a Python object.
157 The return value is determined by rpy2's behaviour of returning the result of evaluating the
173 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.
174 final line. Multiple R lines can be executed by joining them with semicolons::
159
175
160 In [9]: %R X=c(1,4,5,7); sd(X); mean(X)
176 In [9]: %R X=c(1,4,5,7); sd(X); mean(X)
161 Out[9]: array([ 4.25])
177 Out[9]: array([ 4.25])
162
178
163 As a cell, this will run a block of R code, without bringing anything back by default::
179 As a cell, this will run a block of R code, without bringing anything back by default::
164
180
165 In [10]: %%R
181 In [10]: %%R
166 ....: Y = c(2,4,3,9)
182 ....: Y = c(2,4,3,9)
167 ....: print(summary(lm(Y~X)))
183 ....: print(summary(lm(Y~X)))
168 ....:
184 ....:
169
185
170 Call:
186 Call:
171 lm(formula = Y ~ X)
187 lm(formula = Y ~ X)
172
188
173 Residuals:
189 Residuals:
174 1 2 3 4
190 1 2 3 4
175 0.88 -0.24 -2.28 1.64
191 0.88 -0.24 -2.28 1.64
176
192
177 Coefficients:
193 Coefficients:
178 Estimate Std. Error t value Pr(>|t|)
194 Estimate Std. Error t value Pr(>|t|)
179 (Intercept) 0.0800 2.3000 0.035 0.975
195 (Intercept) 0.0800 2.3000 0.035 0.975
180 X 1.0400 0.4822 2.157 0.164
196 X 1.0400 0.4822 2.157 0.164
181
197
182 Residual standard error: 2.088 on 2 degrees of freedom
198 Residual standard error: 2.088 on 2 degrees of freedom
183 Multiple R-squared: 0.6993,Adjusted R-squared: 0.549
199 Multiple R-squared: 0.6993,Adjusted R-squared: 0.549
184 F-statistic: 4.651 on 1 and 2 DF, p-value: 0.1638
200 F-statistic: 4.651 on 1 and 2 DF, p-value: 0.1638
185
201
186 In the notebook, plots are published as the output of the cell.
202 In the notebook, plots are published as the output of the cell.
187
203
@@ -192,20 +208,19 b' class RMagics(Magics):'
192 If cell is not None and line has some R code, it is prepended to
208 If cell is not None and line has some R code, it is prepended to
193 the R code in cell.
209 the R code in cell.
194
210
195 Objects can be passed back and forth between rpy2 and python via the -i -o flags in line
211 Objects can be passed back and forth between rpy2 and python via the -i -o flags in line::
196
212
213 In [14]: Z = np.array([1,4,5,10])
197
214
198 In [14]: Z = np.array([1,4,5,10])
215 In [15]: %R -i Z mean(Z)
216 Out[15]: array([ 5.])
199
217
200 In [15]: %R -i Z mean(Z)
201 Out[15]: array([ 5.])
202
218
219 In [16]: %R -o W W=Z*mean(Z)
220 Out[16]: array([ 5., 20., 25., 50.])
203
221
204 In [16]: %R -o W W=Z*mean(Z)
222 In [17]: W
205 Out[16]: array([ 5., 20., 25., 50.])
223 Out[17]: array([ 5., 20., 25., 50.])
206
207 In [17]: W
208 Out[17]: array([ 5., 20., 25., 50.])
209
224
210 If the cell is None, the resulting value is returned,
225 If the cell is None, the resulting value is returned,
211 after conversion with self.Rconverter
226 after conversion with self.Rconverter
@@ -271,6 +286,9 b' class RMagics(Magics):'
271 )
286 )
272 value = {}
287 value = {}
273
288
289 # kill the temporary directory
290 rmtree(tmpd)
291
274 # try to turn every output into a numpy array
292 # try to turn every output into a numpy array
275 # this means that output are assumed to be castable
293 # this means that output are assumed to be castable
276 # as numpy arrays
294 # as numpy arrays
@@ -280,8 +298,6 b' class RMagics(Magics):'
280 # with self.shell, we assign the values to variables in the shell
298 # with self.shell, we assign the values to variables in the shell
281 self.shell.push({output:self.Rconverter(self.r(output))})
299 self.shell.push({output:self.Rconverter(self.r(output))})
282
300
283 # kill the temporary directory
284 rmtree(tmpd)
285
301
286 # if there was a single line, return its value
302 # if there was a single line, return its value
287 # converted to a python object
303 # converted to a python object
General Comments 0
You need to be logged in to leave comments. Login now