我正在尝试使用Subprocess库从Python中运行R代码。我需要从python运行一个.R文件,并传递一个字符串值(args= "INV28338")。我是R的新手,所以不能在R和Python中弄清楚确切的数据类型转换代码。
请帮帮我,如何将Python中的单个字符串/标量值传递给R函数/模型?稍后我将给出这段代码的'Flask REST API‘的形状。
提前谢谢你
Python代码:
import subprocess
command = 'Rscript'
path2script = 'classification_model_code.R'
args = ["INV28338"]
cmd = [command, path2script] + args
x = subprocess.check_output(cmd, universal_newlines=True)
print('The Output is:', x)R代码:-
myArgs <- commandArgs(trailingOnly = TRUE)
nums = as.numeric(myArgs)
invoice_in_scope<-nums
#Followed by more code 发布于 2019-11-29 16:41:25
我的脚本test2.R
myArgs <- commandArgs(trailingOnly = TRUE)
nums = as.character(myArgs)
print(nums)您需要将args作为列表的一部分,因此将其附加到cmd,它就可以正常工作:
import subprocess
command = 'Rscript'
path2script = './test2.R'
args = "INV28338"
cmd = [command, path2script]
cmd.append(args)
x = subprocess.check_output(cmd, universal_newlines=True)
print('The Output is:', x) https://stackoverflow.com/questions/59099378
复制相似问题