3

When I execute the script in R, it is:

$ R --vanilla --args test_matrix.csv < hierarchical_clustering.R > out.txt

In Python, it works if I use:

process = subprocess.call("R --vanilla --args "+output_filename+"_DM_Instances_R.csv < /home/kevin/AV-labels/Results/R/hierarchical_clustering.R > "+output_filename+"_out.txt", shell=True)

But this method doesn't provide the process.wait() function.

So, I would like to use the subprocess.Popen, I tried:

process = subprocess.Popen(['R', '--vanilla', '--args', "\'"+output_filename+"_DM_Instances_R.csv\'",  '<', '/home/kevin/AV-labels/Results/R/hierarchical_clustering.R'])

But it didn't work, Python just opened R but didn't execute my script.

4
  • 1
    have you tried with shell=True, i.e: subprocess.Popen(['R', '--vanilla', '--args', "\'"+output_filename+"_DM_Instances_R.csv\'", '<', '/home/kevin/AV-labels/Results/R/hierarchical_clustering.R'], shell=True) Commented Jun 22, 2011 at 4:38
  • It says:Fatal error: you must specify '--save', '--no-save' or '--vanilla' But the '--vanilla' is there... Pls help!! Thanks!! Commented Jun 22, 2011 at 18:33
  • According to 'R --help', the '--args' argument means 'Skip the rest of the command line'. This is probably not what you mean. Have you got command line that you could show us that works in the shell? Commented Jun 22, 2011 at 19:11
  • I mean... when I run my script in R, it works with $ R --vanilla --args test_matrix.csv < hierarchical_clustering.R > out.txt Besides, when I use the subprocess.call in python, it works as well... but i need the subprocess.Popen so that I can use the wait() function. Commented Jun 22, 2011 at 20:15

5 Answers 5

4

Instead of 'R', give it the path to Rscript. I had the same problem. Opens up R but doesn't execute my script. You need to call Rscript (instead of R) to actually execute the script.

retcode = subprocess.call("/Pathto/Rscript --vanilla /Pathto/test.R", shell=True)

This works for me.

Cheers!

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

Comments

1

I've solved this problem by putting everything into the brackets..

process = subprocess.Popen(["R --vanilla --args "+output_filename+"_DM_Instances_R.csv < /home/kevin/AV-labels/Results/R/hierarchical_clustering.R > "+output_filename+"_out.txt"], shell=True)
process.wait()

Comments

1

A couple of ideas:

  1. You might want to consider using the Rscript frontend, which makes running scripts easier; you can pass the script filename directly as a parameter, and do not need to read the script in through standard input.
  2. You don't need the shell for just redirecting standard output to a file, you can do that directly with subprocess.Popen.

Example:

import subprocess

output_name = 'something'
script_filename = 'hierarchical_clustering.R'
param_filename = '%s_DM_Instances_R.csv' % output_name
result_filename = '%s_out.txt' % output_name
with open(result_filename, 'wb') as result:
    process = subprocess.Popen(['Rscript', script_filename, param_filename],
                               stdout=result);
    process.wait()

Comments

0

You never actually execute it fully ^^ try the following

process = subprocess.Popen(['R', '--vanilla', '--args', '\\%s_DM_Instances_R.csv\\' % output_filename, '<', '/home/kevin/AV-labels/Results/R/hierarchical_clustering.R'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) 
process.communicate()#[0] is stdout

2 Comments

It says:Fatal error: you must specify '--save', '--no-save' or '--vanilla' But the '--vanilla' is there... Pls help!! Thanks!!
I have never used R before and don't know the arguments.
0

Keven's solution works for my requirement. Just to give another example about @Kevin's solution. You can pass more parameters to the rscript with python-style string:

import subprocess

process = subprocess.Popen(["R --vanilla --args %s %d %.2f < /path/to/your/rscript/transformMatrixToSparseMatrix.R" % ("sparse", 11, 0.98) ], shell=True)
process.wait()

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 --vanilla --default-packages=utils

Reference: Using R as a scripting language with Rscript or this link

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.