Show More
@@ -184,20 +184,28 b' class RMagics(Magics):' | |||||
184 |
|
184 | |||
185 | def eval(self, line): |
|
185 | def eval(self, line): | |
186 | ''' |
|
186 | ''' | |
187 | Parse and evaluate a line with rpy2. |
|
187 | Parse and evaluate a line of R code with rpy2. | |
188 | Returns the output to R's stdout() connection |
|
188 | Returns the output to R's stdout() connection, | |
189 | and the value of eval(parse(line)). |
|
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 | old_writeconsole = ri.get_writeconsole() |
|
197 | old_writeconsole = ri.get_writeconsole() | |
192 | ri.set_writeconsole(self.write_console) |
|
198 | ri.set_writeconsole(self.write_console) | |
193 | try: |
|
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 | except (ri.RRuntimeError, ValueError) as exception: |
|
203 | except (ri.RRuntimeError, ValueError) as exception: | |
196 | warning_or_other_msg = self.flush() # otherwise next return seems to have copy of error |
|
204 | 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) |
|
205 | raise RInterpreterError(line, str_to_unicode(str(exception)), warning_or_other_msg) | |
198 | text_output = self.flush() |
|
206 | text_output = self.flush() | |
199 | ri.set_writeconsole(old_writeconsole) |
|
207 | ri.set_writeconsole(old_writeconsole) | |
200 | return text_output, value |
|
208 | return text_output, value, visible | |
201 |
|
209 | |||
202 | def write_console(self, output): |
|
210 | def write_console(self, output): | |
203 | ''' |
|
211 | ''' | |
@@ -413,13 +421,16 b' class RMagics(Magics):' | |||||
413 | In [9]: %R X=c(1,4,5,7); sd(X); mean(X) |
|
421 | In [9]: %R X=c(1,4,5,7); sd(X); mean(X) | |
414 | Out[9]: array([ 4.25]) |
|
422 | Out[9]: array([ 4.25]) | |
415 |
|
423 | |||
416 |
|
|
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 | In [10]: %%R |
|
430 | In [10]: %%R | |
419 | ....: Y = c(2,4,3,9) |
|
431 | ....: Y = c(2,4,3,9) | |
420 |
....: |
|
432 | ....: summary(lm(Y~X)) | |
421 | ....: |
|
433 | ||
422 |
|
||||
423 | Call: |
|
434 | Call: | |
424 | lm(formula = Y ~ X) |
|
435 | lm(formula = Y ~ X) | |
425 |
|
436 | |||
@@ -580,14 +591,20 b' class RMagics(Magics):' | |||||
580 | try: |
|
591 | try: | |
581 | if line_mode: |
|
592 | if line_mode: | |
582 | for line in code.split(';'): |
|
593 | for line in code.split(';'): | |
583 | text_result, result = self.eval(line) |
|
594 | text_result, result, visible = self.eval(line) | |
584 | text_output += text_result |
|
595 | text_output += text_result | |
585 | if text_result: |
|
596 | if text_result: | |
586 | # the last line printed something to the console so we won't return it |
|
597 | # the last line printed something to the console so we won't return it | |
587 | return_output = False |
|
598 | return_output = False | |
588 | else: |
|
599 | else: | |
589 | text_result, result = self.eval(code) |
|
600 | text_result, result, visible = self.eval(code) | |
590 | text_output += text_result |
|
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 | except RInterpreterError as e: |
|
609 | except RInterpreterError as e: | |
593 | print(e.stdout) |
|
610 | print(e.stdout) |
General Comments 0
You need to be logged in to leave comments.
Login now