diff --git a/IPython/extensions/rmagic.py b/IPython/extensions/rmagic.py index 5e1a72c..82cd141 100644 --- a/IPython/extensions/rmagic.py +++ b/IPython/extensions/rmagic.py @@ -72,11 +72,19 @@ def Rconverter(Robj): Robj: an R object returned from rpy2 """ if is_data_frame(Robj): - names = np.array(Robj.do_slot('names')) - Robj = np.rec.fromarrays(Robj, names = tuple(names)) + dimRobj = dimR(Robj) + if dimRobj != ri.NULL: + dimRobj = list(np.array(dimRobj)) + if len(dimRobj) > 1: + try: + names = np.array(Robj.do_slot('names')) + Robj = np.rec.fromarrays(Robj, names = tuple(names)) + except LookupError: + pass return np.asarray(Robj) is_data_frame = None +dimR = None @magics_class class RMagics(Magics): @@ -105,8 +113,9 @@ class RMagics(Magics): self.cache_display_data = cache_display_data self.r = ro.R() - global is_data_frame + global is_data_frame, dimR is_data_frame = self.r('is.data.frame') + dimR = self.r('dim') self.Rstdout_cache = [] self.pyconverter = pyconverter diff --git a/IPython/extensions/tests/test_rmagic.py b/IPython/extensions/tests/test_rmagic.py index 7d726eb..1584542 100644 --- a/IPython/extensions/tests/test_rmagic.py +++ b/IPython/extensions/tests/test_rmagic.py @@ -1,6 +1,7 @@ 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') @@ -20,19 +21,39 @@ def test_pull(): 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_inline(): -# rm = rmagic.RMagics(ip) -# c = ip.run_line_magic('Rinline', 'lm(Y~X)$coef') -# np.testing.assert_almost_equal(c, [3.2, 0.9]) +def test_Rconverter(): + datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')], + dtype=[('x', '