Chapter 10 Packages
R
packages are a collection of R
functions, complied code and sample data. They are stored under a directory called “library” in the R
environment. By default, R
installs a set of packages during installation. More packages are added later, when they are needed for some specific purpose. When we start the R
console, only the default packages are available by default. Other packages which are already installed have to be loaded explicitly to be used by the R
program that is going to use them.
All the packages available in R
language are listed at R Packages.
To see a list of all packages installed on your device.
To see a list of all packages that are currently loaded (note that yours will likely look different).
## [1] ".GlobalEnv" "package:knitr" "package:stats"
## [4] "package:graphics" "package:grDevices" "package:utils"
## [7] "package:datasets" "package:methods" "Autoloads"
## [10] "package:base"
When adding a new package to our library we only have to install it once. We can do so with the following command.
Alternatively, we can also go to the lower left hand window and select the Packages tab. Then hit the button Install. A dropdown menu will appear where we can search for the package name.
Before a package can be used in the code, it must be loaded to the current R environment. You also need to load a package that is already installed previously but not available in the current environment.
For example, suppose we wanted to install and load the package “ggplot2”, (a very popular package for making plots). We would type the following commands.
# Install package (only need to do this once)
install.packages("ggplot2")
# Load into working environment (need to do this for each
# new R session)
library("ggplot2")
It might seem strange to (1) have to download packages tto use features and (2) have to load these packages each time we wish to use them; however, there are several good reasons for doing packages in this way, and this system is considered a feature. We don’t have all possible packages available to us at all times because that is a lot of information that R would need to store at once which could make our computer lag. These packages are also always being updated at different rates with different features. Most users will only utilize packages in CRAN, so having to update R each session for features you may never use would be tedious. Furthermore, there are several packages which contain objects and functions with the same name.
10.1 Namespace Collisions
Every time we load a package into our environment the results for the search()
function changes. The most recently added package is always listed after the global environment, followed by the second most recent, and so on.
The search()
function tells us how R searches for an object that we called. For example, consider the following command.
## [1] 2
In this case R is searching for the function mean first in the global environment, and then in the package in the order that appears in the output of the search()
function. The package base
is always last. In our case the function mean()
is only defined in the base package. If you want to call a function from a specific package explicitly and want to be sure there is no confusion you can type the package name followed by ::
and the desired command.
## [1] 2
This can be useful, but is not usually necessary.