[R] Calling r-scripts with arguments from other scripts, or possibly R programming conventions/style guide

Alex Zarebski @ez@reb@k| @end|ng |rom gm@||@com
Thu Jun 14 00:55:32 CEST 2018


Sourcing scripts is a bit hacky but it is a quick way to get a job done.
If you want to take your source-ing to the next level you might want to
look up how to use the "local" argument with environments.

Packages are probably the way to go if you are doing anything substantial
though.

Also, "argparse" (available through CRAN) provides some tools for parsing
command line arguments.
It is very similar to the python package of the same name which might be
more familiar.

Cheers,
Alex

On Thu, Jun 14, 2018 at 2:27 AM, William Dunlap via R-help <
r-help using r-project.org> wrote:

> Use functions calling functions instead of scripts invoking scripts.  Put
> all your
> functions in a 'package'.  Then your scripts would be very small, just
> passing
> command line arguments to R functions.
>
> (It is possible to call scripts from scripts, but I think it quickly leads
> to headaches.)
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Tue, Jun 12, 2018 at 7:33 PM, Sam Tuck <STuck using nzsuperfund.co.nz> wrote:
>
> > Hi All,
> >           I am new to R and am wondering if there is a way to pass
> > arguments between rscripts.  I have this working but have had to create a
> > C# shell calling the scripts in sequence via windows scripting which
> > enables command line arguments to get the necessary interaction.
> >
> > I'm wondering if I'm using an outdated program construction technique - I
> > create r files like I would programme functions or reoccurring code
> > snippets in C.  It may be that r was not designed to create lots of
> little
> > r script modules that interact via a master script?
> >
> > Ideally I'd like to call r scripts from other r scripts and have all the
> > variables still in memory: For example
> >
> > I've been using RStudio Version 1.1.447 to programme and regression test
> > my individual scripts,.
> >
> > Script Arg Script.R
> > {
> > # We are going to pass arguments into this script
> > arguments <- commandArgs(trailingOnly = TRUE)
> > #arguments[1] is double
> > #arguments[2] is double
> > #arguments[3] is double.
> > if(length(arguments) <3)
> > {
> >   stop("Not enough arguments, please supply 3, [% dbl}total deviation, [%
> > dbl] individual deviation, [int] periods before recenter")
> > }
> > TotalDeviation <- as.numeric(arguments[1])/100
> > IndividualDeviation <- as.numeric(arguments[2])/100
> > RecenterPeriods <- as.numeric(arguments[3])
> > # We then manipulate some objects based on these inputs, but for this
> test
> > we will output them to a file.
> > fileConn<-file("output.txt")
> > writeLines(c(TotalDeviation, IndividualDeviation, RecenterPeriods),
> > fileConn)
> > close(fileConn)
> > }
> > Script RunningScript.R
> > {
> > Arg Script.R 0.6 0.4 132
> > }
> >
> > To which I get
> > Error: unexpected symbol in " Arg Script.R"
> >
> > When I use the script RunningScript.R
> > {
> > system(paste("Arg Script.R", 0.8, 0.4, 132))
> > }
> > Nothing occurs (there is no output file created, but also no error)
> >
> > When I use RunningScript.R
> > {
> > commandArgs <- c(0.6,0.4,132)
> > source("Arg Script.R')
> > }
> > I don't get any args passed into the file.  Instead getting the error
> > Not enough arguments, please supply 3, [% dbl}total deviation, [% dbl]
> > individual deviation, [int] periods before recenter
> >
> > Thanks
> >
> > Sam Tuck
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/
> > posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]




More information about the R-help mailing list