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"
##  [7] "package:methods"   "Autoloads"
##  [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"
##  [9] "package:methods"   "Autoloads" 
## [11] "package:base"


Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 1995-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.