Sam Tuck
2018-Jun-13 02:33 UTC
[R] Calling r-scripts with arguments from other scripts, or possibly R programming conventions/style guide
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
Eric Berger
2018-Jun-13 07:09 UTC
[R] Calling r-scripts with arguments from other scripts, or possibly R programming conventions/style guide
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 <STuck at 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 at 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]]
William Dunlap
2018-Jun-13 16:27 UTC
[R] Calling r-scripts with arguments from other scripts, or possibly R programming conventions/style guide
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 at 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 at 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]]
Alex Zarebski
2018-Jun-13 22:55 UTC
[R] Calling r-scripts with arguments from other scripts, or possibly R programming conventions/style guide
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 at 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 at 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 at 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 at 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]]
Jeff Newmiller
2018-Jun-14 02:03 UTC
[R] Calling r-scripts with arguments from other scripts, or possibly R programming conventions/style guide
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 <r-help at 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 at 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 at 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 at 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.-- Sent from my phone. Please excuse my brevity.