关于 r:Reticulate – 在不分配给变量的情况下获取 Python 结果
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 上打开了一个问题。如果那里没有更新,我会接受你的回答,好吗?
- 是的,没关系。仅供参考,我认为在 github 问题上有一种方法可以将它们标记为功能请求
- 我做了一个编辑。 (不小心对你的回答也很抱歉)。
- 不确定您是否在 Github 上收到通知。 Kevin Ushey 给出了答案。
我在 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。