##// END OF EJS Templates
Merge pull request #4017 from gmbecker/PR_rmagic2...
Thomas Kluyver -
r12183:681735e3 merge
parent child Browse files
Show More
@@ -184,20 +184,28 b' class RMagics(Magics):'
184 184
185 185 def eval(self, line):
186 186 '''
187 Parse and evaluate a line with rpy2.
188 Returns the output to R's stdout() connection
189 and the value of eval(parse(line)).
187 Parse and evaluate a line of R code with rpy2.
188 Returns the output to R's stdout() connection,
189 the value generated by evaluating the code, and a
190 boolean indicating whether the return value would be
191 visible if the line of code were evaluated in an R REPL.
192
193 R Code evaluation and visibility determination are
194 done via an R call of the form withVisible({<code>})
195
190 196 '''
191 197 old_writeconsole = ri.get_writeconsole()
192 198 ri.set_writeconsole(self.write_console)
193 199 try:
194 value = ri.baseenv['eval'](ri.parse(line))
200 res = ro.r("withVisible({%s})" % line)
201 value = res[0] #value (R object)
202 visible = ro.conversion.ri2py(res[1])[0] #visible (boolean)
195 203 except (ri.RRuntimeError, ValueError) as exception:
196 204 warning_or_other_msg = self.flush() # otherwise next return seems to have copy of error
197 205 raise RInterpreterError(line, str_to_unicode(str(exception)), warning_or_other_msg)
198 206 text_output = self.flush()
199 207 ri.set_writeconsole(old_writeconsole)
200 return text_output, value
208 return text_output, value, visible
201 209
202 210 def write_console(self, output):
203 211 '''
@@ -413,13 +421,16 b' class RMagics(Magics):'
413 421 In [9]: %R X=c(1,4,5,7); sd(X); mean(X)
414 422 Out[9]: array([ 4.25])
415 423
416 As a cell, this will run a block of R code, without bringing anything back by default::
424 In cell mode, this will run a block of R code. The resulting value
425 is printed if it would printed be when evaluating the same code
426 within a standard R REPL.
427
428 Nothing is returned to python by default in cell mode.
417 429
418 430 In [10]: %%R
419 431 ....: Y = c(2,4,3,9)
420 ....: print(summary(lm(Y~X)))
421 ....:
422
432 ....: summary(lm(Y~X))
433
423 434 Call:
424 435 lm(formula = Y ~ X)
425 436
@@ -580,14 +591,20 b' class RMagics(Magics):'
580 591 try:
581 592 if line_mode:
582 593 for line in code.split(';'):
583 text_result, result = self.eval(line)
594 text_result, result, visible = self.eval(line)
584 595 text_output += text_result
585 596 if text_result:
586 597 # the last line printed something to the console so we won't return it
587 598 return_output = False
588 599 else:
589 text_result, result = self.eval(code)
600 text_result, result, visible = self.eval(code)
590 601 text_output += text_result
602 if visible:
603 old_writeconsole = ri.get_writeconsole()
604 ri.set_writeconsole(self.write_console)
605 ro.r.show(result)
606 text_output += self.flush()
607 ri.set_writeconsole(old_writeconsole)
591 608
592 609 except RInterpreterError as e:
593 610 print(e.stdout)
General Comments 0
You need to be logged in to leave comments. Login now