## 2.8 Attaching a Package

REVIEW We can attach a package to our current R session from our local library for added convenience. This will make the command available during this specific R session without the requirement to specify the package name each time we use the command. Attaching a package tells the R software to look within that package (and then to look within any other attached packages) when it needs to find out what the command should do (the definition of the command).

We can see which packages are currently attached using stringi::search(). The order in which they are listed here corresponds to the order in which R searches for the definition of a command.

##  [1] ".GlobalEnv"        "package:stats"
##  [3] "package:graphics"  "package:grDevices"
##  [5] "package:utils"     "package:datasets"
##  [9] "package:base"

Notice that a collection of packages are installed by default among a couple of other special objects (.GlobalEnv and Autoloads).

A package is attached using the base::library() command. The base::library() command takes an argument to identify the package= we wish to attach.

# Load packages from the local library into the R session.

library(package=dplyr)
library(package=rattle)

Running these two commands will affect the search path by placing these packages early within the path.

##  [1] ".GlobalEnv"        "package:rattle"
##  [3] "package:dplyr"     "package:stats"
##  [5] "package:graphics"  "package:grDevices"
##  [7] "package:utils"     "package:datasets"
## [11] "package:base"