[R] Inheritance and automatic function call on script exit

Joshua Wiley jwiley.psych at gmail.com
Wed Oct 6 17:19:33 CEST 2010


On Wed, Oct 6, 2010 at 7:59 AM, Ralf B <ralf.bierig at gmail.com> wrote:
> Hi all,
>
> in order to add certain standard functionality to a large set of
> scripts that I maintain, I developed a superscript that I manually
> include into every script at the beginning. Here an example of a very
> simplified super and subscript.

This sounds like it may be time to put things in a package.  Then in
your scripts you could just load the package and use functions.  Just
my 2 cents.

>
> #
> # superscript
> #
>
> output <- NULL
>
> writeOutput <- function() {
>        processTime <- proc.time()
>        outputFilename <- paste("C:/myOutput_", processTime[3], ".csv", sep="")
>        write.csv(output, file = outputFilename)
> }
>
>
>
> #
> # subscript
> #
>
> source("C:/superscript.R")
> output <- data.frame(a=c(1,2,3), b=c(4,5,6))
> writeOutput()
>
>
> I would like to
>
> a) avoid the need to include the super script manually. Does R support
> some kind of script inheritance?
> b) Is it possible to call writeOutput() automatically when a script is exiting?

If you are running these interactively, you could make your own
source() function.  In that function you could define the super and
subscripts, and have it call writeOutput on.exit().  I suspect you
could get something like that to work even in batch mode by having R
load the function by default and some tweaking of your scripts.

>
> Best,
> Ralf
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>

-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list