Running R Code from Command Line (Windows) Running R Code from Command Line (Windows) r r

Running R Code from Command Line (Windows)


  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


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 code3.C:\Program Files\R\R-3.2.2\bin\x64>\Rscript Rcode.r


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

1) R CMD wayR 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 Rscriptcols <- 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 indexFUN <- 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 1givesyou passed the following at the command line /home/impadmin/test.csv cols 1Matrix is: x1 x2 1 1 2 2 3 4 3 5 6 4 7 8Dimensions of the matrix are 4 2This function will print sum of the column whose index is passed from commandlineprocessing...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 2Matrix is:      x1 x2    1  1  2    2  3  4    3  5  6    4  7  8Dimensions of the matrix are    4 2

This function will print sum of the row whose index is passed from commandlineprocessing...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..