Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running R script from python

Tags:

python

r

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.

like image 726
Ehsan Avatar asked Nov 10 '13 19:11

Ehsan


People also ask

Can I run R script from Python?

rpy2 provides an interface that allows you to run R in Python processes. Users can move between languages and use the best of both programming languages.

Does R studio work with Python?

The RStudio IDE(opens in a new tab) is a free and open-source IDE for Python, as well as R. You can write scripts, import modules, and interactively use Python within the RStudio IDE. To get started writing Python in the RStudio IDE, go to File, New File, then Python Script.

How do I run an R script?

To run an R command, put the cursor on the line of the command and then click the Run button at the top of the file window. Or just press CTRL-Enter. In the next figure data was read, and a histogram created, with the lessR Histogram function.


2 Answers

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") 
like image 200
dmontaner Avatar answered Sep 28 '22 03:09

dmontaner


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") 
like image 27
Russia Must Remove Putin Avatar answered Sep 28 '22 04:09

Russia Must Remove Putin