Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

%Rpush: Look for variables in the local scope first. #2731

Merged
merged 2 commits into from Jan 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions IPython/extensions/rmagic.py
Expand Up @@ -178,8 +178,9 @@ def flush(self):
return value return value


@skip_doctest @skip_doctest
@needs_local_scope
@line_magic @line_magic
def Rpush(self, line): def Rpush(self, line, local_ns=None):
''' '''
A line-level magic for R that pushes A line-level magic for R that pushes
variables from python to rpy2. The line should be made up variables from python to rpy2. The line should be made up
Expand All @@ -199,10 +200,16 @@ def Rpush(self, line):
Out[11]: array([ 6.23333333]) Out[11]: array([ 6.23333333])


''' '''
if local_ns is None:
local_ns = {}


inputs = line.split(' ') inputs = line.split(' ')
for input in inputs: for input in inputs:
self.r.assign(input, self.pyconverter(self.shell.user_ns[input])) try:
val = local_ns[input]
except KeyError:
val = self.shell.user_ns[input]
self.r.assign(input, self.pyconverter(val))


@skip_doctest @skip_doctest
@magic_arguments() @magic_arguments()
Expand Down
14 changes: 14 additions & 0 deletions IPython/extensions/tests/test_rmagic.py
Expand Up @@ -14,6 +14,20 @@ def test_push():
np.testing.assert_almost_equal(np.asarray(rm.r('X')), ip.user_ns['X']) np.testing.assert_almost_equal(np.asarray(rm.r('X')), ip.user_ns['X'])
np.testing.assert_almost_equal(np.asarray(rm.r('Y')), ip.user_ns['Y']) np.testing.assert_almost_equal(np.asarray(rm.r('Y')), ip.user_ns['Y'])


def test_push_localscope():
"""Test that Rpush looks for variables in the local scope first."""
ip.run_cell('''
def rmagic_addone(u):
%Rpush u
%R result = u+1
%Rpull result
return result[0]
u = 0
result = rmagic_addone(12344)
''')
result = ip.user_ns['result']
np.testing.assert_equal(result, 12345)

def test_pull(): def test_pull():
rm = rmagic.RMagics(ip) rm = rmagic.RMagics(ip)
rm.r('Z=c(11:20)') rm.r('Z=c(11:20)')
Expand Down