Show More
@@ -72,11 +72,19 b' def Rconverter(Robj):' | |||||
72 | Robj: an R object returned from rpy2 |
|
72 | Robj: an R object returned from rpy2 | |
73 | """ |
|
73 | """ | |
74 | if is_data_frame(Robj): |
|
74 | if is_data_frame(Robj): | |
75 | names = np.array(Robj.do_slot('names')) |
|
75 | dimRobj = dimR(Robj) | |
76 | Robj = np.rec.fromarrays(Robj, names = tuple(names)) |
|
76 | if dimRobj != ri.NULL: | |
|
77 | dimRobj = list(np.array(dimRobj)) | |||
|
78 | if len(dimRobj) > 1: | |||
|
79 | try: | |||
|
80 | names = np.array(Robj.do_slot('names')) | |||
|
81 | Robj = np.rec.fromarrays(Robj, names = tuple(names)) | |||
|
82 | except LookupError: | |||
|
83 | pass | |||
77 | return np.asarray(Robj) |
|
84 | return np.asarray(Robj) | |
78 |
|
85 | |||
79 | is_data_frame = None |
|
86 | is_data_frame = None | |
|
87 | dimR = None | |||
80 |
|
88 | |||
81 | @magics_class |
|
89 | @magics_class | |
82 | class RMagics(Magics): |
|
90 | class RMagics(Magics): | |
@@ -105,8 +113,9 b' class RMagics(Magics):' | |||||
105 | self.cache_display_data = cache_display_data |
|
113 | self.cache_display_data = cache_display_data | |
106 |
|
114 | |||
107 | self.r = ro.R() |
|
115 | self.r = ro.R() | |
108 | global is_data_frame |
|
116 | global is_data_frame, dimR | |
109 | is_data_frame = self.r('is.data.frame') |
|
117 | is_data_frame = self.r('is.data.frame') | |
|
118 | dimR = self.r('dim') | |||
110 |
|
119 | |||
111 | self.Rstdout_cache = [] |
|
120 | self.Rstdout_cache = [] | |
112 | self.pyconverter = pyconverter |
|
121 | self.pyconverter = pyconverter |
@@ -1,6 +1,7 b'' | |||||
1 | import numpy as np |
|
1 | import numpy as np | |
2 | from IPython.core.interactiveshell import InteractiveShell |
|
2 | from IPython.core.interactiveshell import InteractiveShell | |
3 | from IPython.extensions import rmagic |
|
3 | from IPython.extensions import rmagic | |
|
4 | import nose.tools as nt | |||
4 |
|
5 | |||
5 | ip = get_ipython() |
|
6 | ip = get_ipython() | |
6 | ip.magic('load_ext rmagic') |
|
7 | ip.magic('load_ext rmagic') | |
@@ -20,19 +21,39 b' def test_pull():' | |||||
20 | np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z']) |
|
21 | np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z']) | |
21 | np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21)) |
|
22 | np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21)) | |
22 |
|
23 | |||
23 |
|
|
24 | def test_Rconverter(): | |
24 | # rm = rmagic.RMagics(ip) |
|
25 | datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')], | |
25 | # c = ip.run_line_magic('Rinline', 'lm(Y~X)$coef') |
|
26 | dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|S1')]) | |
26 | # np.testing.assert_almost_equal(c, [3.2, 0.9]) |
|
27 | ip.user_ns['datapy'] = datapy | |
|
28 | ip.run_line_magic('Rpush', 'datapy') | |||
|
29 | ||||
|
30 | # test to see if a copy is being made | |||
|
31 | v = ip.run_line_magic('R', 'datapy') | |||
|
32 | w = ip.run_line_magic('R', 'datapy') | |||
|
33 | np.testing.assert_almost_equal(w['x'], v['x']) | |||
|
34 | np.testing.assert_almost_equal(w['y'], v['y']) | |||
|
35 | nt.assert_true(np.all(w['z'] == v['z'])) | |||
|
36 | np.testing.assert_equal(id(w.data), id(v.data)) | |||
|
37 | nt.assert_equal(w.dtype, v.dtype) | |||
|
38 | ||||
|
39 | ip.run_cell_magic('R', ' -o datar datar=datapy', '') | |||
|
40 | ||||
|
41 | u = ip.run_line_magic('R', 'datar') | |||
|
42 | np.testing.assert_almost_equal(u['x'], v['x']) | |||
|
43 | np.testing.assert_almost_equal(u['y'], v['y']) | |||
|
44 | nt.assert_true(np.all(u['z'] == v['z'])) | |||
|
45 | np.testing.assert_equal(id(u.data), id(v.data)) | |||
|
46 | nt.assert_equal(u.dtype, v.dtype) | |||
|
47 | ||||
27 |
|
48 | |||
28 | def test_cell_magic(): |
|
49 | def test_cell_magic(): | |
29 |
|
50 | |||
30 | ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])}) |
|
51 | ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])}) | |
31 | snippet = ''' |
|
52 | snippet = ''' | |
32 | print(summary(a)) |
|
53 | print(summary(a)) | |
33 |
plot( |
|
54 | plot(x, y, pch=23, bg='orange', cex=2) | |
34 |
plot( |
|
55 | plot(x, x) | |
35 |
print(summary( |
|
56 | print(summary(x)) | |
36 | r = resid(a) |
|
57 | r = resid(a) | |
37 | xc = coef(a) |
|
58 | xc = coef(a) | |
38 | ''' |
|
59 | ''' |
General Comments 0
You need to be logged in to leave comments.
Login now