I have seen various versions of this question, but have not been able to figure out how to pass numeric variables into Python from R using system2()
For example suppose I have the following python script as Test.py.
a = 1
b = 2
def add(a,b):
print a + b
I can run this in R with
pypath = './Test.py'
system2('python', args=(sprintf('"%1$s"',pypath)))
but what if I wanted to pass the values of a and b in, and have the values returned within R to an R object? Something like,
a = 1
b = 2
d = system2('python',args = (sprintf('"%1$s" "%2$s" "%3$s"',pypath,a,b)))
I have tried a variety of ways of doing this, but have failed. I know the above is wrong, I just can't figure out how to pass in a and b, much less return a value
My questions are (1) is it possible? and (2) how do you do it?
system2('python', args = c('./Test.py', as.character(a), as.character(b))). To write into a file, as a workaround you can use>to direct the output to a temp file and read from the file with R. It'll be slower but it'll worksystem2('yourcommand',c('ARG1', 'ARG2', '>/targetDir/targetFile'))