Reticulate – Get Python result without assigning to variable
如果可能的话,我想在 R 中打印 python 代码的结果(不分配给变量)。
这行得通:
| 1 2 3 | library(reticualte) py_run_string("print(2)") 2 | 
这行得通:
| 1 2 3 | p = py_run_string("x = 2") p$x 2 | 
我希望这个工作:
| 1 2 | py_run_string("2") 2 | 
背景:
即使不使用 (print),我也想阅读完整的 python 代码并捕获输出。
如果我打开 Python3.7 Shell 并仅执行”2″作为命令,输出将为”2″。这里是空的。
Github 请求链接:https://github.com/rstudio/reticulate/issues/595。
在尝试过这个之后,我会选择 
试过了:
| 1 2 3 | return( py_run_string("2") ) | 
试过了:
| 1 2 3 4 5 6 7 | f <– function() { return( py_run_string("2") ) } f() | 
std out 中似乎没有任何内容
对比:
| 1 2 3 4 5 6 7 | b <– function() { return(2) } b() # Out[]: 2 | 
我猜它正在访问 python 的 
还有:
| 1 2 3 4 5 6 7 8 9 10 | library(reticulate)
 py_run_string("2") a <– 3 | 
R 的局部变量中没有任何内容代表 
Github 请求链接:https://github.com/rstudio/reticulate/issues/595。
我在 Github 上从 Kevin Ushey 那里得到了答案。
| 1 2 3 4 5 6 7 8 9 10 11 | library(reticulate)
 py_evaluate <– function(code) { py_evaluate("2") | 
见这里:https://github.com/rstudio/reticulate/issues/595#issuecomment-531888843。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/268007.html
