I see the following idiom in the .First.lib function in many R packages:
fullName <- paste("package", pkgname, sep=":") myEnv <- as.environment(match(fullName, search())) barepackage <- sub("([^-]+)_.*", "\\1", pkgname) dbbase <- file.path(libname, pkgname, "R", barepackage) rm(.First.lib, envir = myEnv) lazyLoad(dbbase, myEnv) if(exists(".First.lib", envir = myEnv, inherits = FALSE)) { f <- get(".First.lib", envir = myEnv, inherits = FALSE) if(is.function(f)) f(libname, pkgname) else stop(gettextf("package '%s' has a non-function '.First.lib'", pkgname), domain = NA) }
I understand that the .First.lib function starts when the package loads.
I understand that the above code defines the environment for the package and sets the path, but I don't understand why it is looking for the .First.lib function after it explicitly removes the .First.lib function. What makes the above idiom so common? Is it "best practice" to include this in package R?
source share