3

After seven hours of googling and rereading through somewhat similar questions, and then lots of trial and error, I'm now comfortable asking for some guidance.

To simplify my actual task, I created a very basic R script (named test_script):

x <- c(1,2,3,4,5)
avg <- mean(x)
write.csv(avg, file = "output.csv")

This works as expected.

I'm new to python and I'm just trying to figure out how to execute the R script so that the same .csv file is created.

Notable results come from:

subprocess.call(["C:/Program Files/R/R-2.15.2/bin/R", 'C:/Users/matt/Desktop/test_script.R'])

This opens a cmd window with the typical R start-up verbiage, except there is a message which reads, "ARGUMENT 'C:/Users/matt/Desktop/test_script.R' __ ignored __"

And:

subprocess.call(['C:/Program Files/R/R-2.15.2/bin/Rscript', 'C:/Users/matt/Desktop/test_script.r'])

This flashes a cmd window and returns a 0, but no .csv file is created.

Otherwise, I've tried every suggestion I could identify on this site or any other. Any insight will be greatly appreciated. Thanks in advance for your time and efforts.

2
  • 1
    I'm confused. Have you not discovered rpy? Commented Jan 29, 2013 at 1:07
  • 1
    Certainly. I've researched Rpy and installed it's successor, Rpy2. However, everything I need to do in R is already done and I am just trying to execute the script. If there is a way to do that in Rpy2, than I'm all ears! Commented Jan 29, 2013 at 17:26

2 Answers 2

3

Running R --help at the command prompt prints:

Usage: R [options] [< infile] [> outfile]
   or: R CMD command [arguments]

Start R, a system for statistical computation and graphics, with the
specified options, or invoke an R tool via the 'R CMD' interface.

Options:
  -h, --help            Print short help message and exit
  --version             Print version info and exit
  ...
  -f FILE, --file=FILE  Take input from 'FILE'
  -e EXPR               Execute 'EXPR' and exit

FILE may contain spaces but not shell metacharacers.

Commands:
  BATCH         Run R in batch mode
  COMPILE       Compile files for use with R
  ...

Try

call(["C:/Program Files/R/R-2.15.2/bin/R", '-f', 'C:/Users/matt/Desktop/test_script.R'])

There are also some other command-line arguments you can pass to R that may be helpful. Run R --help to see the full list.

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

1 Comment

Most people prefer Rscript for batch-style execution.
1

It might be too late, but hope it helps for others:

Just add --vanilla in the call list.

subprocess.call(['C:/Program Files/R/R-2.15.2/bin/Rscript',  '--vanilla', 'C:/Users/matt/Desktop/test_script.r'])

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.