3
rtest = function(input ,output) {
  a <- input
  b <- output 
  outpath <- a+b
  print(a+b)
  return(outpath)
}

I have just return this R code for as a function for getting sum of two numbers. I tried to run this function from my python code using subprocess by passing 2 numbers as arguments. But it does not return sum value as return output. Do you know any method for implement this in python3 by passing function arguments.

my python code using subprocess is:

args=['3','10'] # (i tried to pass aruments like this) 
command="Rscript" 
path2script = '/...path/rtest.R' 
cmd = [command, path2script] +args 
x = subprocess.check_output(cmd, universal_newlines=True) 
print(x)

but x return ' ' null value

1
  • 1
    Please show your actual code for both your python subprocess call and your R script. It is not a problem to run a R script from python using subprocess. The problem is probably with your subprocess call. What are you actually calling? This code you provide is just an R function. When you call the R script, does it run this function and return the output? Or does it just define the function and do nothing? Commented May 29, 2018 at 13:17

1 Answer 1

6

This could be easily done by rpy2 library in python.

    import rpy2.robjects as ro
    path="specify/path to/ R file"

        def function1(input,output):
            r=ro.r
            r.source(path+"rtest.R")
            p=r.rtest(input,output)
            return p


  a=function1(12,12)   # calling the function with passing arguments

Thanks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.