##// END OF EJS Templates
fixed Rconverter a little to catch return values from %R
Jonathan Taylor -
Show More
@@ -59,12 +59,31 b' from IPython.utils.py3compat import str_to_unicode, unicode_to_str'
59 class RMagicError(ri.RRuntimeError):
59 class RMagicError(ri.RRuntimeError):
60 pass
60 pass
61
61
62 def Rconverter(Robj):
63 """
64 Convert an object in R's namespace to one suitable
65 for ipython's namespace.
66
67 For a data.frame, it tries to return a structured array.
68
69 Parameters
70 ----------
71
72 Robj: an R object returned from rpy2
73 """
74 if is_data_frame(Robj):
75 names = np.array(Robj.do_slot('names'))
76 Robj = np.rec.fromarrays(Robj, names = tuple(names))
77 return np.asarray(Robj)
78
79 is_data_frame = None
80
62 @magics_class
81 @magics_class
63 class RMagics(Magics):
82 class RMagics(Magics):
64 """A set of magics useful for interactive work with R via rpy2.
83 """A set of magics useful for interactive work with R via rpy2.
65 """
84 """
66
85
67 def __init__(self, shell, Rconverter=np.asarray,
86 def __init__(self, shell, Rconverter=Rconverter,
68 pyconverter=np.asarray,
87 pyconverter=np.asarray,
69 cache_display_data=False):
88 cache_display_data=False):
70 """
89 """
@@ -73,10 +92,6 b' class RMagics(Magics):'
73
92
74 shell : IPython shell
93 shell : IPython shell
75
94
76 Rconverter : callable
77 To be called on return values from R before returning
78 to ipython.
79
80 pyconverter : callable
95 pyconverter : callable
81 To be called on values in ipython namespace before
96 To be called on values in ipython namespace before
82 assigning to variables in rpy2.
97 assigning to variables in rpy2.
@@ -90,9 +105,12 b' class RMagics(Magics):'
90 self.cache_display_data = cache_display_data
105 self.cache_display_data = cache_display_data
91
106
92 self.r = ro.R()
107 self.r = ro.R()
108 global is_data_frame
109 is_data_frame = self.r('is.data.frame')
110
93 self.Rstdout_cache = []
111 self.Rstdout_cache = []
94 self.Rconverter = Rconverter
95 self.pyconverter = pyconverter
112 self.pyconverter = pyconverter
113 self.Rconverter = Rconverter
96
114
97 def eval(self, line):
115 def eval(self, line):
98 '''
116 '''
@@ -184,11 +202,7 b' class RMagics(Magics):'
184 '''
202 '''
185 outputs = line.split(' ')
203 outputs = line.split(' ')
186 for output in outputs:
204 for output in outputs:
187 if self.r("is.data.frame({0})".format(output))[0]:
205 self.shell.push({output:self.Rconverter(self.r(output))})
188 o = np.rec.fromarrays(self.r(output), names = tuple(self.r(output).names))
189 self.shell.push({output:self.Rconverter(o)})
190 else:
191 self.shell.push({output:self.Rconverter(self.r(output))})
192
206
193
207
194 @skip_doctest
208 @skip_doctest
@@ -375,11 +389,7 b' class RMagics(Magics):'
375
389
376 if args.output:
390 if args.output:
377 for output in ','.join(args.output).split(','):
391 for output in ','.join(args.output).split(','):
378 if self.r("is.data.frame({0})".format(output))[0]:
392 self.shell.push({output:self.Rconverter(self.r(output))})
379 o = np.rec.fromarrays(self.r(output), names = tuple(self.r(output).names))
380 self.shell.push({output:self.Rconverter(o)})
381 else:
382 self.shell.push({output:self.Rconverter(self.r(output))})
383
393
384 for tag, disp_d in display_data:
394 for tag, disp_d in display_data:
385 publish_display_data(tag, disp_d)
395 publish_display_data(tag, disp_d)
General Comments 0
You need to be logged in to leave comments. Login now