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

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Sam Tuck
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

Eric Berger
Hi Sam,
I use the littler package for scripting. You may find it meets your needs.

https://github.com/eddelbuettel/littler

HTH,
Eric


On Wed, Jun 13, 2018 at 5:33 AM, Sam Tuck <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

R help mailing list-2
In reply to this post by Sam Tuck
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

R help mailing list-2
In reply to this post by Sam Tuck
When I want to run multiple scripts one after the other, and have variables created in a script be still in memory for use by a subsequent script, I normally create a master script (say, "runall.r") and it sources each of the others in turn. For example, my master script (runall.r) would look like this:

## master script (runall.r)
source('script1.r')
source('script2.r')
source('script3.r')
cat('[runall.r] Done\n')
## end of master script

Then, optionally, I can pass command line arguments to runall.r and parse them before sourcing the other scripts.

Since all three scripts are run in the same R session, all variables in memory when script1 finishes will be available to script2, and so on.

If you want to have each of the scripts be executable (by which I mean you run them by typing their name at the command line in a Linux environment; I don't know how it's done in Windows) and have the results of script1 be used as command line arguments to script2 -- well, I'm sure it can be done, but I don't think R's way of doing things is conducive to this approach. If you want to go that route, look up the --save, --no-save, --restore, and --no-restore arguments to R scripts.

See also the help page for Rscript [type help('Rscript') or ?Rscript at the R prompt] if you haven't already.

In my opinion, there are pros and cons to William Dunlap's suggestion to embed everything in functions in a package. I do it either way, depending on various factors. I would suggest, however, that you wait until you have more R experience before trying that route.

Incidentally, this construct:

  fileConn<-file("output.txt")
  writeLines(c(TotalDeviation, IndividualDeviation, RecenterPeriods), fileConn)
  close(fileConn)

looks to me like it's more complex than needed.
I would suggest

  cat( TotalDeviation, IndividualDeviation, RecenterPeriods, '\n', file='output.txt')


--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

´╗┐On 6/12/18, 7:33 PM, "R-help on behalf of Sam Tuck" <[hidden email] on behalf of [hidden email]> 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
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

Alex Zarebski
In reply to this post by R help mailing list-2
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 <
[hidden email]> 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 <[hidden email]> 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
> >
> > ______________________________________________
> > [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

Jeff Newmiller
In reply to this post by R help mailing list-2
I echo Bill's sentiments regarding use of functions, but think that you can afford to delay building packages while you are in the exploratory phase.

You can start out by building your sequence of statements using explicitly defined variables at the beginning like

fname <- "test.csv"
#your code

and then wrap the statements into functions using those fname as a parameter:

myfunc <- function( fname ) {
   # your code
}

Functions are written in files, but loaded into (typically) the global variables environment to be used.

RStudio helps incentivise working with functions by supporting debugging in the original source code if you source the entire file. Just mark the function

debug(myfunc)

before running code the directly or indirectly runs that function.

(Debugging works at the terminal console as well, but it only shows you the next line so you have to keep track of where you are in the function yourself.)

Once you have a lot of useful functions available, you will find making packages to be a way better approach to handling code re-use than keeping track of script files.

On June 13, 2018 6:27:10 AM HST, William Dunlap via R-help <[hidden email]> 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 <[hidden email]>
>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
>>
>> ______________________________________________
>> [hidden email] 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]]
>
>______________________________________________
>[hidden email] 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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] 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.