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