【发布时间】:2021-10-27 19:00:29
【问题描述】:
我用 python 编写代码,我的同事用 R 编写代码。我正在尝试调用他编写的 Rscript 文件以将其合并到 python 应用程序中。但是,当使用与 python 子进程库 [subprocess.run() /subprocess.call()/ subprocess.Popen()/ subprocess.check_output()] 完全相同的命令时,我可以使用窗口的 powershell 成功调用 Rscript . 在 power shell 中工作的命令是:
Rscript C:/Users/file/path/script_name.R
Rscript 是我的 PATH 环境中的一个变量,它引用了我的 Rscript.exe 文件路径。我试过用它代替完整的文件路径,但这不会改变结果。 在我的 python 代码中,子进程调用如下所示:
subprocess.check_output('Rscript C:/Users/file/path/script_name.R, shell=True)
运行此代码会在我的 python IDE 中显示以下错误消息。我发现这很不寻常,原因有三个。第一个是程序声称多次停止执行但继续执行并多次遇到相同的错误。其次是包'ggplot2'和'scales'肯定是导入的,因为代码在Rstudio和powershell中都可以工作而不会触发这个错误。还有一行提到“rstudioapi”有问题,但是这个包没有出现在文件中的任何地方,而且我没有使用 Rstudio。我正在寻找一种修复子进程调用的方法,或者一种从python打开powershell窗口并将工作命令输入终端窗口的方法。我对此感到困惑,我认为我不了解子流程功能。
Error in loadNamespace(name) : there is no package called 'rstudioapi'
Calls: ReqFunc ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Fatal error: cannot open file 'script_name.R': No such file or directory
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror
Calls: install.packages -> contrib.url
Execution halted
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror
Calls: install.packages -> contrib.url
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(scales) : there is no package called 'scales'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(scales) : there is no package called 'scales'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(scales) : there is no package called 'scales'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
Error in library(ggplot2) : there is no package called 'ggplot2'
Calls: ReqFunc -> source -> withVisible -> eval -> eval -> library
Execution halted
ReqFunc 是脚本中函数的名称
【问题讨论】:
-
您可能在不同的环境或版本中运行 Python 或 R。在可以运行 Rscript 的 PowerShell 中,运行
python C:\path\to\script_name.py,假设 python` 在你的 PATH 变量中。 -
该函数提到一些错误来自
source(),因此脚本文件可能正在运行来自其他脚本文件的代码。这些文件可能引用“rstudioapi”。它们需要从命令行重新编写。看起来代码有install.packages(),这在你想从命令行重复运行的脚本中是不好的。没有任何形式的reproducible code,很难真正提供帮助。
标签: python r subprocess