[英]Calling R script using subprocess command but Library() command in r script prevents it from running
我創建了一個 R 腳本,我需要從 Python 調用 R 腳本。 R 腳本需要運行一些包,但是每當我運行我的 python 代碼時,我都會收到以下錯誤:
CalledProcessError: Command '['C:/Program Files/R/R-3.2.3/bin/x64/Rscript.exe', 'D:/Abhi/desktop/testing/SCRIPTS/JMOTIF/data-04-13/NewClassificationMethod.R', '51', '9', '20', '20', 'D://Abhi//desktop//testing//SCRIPTS//JMOTIF//data-04-13//PureAgri.csv', 'D://Abhi//desktop//testing//SCRIPTS//JMOTIF//data-04-13//PureForest.csv']' returned non-zero exit status 1
。
如果我注釋掉庫調用,那么我不會出錯並且腳本可以工作。
作為一個小例子,以下代碼將不起作用:
pythonArgs = commandArgs(trailingOnly = TRUE)
library(plyr)
wSize= as.numeric(pythonArgs[1]) #as.numeric(pythonArgs[1])#Window Size
paaSize=as.numeric(pythonArgs[2])#Pax approximation size
cat(wSize, paaSize)
這段代碼雖然有效
pythonArgs = commandArgs(trailingOnly = TRUE)
#library(plyr)
wSize= as.numeric(pythonArgs[1]) #as.numeric(pythonArgs[1])#Window Size
paaSize=as.numeric(pythonArgs[2])#Pax approximation size
cat(wSize, paaSize)
以下是我目前從 Python 調用它的方式:
cmd = [command, path2Script] + args
x = subprocess.check_output(cmd,universal_newlines = True)
考慮使用 Python 的子進程Popen
有條件地捕獲子進程的輸出或錯誤,這里是 R 腳本。
from subprocess import Popen, PIPE
cmd = [command, path2Script] + args
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, error = p.communicate()
if p.returncode == 0:
print('R OUTPUT:\n {0}'.format(output))
else:
print('R ERROR:\n {0}'.format(error))
如果您運行POpen
,則會直接在 Python 控制台中產生一條更具信息性的錯誤消息,提醒您包路徑情況。
R ERROR:
b"Error in library(plyr) : there is no package called 'plyr'\r\nExecution halted\r\n"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.