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. # # 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? Best, Ralf
Joshua Wiley
2010-Oct-06 15:19 UTC
[R] Inheritance and automatic function call on script exit
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/
> 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.What if I do not control the subscripts but only the superscript. In other words, other people keep adding subscripts and the function of my superscript only ensures certain standard behaviors. Ralf> >> >> 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/ >
I think base:on.exit() will do the trick. Thank you :) Ralf On Wed, Oct 6, 2010 at 11:24 AM, Ralf B <ralf.bierig at gmail.com> wrote:>> 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. > > What if I do not control the subscripts but only the superscript. In > other words, other people keep adding subscripts and the function of > my superscript only ensures certain standard behaviors. > > Ralf > > > >> >>> >>> 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/ >> >
Here the modified script with what I learned from Joshua: # # superscript # output <- NULL writeOutput <- function() { processTime <- proc.time() outputFilename <- paste("C:/myOutput_", processTime[3], ".csv", sep="") write.csv(output, file = outputFilename) } on.exit(writeOutput, add=T) # # subscript # source("C:/superscript.R") output <- data.frame(a=c(1,2,3), b=c(4,5,6)) For some reason, the file is not created. So it seems not to do the call. What do I do wrong? Ralf