LearnToGrow
LearnToGrow

Reputation: 1750

How to run R script in python using rpy2

My question seems very basic but I don't find an answer even on rpy2 documentation. I have *.R script that accept one argument as "file.txt" (I need to pass the argument not from the command line). I want to call the R script inside a python script . My questions is: How to pass and recuperate the arguments to the R script ? My solution is : suppose that the R script is start by this line:

df <- read.table(args[1], header=FALSE)
"
 here args[1] should be the file which is not passed from the command line
"
....

Now I write a function in my python script:

from rpy2 import robjects as ro
def run_R(file):
    r = ro.r
    r.source("myR_script.R")
   # how to pass the file argument to
   # the R script and how to
   # recuperate this argument in the R code?

Upvotes: 7

Views: 9877

Answers (2)

Parfait
Parfait

Reputation: 107587

Why use rpy2 simply to run an R script? Consider avoiding this interface and instead use the automated Rscript.exe command line which Python can call with built-in subprocess like any external executable even while passing needed arguments.

Below assumes you have your R bin folder in the PATH environment variable to recognize Rscript. If not, add full path of this executable in first arg of cmd. Also, be sure to pass full path of file into run_R method:

from subprocess import Popen, PIPE

def run_R(file):
  # COMMAND WITH ARGUMENTS
  cmd = ["Rscript", "myR_script.R", file]

  p = Popen(cmd, cwd="/path/to/folder/of/my_script.R/"      
            stdin=PIPE, stdout=PIPE, stderr=PIPE)     
  output, error = p.communicate()

  # PRINT R CONSOLE OUTPUT (ERROR OR NOT)
  if p.returncode == 0:            
      print('R OUTPUT:\n {0}'.format(output))            
  else:                
      print('R ERROR:\n {0}'.format(error))

Upvotes: 7

lgautier
lgautier

Reputation: 11545

My question seems very basic but I don't find an answer even on rpy2 documentation.

This might be a good starting point though:

https://rpy2.github.io/doc/v3.0.x/html/robjects_rpackages.html#importing-arbitrary-r-code-as-a-package

(...)

df <- read.table(args[1], header=FALSE)
"
 here args[1] should be the file which is not passed from the command line
"

The arguments to the command line have long been passed to R when you'll reach there (as R is already initialized and running by then). The link above in the documentation would be a relatively elegant way to solve it. Otherwise you could always create a vector args in R:

rpy2.robjects.globalenv['args'] = robjects.vectors.StrVector(['my_file.csv'])
r.source("myR_script.R")

Upvotes: 0

Related Questions