The functionality provided by a fresh install of R is only a small fraction of what is possible. In fact, we refer to what you get after your first install as base R. The extra functionality comes from add-ons available from developers. There are currently hundreds of these available from CRAN and many others shared via other repositories such as GitHub. However, because not everybody needs all available functionality, R instead makes different components available via packages. R makes it very easy to install packages from within R. For example, to install the dslabs package, which we use to share datasets and code related to this book, you would type:
install.packages("dslabs")
In RStudio, you can navigate to the Tools tab and select install
packages. We can then load the package into our R sessions using the
library
function:
library(dslabs)
As you go through this book, you will see that we load packages without
installing them. This is because once you install a package, it remains
installed and only needs to be loaded with library
. The package
remains loaded until we quit the R session. If you try to load a package
and get an error, it probably means you need to install it first.
We can install more than one package at once by feeding a character vector to this function:
install.packages(c("tidyverse", "dslabs"))
Note that installing tidyverse actually installs several packages.
This commonly occurs when a package has dependencies, or uses
functions from other packages. When you load a package using library
,
you also load its dependencies.
Once packages are installed, you can load them into R and you do not need to install them again, unless you install a fresh version of R. Remember packages are installed in R not RStudio.
It is helpful to keep a list of all the packages you need for your work in a script because if you need to perform a fresh install of R, you can re-install all your packages by simply running a script.
You can see all the packages you have installed using the following function:
installed.packages()