merlin2011
merlin2011

Reputation: 75639

How do I run R scripts with arguments from the R prompt?

Suppose I have an R script called foo.r. Normally I would run it as Rscript foo.r <args>.

How would I run the same script from the interactive R prompt from running R on the command line?

Upvotes: 3

Views: 896

Answers (2)

Spacedman
Spacedman

Reputation: 94317

This is almost a duplicate. First use one of the solutions from:

Set value of --args from within R session

to set the commandArgs(), then source("foo.R").

Upvotes: 0

qwwqwwq
qwwqwwq

Reputation: 7329

If it is the case that you need to run this script both interactively and non-interactively I would add some logic like this:

if( interactive() ) {
    args <- strsplit(readline("Enter Args: "), " ")
} else {
    args <- commandArgs(trailingOnly = TRUE)
}

Basically, if the script is being run interactively prompt user for a command option string that you will then parse somehow so that args is set in the same way as it would be from whatever command line parsing you are using now.

http://stat.ethz.ch/R-manual/R-devel/library/base/html/interactive.html

Upvotes: 2

Related Questions