##// END OF EJS Templates
Add REPL-like printing to %%R cell magic
gmbecker -
Show More
@@ -182,19 +182,42 b' class RMagics(Magics):'
182 self.pyconverter = pyconverter
182 self.pyconverter = pyconverter
183 self.Rconverter = Rconverter
183 self.Rconverter = Rconverter
184
184
185 def eval(self, line):
185 def eval(self, line, line_mode):
186 '''
186 '''
187 Parse and evaluate a line with rpy2.
187 Parse and evaluate a line with rpy2.
188 Returns the output to R's stdout() connection
188 Returns the output to R's stdout() connection, and
189 and the value of eval(parse(line)).
189 the value generated by evaluating the code (see below)
190
191 In line mode (ie called with %R <stuff>), behavior
192 is unchanged, returns a python object representing
193 the final value, suitable for assignment into a
194 python variable.
195
196 In cell mode (called with %%R ...), behavior
197 now reproduces the REPL behavior of the R
198 interpreter (which agrees with how cells of
199 python code are handled by the notebook).
200
201 In cell mode if the last line of code is not
202 an assignment, wrapped in invisible(), or
203 a call to a function which returns its value
204 invisibly, the value will be printed using the
205 show R function.
206
207 Actual evaluation of R code is done via a call
208 of the form withVisible({<code>})
209
190 '''
210 '''
191 old_writeconsole = ri.get_writeconsole()
211 old_writeconsole = ri.get_writeconsole()
192 ri.set_writeconsole(self.write_console)
212 ri.set_writeconsole(self.write_console)
193 try:
213 try:
194 value = ri.baseenv['eval'](ri.parse(line))
214 res = ro.r("withVisible({%s})" % line)
215 value = res[0] #value
195 except (ri.RRuntimeError, ValueError) as exception:
216 except (ri.RRuntimeError, ValueError) as exception:
196 warning_or_other_msg = self.flush() # otherwise next return seems to have copy of error
217 warning_or_other_msg = self.flush() # otherwise next return seems to have copy of error
197 raise RInterpreterError(line, str_to_unicode(str(exception)), warning_or_other_msg)
218 raise RInterpreterError(line, str_to_unicode(str(exception)), warning_or_other_msg)
219 if not line_mode and ro.conversion.ri2py(res[1])[0]:
220 ro.r.show(value)
198 text_output = self.flush()
221 text_output = self.flush()
199 ri.set_writeconsole(old_writeconsole)
222 ri.set_writeconsole(old_writeconsole)
200 return text_output, value
223 return text_output, value
@@ -580,13 +603,13 b' class RMagics(Magics):'
580 try:
603 try:
581 if line_mode:
604 if line_mode:
582 for line in code.split(';'):
605 for line in code.split(';'):
583 text_result, result = self.eval(line)
606 text_result, result = self.eval(line, line_mode)
584 text_output += text_result
607 text_output += text_result
585 if text_result:
608 if text_result:
586 # the last line printed something to the console so we won't return it
609 # the last line printed something to the console so we won't return it
587 return_output = False
610 return_output = False
588 else:
611 else:
589 text_result, result = self.eval(code)
612 text_result, result = self.eval(code, line_mode)
590 text_output += text_result
613 text_output += text_result
591
614
592 except RInterpreterError as e:
615 except RInterpreterError as e:
General Comments 0
You need to be logged in to leave comments. Login now