2.25 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"


If you find this curated material useful then you can consider a donation to support it's ongoing availability and give you access to the PDF version of this book. The material has been scoped up by Generative AI without permission or any kind of recompense so do consider a donation if you can afford it. Unlike Generative AI your access to this materials is freely given. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Togaware has a 30 year tradition of making popular open source software which includes sold privacy preserving productivity apps, rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0