【问题标题】:rpython alternative for calling python from R?从 R 调用 python 的 rpython 替代方案?
【发布时间】:2017-03-02 18:48:04
【问题描述】:

我正在尝试使用rpython 从 R 调用 python。效果很好。现在我正在尝试将我的 R 代码安装到具有 4 个不同 python 的 docker 容器中。原来configure script 允许你指定 python 版本(RPYTHON_PYTHON_VERSION),但不能指定 Python 的路径。

那么,是否有替代的 R 包可以从 R 调用 Python?

【问题讨论】:

  • @hrbrmstr 这是一个相当蹩脚的被动攻击性反应,但如果您将其作为答案提交,我会接受。特别是我试过reticulate,感觉还不错。

标签: python r


【解决方案1】:

您可以使用 R 的 system2 函数将其作为 子进程 执行,以执行和捕获输出。

从 R 执行 Python 脚本的示例:

# run_splitstr.R
command = "python"

# Note the single + double quotes in the string (needed if paths have spaces)
path2script='"path/to your script/splitstr.py"'

# Build up args in a vector
string = "3523462---12413415---4577678---7967956---5456439"
pattern = "---"
args = c(string, pattern)

# Add path to script as first arg
allArgs = c(path2script, args)

output = system2(command, args=allArgs, stdout=TRUE)

print(paste("The Substrings are:\n", output))

要在字符向量中捕获标准输出(每个元素一行),必须在 system2 中指定 stdout=TRUE,否则只返回退出状态。当 stdout=TRUE 时,退出状态存储在名为“status”的属性中。

【讨论】:

  • 谢谢,这是合理的。我想调用一个 Python 函数,而不是一个进程,但也许我必须缩减和更改。
【解决方案2】:

reticulatebasilisk。我相信只有后者,虽然不太成熟,但可以同时使用多个 python 版本/环境。

【讨论】:

    猜你喜欢
    • 2011-11-18
    • 2017-10-06
    • 2023-03-28
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多