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 ISLR2 package, which is used a lot in the book, you would type:

install.packages("ISLR2")

You will then be prompted to choose a mirror (best option is to choose the mirror located in Ghent, Belgium).

Alternatively, 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(ISLR2)

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.

Note that installing certain packages might actually install 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()

Note: if you are working on R-Studio via Athena, then you never have to install packages, as all R packages are installed already on the Athena server. However, we strongly advise you to install R and RStudio locally on your PC instead of using Athena.