简体   繁体   中英

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. I need to run one.R file from the python and have pass one single String value(args = ["INV28338"]). I am new in R and so not able to figure out exact data type conversion code in both R and Python.

Please someone help me, how to pass single string/scalar value from Python to R Function/Model? Later I will give the shape of 'Flask REST API' for this code.

Thank you so much in advance

Python code:-

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:-

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

my script, 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:

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)   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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