Tyler Rinker
Tyler Rinker

Reputation: 109894

Programmatically get list of base packages

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.

Upvotes: 17

Views: 2582

Answers (3)

polkas
polkas

Reputation: 4184

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

Upvotes: 4

Josh O'Brien
Josh O'Brien

Reputation: 162371

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

Upvotes: 29

jbaums
jbaums

Reputation: 27388

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

installed.packages()[grep('^base$', installed.packages()[, 'Priority']), ]

Upvotes: 2

Related Questions