Skip to content

getting package version inside .First.lib

4 messages · Adrian Baddeley, Brian Ripley, Roger Bivand +1 more

#
Greetings - 

Is it possible, inside .First.lib,
to find out the version number of the package that is being loaded?

If only one version of the package has been installed,
we could scan the DESCRIPTION file, something like

.First.lib <- function(lib, pkg) {
    library.dynam("spatstat", pkg, lib)
    dfile <- system.file("DESCRIPTION", package="spatstat")
    ttt <- scan(dfile, what="", sep="^M", quiet=TRUE)[2]
    vvv <- strsplit(ttt," ")[[1]][2]
    cat("spatstat version number",vvv,"\n")
}

but even this does not seem very safe (it makes assumptions about the
format of the DESCRIPTION file).

Is there a better way?

thanks
Adrian Baddeley
#
On Thu, 27 Jan 2005, Adrian Baddeley wrote:

            
"\n" not "^M", please, and readLines is better than scan here.
It is better to use read.dcf or the installed description information in 
package.rds. Take a look at how library() does this.

Post R-2.0.0 you can assume the format is as library uses.

BTW: all installed.packages does is to read the descriptions of all the 
packages it finds, and in .First.lib you know the path to your package.
#
On Thu, 27 Jan 2005, Prof Brian Ripley wrote:

            
Or even packageDescription() in utils, which uses read.dcf() and should be
a way of making sure you get the version even if the underlying formatting
changes.

Roger

  
    
#
This is what I use for all my packages, which I believe handles multiple 
versions of the same package being installed:

.First.lib <- function(lib, pkg) {
     ver <- read.dcf(file.path(lib, pkg, "DESCRIPTION"), "Version")
     ver <- as.character(ver)
     ...
}

-roger
Adrian Baddeley wrote: