Running R script from python

PythonR

Python Problem Overview


I searched for this question and found some answers on this, but none of them seem to work. This is the script that I'm using in python to run my R script.

import subprocess
retcode = subprocess.call("/usr/bin/Rscript --vanilla -e 'source(\"/pathto/MyrScript.r\")'", shell=True)

and I get this error:

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  no lines available in input
Calls: source ... withVisible -> eval -> eval -> read.csv -> read.table
Execution halted

and here is the content of my R script (pretty simple!)

data = read.csv('features.csv')
data1 = read.csv("BagofWords.csv")
merged = merge(data,data1)
write.table(merged, "merged.csv",quote=FALSE,sep=",",row.names=FALSE)
for (i in 1:length(merged$fileName))
{
        fileConn<-file(paste("output/",toString(merged$fileName[i]),".txt",sep=""))
        writeLines((toString(merged$BagofWord[i])),fileConn)
        close(fileConn)
}

The r script is working fine, when I use source('MyrScript.r') in r commandline. Moreover, when I try to use the exact command which I pass to the subprocess.call function (i.e., /usr/bin/Rscript --vanilla -e 'source("/pathto/MyrScript.r")') in my commandline it works find, I don't really get what's the problem.

Python Solutions


Solution 1 - Python

I would not trust too much the source within the Rscript call as you may not completely understand where are you running your different nested R sessions. The process may fail because of simple things such as your working directory not being the one you think.

Rscript lets you directly run an script (see man Rscript if you are using Linux).

Then you can do directly:

subprocess.call ("/usr/bin/Rscript --vanilla /pathto/MyrScript.r", shell=True)

or better parsing the Rscript command and its parameters as a list

subprocess.call (["/usr/bin/Rscript", "--vanilla", "/pathto/MyrScript.r"])

Also, to make things easier you could create an R executable file. For this you just need to add this in the first line of the script:

#! /usr/bin/Rscript

and give it execution rights. See here for detalis.

Then you can just do your python call as if it was any other shell command or script:

subprocess.call ("/pathto/MyrScript.r")

Solution 2 - Python

I think RPy2 is worth looking into, here is a cool presentation on R-bloggers.com to get you started:

http://www.r-bloggers.com/accessing-r-from-python-using-rpy2/

Essentially, it allows you to have access to R libraries with R objects that provides both a high level and low level interface.

Here are the docs on the most recent version: https://rpy2.github.io/doc/latest/html/

I like to point Python users to Anaconda, and if you use the package manager, conda, to install rpy2, it will also ensure you install R.

$ conda install rpy2

And here's a vignet based on the documents' introduction:

>>> from rpy2 import robjects
>>> pi = robjects.r['pi']
>>> pi
R object with classes: ('numeric',) mapped to:
<FloatVector - Python:0x7fde1c00a088 / R:0x562b8fbbe118>
[3.141593]

>>> from rpy2.robjects.packages import importr
>>> base = importr('base')
>>> utils = importr('utils')

>>> import rpy2.robjects.packages as rpackages
>>> utils = rpackages.importr('utils')
>>> packnames = ('ggplot2', 'hexbin')
>>> from rpy2.robjects.vectors import StrVector
>>> names_to_install = [x for x in packnames if not rpackages.isinstalled(x)]
>>> if len(names_to_install) > 0:
...     utils.install_packages(StrVector(names_to_install))

And running an R snippet:

>>> robjects.r('''
...         # create a function `f`
...         f <- function(r, verbose=FALSE) {
...             if (verbose) {
...                 cat("I am calling f().\n")
...             }
...             2 * pi * r
...         }
...         # call the function `f` with argument value 3
...         f(3)
...         ''')
R object with classes: ('numeric',) mapped to:
<FloatVector - Python:0x7fde1be0d8c8 / R:0x562b91196b18>
[18.849556]

And a small self-contained graphics demo:

from rpy2.robjects.packages import importr
graphics = importr('graphics')
grdevices = importr('grDevices')
base = importr('base')
stats = importr('stats')

import array

x = array.array('i', range(10))
y = stats.rnorm(10)

grdevices.X11()

graphics.par(mfrow = array.array('i', [2,2]))
graphics.plot(x, y, ylab = "foo/bar", col = "red")

kwargs = {'ylab':"foo/bar", 'type':"b", 'col':"blue", 'log':"x"}
graphics.plot(x, y, **kwargs)


m = base.matrix(stats.rnorm(100), ncol=5)
pca = stats.princomp(m)
graphics.plot(pca, main="Eigen values")
stats.biplot(pca, main="biplot")

Solution 3 - Python

I would not suggest using a system call to there are many differences between python and R especially when passing around data.

There are many standard libraries to call R from Python to choose from see this answer

Solution 4 - Python

If you just want run a script then you can use system("shell command") of the sys lib available by import sys. If you have an usefull output you can print the result by " > outputfilename" at the end of your shell command.

For example:

import sys

system("ls -al > output.txt")

Solution 5 - Python

Try adding a line to the beginning of your R script that says:

setwd("path-to-working-directory")

Except, replace the path with the path to the folder containing the files features.csv and BagofWords.csv.

I think the problem you are having is because when you run this script from R your working directory is already the correct path, but when you run the script from python, it defaults to a working directory somewhere else (likely the top of the user directory).

By adding the extra line at the beginning of your R script, you are explicitly setting the working directory and the code to read in these files will work. Alternatively, you could replace the filenames in read.csv() with the full filepaths of these files.

@dmontaner suggested this possibility in his answer:

> The process may fail because of simple things such as your working directory not being the one you think.

Solution 6 - Python

The following code should work out:

import rpy2.robjects as robjects
robjects.r.source("/pathto/MyrScript.r", encoding="utf-8")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEhsanView Question on Stackoverflow
Solution 1 - PythondmontanerView Answer on Stackoverflow
Solution 2 - PythonRussia Must Remove PutinView Answer on Stackoverflow
Solution 3 - PythonpyCthonView Answer on Stackoverflow
Solution 4 - PythonMips42View Answer on Stackoverflow
Solution 5 - PythonenpitsuView Answer on Stackoverflow
Solution 6 - Pythonah bonView Answer on Stackoverflow