15

How can I get R to list its base install packages. Dirk gives a list HERE but how can I get R to tell me this information, that is the packages in src/library/?

getOption("defaultPackages") is close but only lists some of these packages.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • 2
    `getOption("defaultPackages")` is actually exactly what I was looking for, thanks! It's just the packages that R loads on startup. – DuckPyjamas Jun 17 '18 at 03:16
  • 1
    Be careful as `c(getOption("defaultPackages"), "base")` is what R loads on startup. Check my answer below. – polkas Jul 04 '21 at 19:14

3 Answers3

27
rownames(installed.packages(priority="base"))
 [1] "base"      "compiler"  "datasets"  "graphics"  "grDevices" "grid"     
 [7] "methods"   "parallel"  "splines"   "stats"     "stats4"    "tcltk"    
[13] "tools"     "utils"    
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
3

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
polkas
  • 3,797
  • 1
  • 12
  • 25
2

There might be a simpler method, but I think that this should do the trick:

installed.packages()[grep('^base$', installed.packages()[, 'Priority']), ]
jbaums
  • 27,115
  • 5
  • 79
  • 119