Show More
@@ -178,8 +178,9 b' class RMagics(Magics):' | |||
|
178 | 178 | return value |
|
179 | 179 | |
|
180 | 180 | @skip_doctest |
|
181 | @needs_local_scope | |
|
181 | 182 | @line_magic |
|
182 | def Rpush(self, line): | |
|
183 | def Rpush(self, line, local_ns=None): | |
|
183 | 184 | ''' |
|
184 | 185 | A line-level magic for R that pushes |
|
185 | 186 | variables from python to rpy2. The line should be made up |
@@ -199,10 +200,16 b' class RMagics(Magics):' | |||
|
199 | 200 | Out[11]: array([ 6.23333333]) |
|
200 | 201 | |
|
201 | 202 | ''' |
|
203 | if local_ns is None: | |
|
204 | local_ns = {} | |
|
202 | 205 | |
|
203 | 206 | inputs = line.split(' ') |
|
204 | 207 | for input in inputs: |
|
205 | self.r.assign(input, self.pyconverter(self.shell.user_ns[input])) | |
|
208 | try: | |
|
209 | val = local_ns[input] | |
|
210 | except KeyError: | |
|
211 | val = self.shell.user_ns[input] | |
|
212 | self.r.assign(input, self.pyconverter(val)) | |
|
206 | 213 | |
|
207 | 214 | @skip_doctest |
|
208 | 215 | @magic_arguments() |
@@ -14,6 +14,20 b' def test_push():' | |||
|
14 | 14 | np.testing.assert_almost_equal(np.asarray(rm.r('X')), ip.user_ns['X']) |
|
15 | 15 | np.testing.assert_almost_equal(np.asarray(rm.r('Y')), ip.user_ns['Y']) |
|
16 | 16 | |
|
17 | def test_push_localscope(): | |
|
18 | """Test that Rpush looks for variables in the local scope first.""" | |
|
19 | ip.run_cell(''' | |
|
20 | def rmagic_addone(u): | |
|
21 | %Rpush u | |
|
22 | %R result = u+1 | |
|
23 | %Rpull result | |
|
24 | return result[0] | |
|
25 | u = 0 | |
|
26 | result = rmagic_addone(12344) | |
|
27 | ''') | |
|
28 | result = ip.user_ns['result'] | |
|
29 | np.testing.assert_equal(result, 12345) | |
|
30 | ||
|
17 | 31 | def test_pull(): |
|
18 | 32 | rm = rmagic.RMagics(ip) |
|
19 | 33 | rm.r('Z=c(11:20)') |
General Comments 0
You need to be logged in to leave comments.
Login now