R-alpha: autoload

Thomas Lumley thomas@biostat.washington.edu
Fri, 25 Jul 1997 15:53:34 -0700 (PDT)


You can use the new delay() function to implement autoloading of libraries
or source files.  Functions autosource() and autoload() follow, together
with examples.  autoload("coxph","survival4") creates a promise to
evaluate autoloader("coxph","survival4"), which in turn calls
library(survival4), removes the promise, and evaluates "coxph" in the
newly created environment.  I was somewhat surprised that it worked.

	-thomas


 autosource<- function (name, file) 
{
        newcall <- paste("delay(autosourcer(\"", name, "\",\"", file, 
                "\"))", sep = "")
        assign(name, parse(text = newcall), env = .GlobalEnv)
}
 autosourcer <- function (name, file) 
{
        eval(parse(text = paste("source(\"", file, "\")", sep = "")), 
                .GlobalEnv)
        eval(as.name(name), sys.parent())
}



 autoload<- function (name, file) 
{
        newcall <- paste("delay(autoloader(\"", name, "\",\"", file, 
                "\"))", sep = "")
        assign(name, parse(text = newcall), env = .GlobalEnv)
}
 autoloader <- function (name, file) 
{
	name<-paste(name,"",sep="")
	rm(list=name,envir=sys.frame(sys.parent()),inherits=TRUE)
        eval(parse(text = paste("library(\"", file, "\")", sep = "")), 
                .GlobalEnv)
        eval(as.name(name), pos.to.env(2))
}


autosource("gee",system.file("library","gee"))
autoload("coxph","survival4")
y<-rnorm(100)
x<-rnorm(100)
g<-sort(rep(1:10,10))
tt<-1:10
cc<-rep(1,10)
xx<-rnorm(10)
substitute(gee)
substitute(coxph)
gee(y~x,g)
coxph(Surv(tt,cc)~xx)
q("no")




=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-