使用 powershell 和 python 调用 Rscript
Calling an Rscript using powershell and python
我在 python 中编码,我的同事在 R 中编码。我正在尝试调用他编写的 Rscript 文件以将其合并到 python 应用程序中。我能够使用 window 的强大功能 shell 成功调用 Rscript,但是,当对 python 子进程库 [subprocess.run() /[ 使用完全相同的命令时=26=]()/ subprocess.Popen()/ subprocess.check_output()]。
在 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 window 并将工作命令输入终端 window 的方法。我一直被这个难住了,我不认为我理解子流程功能。
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 是脚本中函数的名称
我找到了问题的答案。我发现的第一件事是 subprocess.check_output 记录了我的 python 控制台的整个日志,因此在尝试调试我的代码时,所有重复的“执行暂停”消息都是我的 activity 的不同实例。 2,调用 运行 作为子进程的 Rscript 失败,因为默认情况下,它调用命令提示符而不是 Powershell。这是使用以下代码行修复的。
subprocess.check_output(["powershell.exe",command])
我在 python 中编码,我的同事在 R 中编码。我正在尝试调用他编写的 Rscript 文件以将其合并到 python 应用程序中。我能够使用 window 的强大功能 shell 成功调用 Rscript,但是,当对 python 子进程库 [subprocess.run() /[ 使用完全相同的命令时=26=]()/ subprocess.Popen()/ subprocess.check_output()]。 在 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 window 并将工作命令输入终端 window 的方法。我一直被这个难住了,我不认为我理解子流程功能。
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 是脚本中函数的名称
我找到了问题的答案。我发现的第一件事是 subprocess.check_output 记录了我的 python 控制台的整个日志,因此在尝试调试我的代码时,所有重复的“执行暂停”消息都是我的 activity 的不同实例。 2,调用 运行 作为子进程的 Rscript 失败,因为默认情况下,它调用命令提示符而不是 Powershell。这是使用以下代码行修复的。
subprocess.check_output(["powershell.exe",command])