[R-pkg-devel] Question regarding listing base and recommended packages programmatically and efficiently

Mikael Jagan j@g@nmn2 @end|ng |rom gm@||@com
Thu Oct 12 17:32:24 CEST 2023


Maybe something like this:

     > isRecommendedPkg <- utils:::isBasePkg
     > body(isRecommendedPkg)[[c(3L, 3L)]] <- "recommended"
     > installed <- unique(list.files(.libPaths()))
     > installed[vapply(installed, isRecommendedPkg, NA)]
      [1] "KernSmooth" "MASS"       "Matrix"     "boot"       "class"
      [6] "cluster"    "codetools"  "foreign"    "lattice"    "mgcv"
     [11] "nlme"       "nnet"       "rpart"      "spatial"    "survival"

where in your package you would define isRecommendedPkg "manually".

Another (but quite undocumented and so maybe not "recommended" :-))
possibility is this:

     > mk <- file.path(R.home("share"), "make", "vars.mk")
     > pp <- sub("^.*= +", "", grep("^R_PKGS_RECOMMENDED", readLines(mk), value 
= TRUE))
     > sort(strsplit(pp, " ")[[1L]])
      [1] "KernSmooth" "MASS"       "Matrix"     "boot"       "class"
      [6] "cluster"    "codetools"  "foreign"    "lattice"    "mgcv"
     [11] "nlme"       "nnet"       "rpart"      "spatial"    "survival"

I grepped around and did not find variables in any base namespace containing
the names of these packages.  It wouldn't be too hard to define such variables
when R is configured/built, but maybe there are "reasons" to not do that ... ?

Mikael

 > It would be much faster (but slightly less reliable) to use
 > list.files(.libPaths()) to get the names of all installed packages, and
 > then filter them to the known list of base and recommended packages,
 > which changes very rarely.
 >
 > Duncan Murdoch
 >
 > On 12/10/2023 8:34 a.m., Tony Wilkes wrote:
 > > Dear all,
 > >
 > > In my R package that I'm developing, I use `installed.packages(priority = 
"base")` to programmatically get all core/base R packages (i.e. base, stats, 
etc.). And similarly, I use installed.packages(priority = "recommended")​` to 
programmatically get the recommended R packages (i.e. mgcv, lattice, etc.).
 > >
 > > However, CRAN has requested to not use `installed.packages()`, as it is 
slow. I fully get and agree with that assesment. I used installed.packages()​` 
anyway because I could not find a better, fool-proof alternative.
 > >
 > > Nonetheless, I was asked to change this code for optimalisation. So I would 
like to ask: how do I programmatically get all base/core R packages safely and 
efficiently, but without using `installed.packages()`? And the same question for 
recommended R packages. I have of course Googled it, and looked at R's 
documentation (though R's documentation is large, so it's easy to miss 
something); no solution found. So if any of you has a smart idea: I'm all ears.
 > >
 > > Thank you in advance.
 > >
 > > Kind regards,
 > >
 > > Tony.




More information about the R-package-devel mailing list