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
> Is it possible, inside .First.lib, to find out the> version number of the package that is being loaded? Berwin Turlach kindly informs me that installed.packages() will extract details of all installed packages. So if only one version of package "spatstat" has been installed, we can determine the version number by installed.packages()["spatstat", "Version"] But: if several versions of the same package are installed, the question remains how to determine which version is being loaded... A
On Thu, 27 Jan 2005, Adrian Baddeley wrote:> 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]"\n" not "^M", please, and readLines is better than scan here.> 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).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. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Thu, 27 Jan 2005, Prof Brian Ripley wrote:> On Thu, 27 Jan 2005, Adrian Baddeley wrote: > > > 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] > > "\n" not "^M", please, and readLines is better than scan here. > > > 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). > > It is better to use read.dcf or the installed description information in > package.rds. Take a look at how library() does this.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> > 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. > >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no
> From: Roger Bivand > > On Thu, 27 Jan 2005, Prof Brian Ripley wrote: > > > On Thu, 27 Jan 2005, Adrian Baddeley wrote: > > > > > 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] > > > > "\n" not "^M", please, and readLines is better than scan here. > > > > > 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). > > > > It is better to use read.dcf or the installed description > information in > > package.rds. Take a look at how library() does this. > > 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.This is how I do it in randomForest (using .onAttach instead of .First.Lib): .onAttach <- function(libname, pkgname) { RFver <- if (as.numeric(R.version$major) < 2 && as.numeric(R.version$minor) < 9.0) package.description("randomForest")["Version"] else packageDescription("randomForest")$Version cat(paste("randomForest", RFver), "\n") cat("Type rfNews() to see new features/changes/bug fixes.\n") } HTH, Andy> Roger > > > > > 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. > > > > > > -- > Roger Bivand > Economic Geography Section, Department of Economics, > Norwegian School of > Economics and Business Administration, Breiviksveien 40, > N-5045 Bergen, > Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 > e-mail: Roger.Bivand at nhh.no > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Thanks, Brian. So, to print the version number when 'mypackage' is loaded, .First.lib <- function(lib, pkg) { library.dynam("mypackage", pkg, lib) vvv <- read.dcf(file=system.file("DESCRIPTION", package="mypackage"), fields="Version") cat(paste("mypackage", vvv, "\n")) }
> From: Prof Brian Ripley > > On Thu, 27 Jan 2005, Liaw, Andy wrote: > > >> From: Roger Bivand > >> > >> On Thu, 27 Jan 2005, Prof Brian Ripley wrote: > >> > >>> On Thu, 27 Jan 2005, Adrian Baddeley wrote: > >>> > >>>> 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] > >>> > >>> "\n" not "^M", please, and readLines is better than scan here. > >>> > >>>> 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). > >>> > >>> It is better to use read.dcf or the installed description > >> information in > >>> package.rds. Take a look at how library() does this. > >> > >> 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. > > > > This is how I do it in randomForest (using .onAttach > instead of .First.Lib): > > > > .onAttach <- function(libname, pkgname) { > > RFver <- if (as.numeric(R.version$major) < 2 && > > as.numeric(R.version$minor) < 9.0) > > package.description("randomForest")["Version"] else > > packageDescription("randomForest")$Version > > cat(paste("randomForest", RFver), "\n") > > cat("Type rfNews() to see new features/changes/bug fixes.\n") > > } > > Please don't use functions from utils in such places without > explicitly > loading them from utils unless your package has an explicit > dependence on > utils (and randomForest does not). > > There was a good reason why I suggested what I did: you don't > need the > utils namespace for this.Thanks for the tip! Will remediate... Andy> -- > Brian D. Ripley, ripley at stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 > >
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:> 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger D. Peng http://www.biostat.jhsph.edu/~rpeng/