1.2 Packages

R is actually a set of software packages, and they are all kept in a central repository called the Comprehensive R Archive Network or CRAN for short.

There are two basic types of packages:

1.2.1 Base R

You get these when you install R from CRAN, and they are loaded automatically whenever you start up a new R session. They include all of the basic functions for loading, modifying and analyzing data. In theory, this is all you need to perform any kind of statistical analysis – if you want to write the R code for it yourself. But you don’t need to do that, because many people have contributed that type of code to the CRAN repository already, in the second type of package:

1.2.2 Contributed packages

These contain R code with functions that allow you to perform many types of analyses, without having to write that code yourself. The packages can be installed whenever you want to use them, and there are different ways to access them in an R session (that we’ll cover below). These packages, like R, are open-source and free.

CRAN currently lists over 13,000 contributed packages. Packages can also depend on other packages; the whole system is designed to work together. This open-source, integrated software ecosystem is one of the reasons that R is so widely used – you can almost always find what you need here, and researchers who develop new statistical methodology typically also publish a package on CRAN to provide public access, so the system is always growing.

1.2.3 Installing Packages

Installing packages from CRAN is very simple. If you find you need a (fictional) package called packagename, you install it with the function install.packages(), like so,

install.packages("packagename")

1.2.4 Using and Loading Packages

Once you have installed a package, there is one more step before you can use the functions it contains. Every time you restart R, you have to tell R which packages you want to access. The reason for this is if R always loads every package you install, R would eventually slow down and take an large amount of memory as you install more and more packages.

If we want to load an entire package, you can use the library() function. Here we load the (fictional) package packagename,

library(packagename)

If we only want to use one or two functions from a package, it is often more efficient to not use library, but instead to remind R which package the function comes from. Here we want to use the (fictional) function func() from the (fictional) packagename package,

packagename::func()

1.2.5 Packages Required for This Tutorial

We will make use of some common packages from CRAN as we work through the examples in this Tutorial. Please execute the following code to install all the required packages:

install.packages(c("tidyverse", 
                   "scatterD3", 
                   "DescTools", 
                   "GGally", 
                   "BSDA", 
                   "asbio"),
                 dependencies = TRUE)

(you could also install each package individually)

There is a nice tutorial on packages provided by DataCamp if you’d like to know more.
But the brief intro we have just given should be enough for you to complete the examples here and the Lab Assignments.