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
• 400 points
279 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,010 points

Related Questions In Data Analytics

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,680 points
30 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
74 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
162 views
0 votes
1 answer

Filter something from dataframe in an order in R

You can use conditional filtering for this ...READ MORE

answered Dec 12, 2018 in Data Analytics by Maverick
• 10,000 points
20 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
27 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
27 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
36 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
64 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,010 points
36 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.