Programmatically get list of base packages

rownames(installed.packages(priority="base"))
 [1] "base"      "compiler"  "datasets"  "graphics"  "grDevices" "grid"     
 [7] "methods"   "parallel"  "splines"   "stats"     "stats4"    "tcltk"    
[13] "tools"     "utils"    

TL;DR.

  • rownames(installed.packages(priority = "base")) all base packages for your R.Version().
  • c(getOption("defaultPackages"), "base") is what R loads on startup

installed.packages will return your currently installed packages.
Where base packages are always the same per certain R version (R.Version()). It is possible that this list will change in the future with a newer R version. E.g. As i remember parallel was added later than others R `parallel` package does not exist on CRAN?.

getOption("defaultPackages") is what R loads on startup although the base package is not counted.
I find out that sessionInfo()$basePkgs solution is more robust for startup packages as it contains a base package too. However sessionInfo()$basePkgs is relatively highly inefficient because it is a simple loop across all DESCRIPTION files.

microbenchmark::microbenchmark(sessionInfo()$basePkgs,
                               getOption("defaultPackages"))
Unit: nanoseconds
                         expr     min      lq       mean  median      uq      max neval
       sessionInfo()$basePkgs 6172017 6242209 6673759.42 6294546 6848292 16656578   100
 getOption("defaultPackages")     205     246     526.85     451     656     1722   100

Tags:

R