Read command line parameters from an R script

0 votes

In a R script I would like to apply various command line parameters.

The script runs on Windows.

I'm not able to find how to read parameters applied on the command line to R-script.

Any suggestions on how to do this?

Apr 17, 2018 in Data Analytics by BHARANI
• 410 points
706 views

1 answer to this question.

0 votes

Here's an example to explain :

Two files are created: example.bat and example.R

  • example.bat:

    set R_Script="C:\Program Files\R-3.0.2\bin\RScript.exe"
    %R_Script% example.R 2018-01-28 example 100 > example.batch 2>&1

    Alternatively, using Rterm.exe:

    set R_TERM="C:\Program Files\R-3.0.2\bin\i386\Rterm.exe"
    %R_TERM% --no-restore --no-save --args 2018-01-28 example 100 < example.R > example.batch 2>&1
  • example.R:

    options(echo=TRUE) # To see commands in output file
    args <- commandArgs(trailingOnly = TRUE)
    print(args)
    # trailingOnly=TRUE this means that only your arguments are returned, 
     # To check you can use:
     # print(commandArgs(trailingOnly=FALSE))

start_date <- as.Date(args[1])
name <- args[2]
n <- as.integer(args[3])
rm(args)

# Few computations:
x <- rnorm(n)
png(paste(name,".png",sep=""))
plot(start_date+(1L:n), x)
dev.off()

summary(x)

Now, save both files in the same directory and start example.bat. In the result you'll get:

  • example.png with some plot
  • example.batch with all that was done

You can add an environment variable %R_Script%:

"C:\Program Files\R-3.0.2\bin\RScript.exe"

And then use it in your batch scripts as %R_Script% <filename.r> <arguments>

Differences between RScript and Rterm:

answered Apr 17, 2018 by kappa3010
• 2,020 points

Related Questions In Data Analytics

0 votes
1 answer

How can I figure out if an R program was executed from the command line?

Try this code: if (interactive()) { print('Hello ...READ MORE

answered Jun 28 in Data Analytics by Zulaikha
• 870 points
29 views
0 votes
1 answer

How to install an R package from source?

If you have your file located locally ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,690 points
60 views
0 votes
1 answer

How can I Split code over multiple lines in an R script?

You can do this as follows: setwd(paste("~/a/very/long/path/here ...READ MORE

answered May 21, 2018 in Data Analytics by zombie
• 3,690 points
131 views
0 votes
1 answer

How can I suppress warnings globally in an R Script ?

You can use: options(warn=-1) // turning off warnings ...READ MORE

answered May 30, 2018 in Data Analytics by zombie
• 3,690 points
359 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
56 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
77 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
184 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
121 views
0 votes
1 answer
0 votes
1 answer

How to join two tables (tibbles) by *list* columns in R

You can use the hash from digest ...READ MORE

answered Apr 5, 2018 in Data Analytics by kappa3010
• 2,020 points
72 views