R scripts in python

This is the place for queries that don't fit in any of the other categories.

R scripts in python

Postby JessiT » Tue Jul 23, 2013 11:15 pm

I am interested in running an R script from python. My example testscript.R has two lines of code.
x <- c(1,2,3,4,5)
mean(x)

In R, running this script would result in the value 3. I would like to see the results of running the script in python. The code below are variations on using subprocess to access R from python, however, they do not show the result from R. Here is some of my trail code.

Code: Select all
import subprocess

paths  = 'C: \\Program Files\\R\\R-2.15.2\\bin\\Rscript.exe --vanilla C :\\Users\\j\\Desktop\\testscript.R'

process = subprocess.Popen(paths, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

process.communicate()[0]

process.returncode

print process.stdout


Trial 2
Code: Select all
import subprocess

Rfilepath = "--vanilla C:\\Users\\j\\Desktop\\testscript"

RScriptCmd = "C:\\Program Files\\R\\R-2.15.2\\bin\\RScript.exe"

subprocess.call([RScriptCmd,Rfilepath],shell=True)


Trial 3
Code: Select all
import subprocess

command  = 'C:\\Program Files\\R\\R-2.15.2\\bin\\Rscript.exe --vanilla C:\\Users\\j\\Desktop\\testscript'

process = subprocess.Popen(command, stdout=open('C:\\Users\\j\\Desktop\\output.out', 'w')).wait()


A follow up to this would be to create x in python send it to R and run testscript containing one line "mean(x)", then view the output in python.
JessiT
 
Posts: 7
Joined: Wed Jun 05, 2013 10:53 pm

Re: R scripts in python

Postby Mekire » Wed Jul 24, 2013 1:09 am

I have zero knowledge of R, but it sounds like this might be a good place to start looking before you start trying to do it all with subprocess.

http://rpy.sourceforge.net/

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: R scripts in python

Postby JessiT » Wed Jul 24, 2013 1:42 am

It is my understanding that rpy and rpy2 allows python users access to the r libraries and could do the same thing, however, I have a 17 page R script already created which I do not wish to rewrite.
JessiT
 
Posts: 7
Joined: Wed Jun 05, 2013 10:53 pm

Re: R scripts in python

Postby Mekire » Wed Jul 24, 2013 2:41 am

I had success with both of these:
Code: Select all
import subprocess

command = ("C:/Program Files/R/R-3.0.1/bin/Rscript.exe "
           "--vanilla C:/Users/Owner/Documents/R_scripts/test.R")

print(subprocess.check_output(command))

Code: Select all
import subprocess

command = ("C:/Program Files/R/R-3.0.1/bin/Rscript.exe "
           "--vanilla C:/Users/Owner/Documents/R_scripts/test.R")

process = subprocess.Popen(command,stdout=subprocess.PIPE)
print(process.communicate()[0])

Output in both cases:
Code: Select all
[1] 3

-Mek

Edit:
removed wait() call in second one as I think it is both unnecessary and contra-indicated.
Last edited by Mekire on Wed Jul 24, 2013 2:55 am, edited 1 time in total.
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: R scripts in python

Postby JessiT » Wed Jul 24, 2013 2:54 am

Thank you.
JessiT
 
Posts: 7
Joined: Wed Jun 05, 2013 10:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests