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

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Jun 14 04:03:39 CEST 2018


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 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.

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




More information about the R-help mailing list