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
• 420 points
3,382 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,090 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, 2019 in Data Analytics by Zulaikha
• 910 points
424 views
0 votes
1 answer

How to install an R package from source?

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

answered Apr 14, 2018 in Data Analytics by CodingByHeart77
• 3,740 points
621 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 22, 2018 in Data Analytics by zombie
• 3,790 points
1,752 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,790 points
2,068 views
0 votes
0 answers

Call R function in Linux command line

Is there a way to directly call ...READ MORE

Apr 13, 2022 in Linux Administration by Rahul
• 9,670 points
281 views
0 votes
1 answer

Big Data transformations with R

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

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
773 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,660 points
848 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,660 points
5,557 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 6, 2018 in Data Analytics by kappa3010
• 2,090 points
1,422 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP