[R] Command line arguments with source() - Windows OS

Duncan Murdoch murdoch at stats.uwo.ca
Mon Nov 17 15:22:30 CET 2008


On 17/11/2008 9:14 AM, Brigid Mooney wrote:
> Is there a better command to use rather than source which would take 
> command arguments? 
>  
> I ask because I currently have 6 parameters, will likely have additional 
> paramaters later, and would like to be able to have default values for 
> each, if I do not specify new values. 

The way I would do it is to write a function to do the calculations. 
Then you can have defaults for the parameters of the function.  You 
might choose to put the function in your script, or more easily, but I 
would recommend against it, just keep a copy in your current workspace.

So for example,

DoIt <- function(param1 = 1, param2 = 2) {

    ... lots of commands ...
}

Then

Doit()
Doit(param1 = 7)
etc.

Duncan Murdoch

>  
> Thanks so much!
> 
> On Mon, Nov 17, 2008 at 9:06 AM, Duncan Murdoch <murdoch at stats.uwo.ca 
> <mailto:murdoch at stats.uwo.ca>> wrote:
> 
>     Brigid Mooney wrote:
> 
>         Hi Everyone,
> 
>         I am pretty new to R and so far have mostly been using R
>         interactively
>         through the Windows console.
> 
>         I'm starting to write some scripts, and have been executing them
>         using the
>         source() command, i.e. source(myRfile.R).
> 
>         My questions is how can I pass command line arguments to R.  My file
>         "myRfile.R" has some global variables which I would like to be
>         able to set
>         at run-time, without having to go in and edit the text of the
>         file each time
>         I want to run it.
> 
>         I can't seem to find much information on this topic for Windows
>         and using it
>         with the console - so any help would be greatly appreciated.
> 
> 
>     This is the same on all platforms, Windows isn't special.
> 
>     When you use source(), any variables currently defined in the R
>     session will be visible to your script.  There is no "command line"
>     needed, because you're executing the R code in the same session.
> 
>     So you could do this:
> 
>     paramValue <- 10
>     source("myRfile.R")
> 
>     paramValue <- 15
>     source("myRfile.R")
> 
>     The quotes are necessary, because source(myRfile.R) would go looking
>     for a variable named myRfile.R, rather than using "myRfile.R" as the
>     filename.
> 
>     Duncan Murdoch
> 
>



More information about the R-help mailing list