import numpy as np from IPython.core.interactiveshell import InteractiveShell from IPython.extensions import rmagic import nose.tools as nt ip = get_ipython() ip.magic('load_ext rmagic') def test_push(): rm = rmagic.RMagics(ip) ip.push({'X':np.arange(5), 'Y':np.array([3,5,4,6,7])}) ip.run_line_magic('Rpush', 'X Y') 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']) 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(): rm = rmagic.RMagics(ip) rm.r('Z=c(11:20)') ip.run_line_magic('Rpull', 'Z') np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z']) np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21)) def test_Rconverter(): datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')], dtype=[('x', '