简体   繁体   English

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

[英]How to pass single String value from Python sub-process code to R Code

I am trying to run a R code from the Python using Subprocess library.我正在尝试使用 Subprocess 库从 Python 运行 R 代码。 I need to run one.R file from the python and have pass one single String value(args = ["INV28338"]).我需要从 python 运行 one.R 文件并传递一个字符串值 (args = ["INV28338"])。 I am new in R and so not able to figure out exact data type conversion code in both R and Python.我是 R 的新手,因此无法在 R 和 Python 中找出确切的数据类型转换代码。

Please someone help me, how to pass single string/scalar value from Python to R Function/Model?请有人帮助我,如何将单个字符串/标量值从 Python 传递到 R 函数/模型? Later I will give the shape of 'Flask REST API' for this code.稍后我将为这段代码给出“Flask REST API”的形状。

Thank you so much in advance非常感谢你提前

Python code:- 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 code:- R代码:-

myArgs <- commandArgs(trailingOnly = TRUE)
nums = as.numeric(myArgs)
invoice_in_scope<-nums
#Followed by more code 

my script, test2.R我的脚本,test2.R

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

you need to have args as part of the list, so append it to cmd and it works ok:您需要将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)   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM