Supporting Statistical Analysis for Research

## 1.11 Packages

The packages which make up the core functions and commands of R are loaded when R is started. There are many packages which extend R's capabilities beyond the core. These extension packages need to be loaded in each R session before you can use the functions they contain. The functions in these extensions range from widely used functions to obscure functions used by only a small number of people.

A package needs to be installed on your computer before you can load it into your session. R and RStudio manage a library of packages that have been installed on your computer. Winstat has a number of common packages installed for you. The packages installed in your library can be seen in the packages tab.

To install a package on your computer, click the Install icon in the Package tab and then enter the package you want installed.

Example

1. Installing the tidyverse package.

The library() function is used to load a package into your R session. The name of the package to be loaded is the first parameter. There may be nothing displayed by this function. Other times the function will display infomation about what is loaded an any conflicts that exist.

library(tidyverse)

The tidyverse tells us that a number of packages are attached. Also, loading the tidyverse has caused a few conflicts with functions in other packages that were previously loaded. The functions of the tidyverse's are now available for use.

You may see code using require() instead of library(). The require() function is designed to be used as a conditional event. The result is that the script continues running even if the package did not load. (Yeah, the name of the function is really misleading.) This is an undesirable behavior and can cause issues when sharing your work. The library() function is the proper function to use to load a package and not require().