如何将单个字符串值从Python子过程代码传递到R代码

问题描述 投票:0回答:1

我正在尝试使用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 
python r python-3.x flask subprocess
1个回答
0
投票

我的脚本,test2.R

myArgs <- commandArgs(trailingOnly = TRUE)
nums = as.numeric(myArgs)
print(nums)

您需要将args作为列表的一部分,因此将其附加到cmd上就可以了:

import subprocess 
command = 'Rscript' 
path2script = './test2.R' 
args = "9" 
cmd = [command, path2script]
cmd.append(args)
x = subprocess.check_output(cmd, universal_newlines=True) 
print('The Output is:', x)   
© www.soinside.com 2019 - 2024. All rights reserved.