Running R Code from Command Line (Windows)

Solution 1:

  1. You want Rscript.exe.

  2. You can control the output from within the script -- see sink() and its documentation.

  3. You can access command-arguments via commandArgs().

  4. You can control command-line arguments more finely via the getopt and optparse packages.

  5. If everything else fails, consider reading the manuals or contributed documentation

Solution 2:

Identify where R is install. For window 7 the path could be

1.C:\Program Files\R\R-3.2.2\bin\x64>
2.Call the R code
3.C:\Program Files\R\R-3.2.2\bin\x64>\Rscript Rcode.r

Solution 3:

There are two ways to run a R script from command line (windows or linux shell.)

1) R CMD way R CMD BATCH followed by R script name. The output from this can also be piped to other files as needed.

This way however is a bit old and using Rscript is getting more popular.

2) Rscript way (This is supported in all platforms. The following example however is tested only for Linux) This example involves passing path of csv file, the function name and the attribute(row or column) index of the csv file on which this function should work.

Contents of test.csv file x1,x2 1,2 3,4 5,6 7,8

Compose an R file “a.R” whose contents are

#!/usr/bin/env Rscript

cols <- function(y){
   cat("This function will print sum of the column whose index is passed from commandline\n")
   cat("processing...column sums\n")
   su<-sum(data[,y])
   cat(su)
   cat("\n")
}

rows <- function(y){
   cat("This function will print sum of the row whose index is passed from commandline\n")
   cat("processing...row sums\n")
   su<-sum(data[y,])
   cat(su)
   cat("\n")
}
#calling a function based on its name from commandline … y is the row or column index
FUN <- function(run_func,y){
    switch(run_func,
        rows=rows(as.numeric(y)),
        cols=cols(as.numeric(y)),
        stop("Enter something that switches me!")
    )
}

args <- commandArgs(TRUE)
cat("you passed the following at the command line\n")
cat(args);cat("\n")
filename<-args[1]
func_name<-args[2]
attr_index<-args[3]
data<-read.csv(filename,header=T)
cat("Matrix is:\n")
print(data)
cat("Dimensions of the matrix are\n")
cat(dim(data))
cat("\n")
FUN(func_name,attr_index)

Runing the following on the linux shell Rscript a.R /home/impadmin/test.csv cols 1 gives you passed the following at the command line /home/impadmin/test.csv cols 1 Matrix is: x1 x2 1 1 2 2 3 4 3 5 6 4 7 8 Dimensions of the matrix are 4 2 This function will print sum of the column whose index is passed from commandline processing...column sums 16

Runing the following on the linux shell

  Rscript a.R /home/impadmin/test.csv rows 2 

gives

you passed the following at the command line
    /home/impadmin/test.csv rows 2
Matrix is:
      x1 x2
    1  1  2
    2  3  4
    3  5  6
    4  7  8
Dimensions of the matrix are
    4 2

This function will print sum of the row whose index is passed from commandline processing...row sums 7

We can also make the R script executable as follows (on linux)

 chmod a+x a.R

and run the second example again as

   ./a.R /home/impadmin/test.csv rows 2

This should also work for windows command prompt..