Running R Code from Command Line (Windows)

Viewed 60579

I have some R code inside a file called analyse.r. I would like to be able to, from the command line (CMD), run the code in that file without having to pass through the R terminal and I would also like to be able to pass parameters and use those parameters in my code, something like the following pseudocode:

C:\>(execute r script) analyse.r C:\file.txt

and this would execute the script and pass "C:\file.txt" as a parameter to the script and then it could use it to do some further processing on it.

How do I accomplish this?

4 Answers

save the following in a text file

f1 <- function(x,y){
print (x)
print (y)
}
args = commandArgs(trailingOnly=TRUE)
f1(args[1], args[2])

No run the following command in windows cmd

Rscript.exe path_to_file "hello" "world"

This will print the following

[1] "hello"
[1] "world"
Related