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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Kevin
  • 31
  • 1
  • 1
  • 2
  • 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) – c-ram Jun 22 '11 at 04:38
  • It says:Fatal error: you must specify '--save', '--no-save' or '--vanilla' But the '--vanilla' is there... Pls help!! Thanks!! – Kevin Jun 22 '11 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? – Michael Dunn Jun 22 '11 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. – Kevin Jun 22 '11 at 20:15

5 Answers5

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!

Tristan Tao
  • 855
  • 11
  • 29
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()
Kevin
  • 2,191
  • 9
  • 35
  • 49
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()
Jukka Matilainen
  • 9,608
  • 1
  • 25
  • 19
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

Community
  • 1
  • 1
Good Will
  • 1,220
  • 16
  • 10
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
Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91