10.4 Attaching versus loading

If a package is installed,

  • Loading makes a package available in memory, but not added to the search path
  • Attaching put package in the search path

Technically, library() loads then attach the package.

Four functions make a package available:

Throws error Returns FALSE
Load loadNamespace("x") requireNamespace("x", quietly = TRUE)
Attach library(x) require(x, quietly = TRUE)
  • library() great for data analysis or vignette
  • Use requireNamespace("x", quietly = TRUE) to specify different behavior based on a package being installed
  • Other two unlikely to be needed in generic situations

10.4.1 Whether to Import or Depend

  • Imports: package will be loaded
  • Depends: package will be attached
  • Favor Imports over Depends as Depends changes the global landscape