Rscript 子进程 Python

1 投票
1 回答
722 浏览
提问于 2025-04-17 22:30

通过谷歌搜索,我发现调用R代码的方法是使用以下内容:

subprocess.call("Rscript" + " /path/Rscript.R")

# The reason there is an add statment is because in actual code I have a 
variable of where the script is. 

在我的个人电脑上,这段代码可以正常运行。现在我在一台服务器上工作。如果我在代码所在的目录中运行

Rscript /path/Rscript.R

它可以正常工作。

但是当我尝试从Python代码中运行时,却出现“没有这样的文件或目录”的错误。我已经确认Rscript在我的路径中(因为我可以从命令行运行它)。

任何帮助都将不胜感激。

我尝试过从 ~/路径 到它,./路径 到它,/绝对路径 到它。

1 个回答

2

这一行的末尾多了一个不必要的括号:

subprocess.call("Rscript" + "/path/Rscript.R"))
                                              )

另外,你需要在命令和参数之间加个空格。否则,Rscript/path/Rscript.R 会被当作一个命令来识别。

subprocess.call("Rscript" + " " + "/path/Rscript.R")
                            ^^^

或者你可以传递一个列表:

subprocess.call(["Rscript", "/path/Rscript.R"])

撰写回答