Installing and Loading Packages

In R, you can load additional packages. These packages include functions that allow you to do all kinds of amazing things. You can use the function library() without any arguments to see what packages are currently installed on your computer. Some of these packages will already be loaded, but some are not. In RStudio, you can also just click on the Packages tab in the lower-right window to see which packages are install on your computer, and which are loaded. Loaded packages have a check to the left of the name of the package. Below I will guide you through installing and loading packages in R.

Installing Packages

If a package is not installed on your computer, then you need to install it. This means downloading the package and copying files to a folder on your computer where R can access them. R makes this very easy.

If you own your computer or have administration access, then it is a piece of cake. Just use the function install.packages() to install the package. Of course you need to know the name of package. Below is the code to install the ggplot2, which includes a bunch of functions to create all kinds of graphs. I have included a second argument called dependencies=T because I want R to also download and install any packages needed to run the ggplot2 package.

  install.packages("ggplot2", dependencies=T)

If you don't have administration access, for example you are using a school computer, then you need to also tell R where to copy the files for the package. This too is easy. The example below copies the files to your u drive, and should work on any school computer.

install.packages("ggplot2", lib="u:/", dependencies=T)

In RStudio, you can just go to the Packages tab in the lower-right window and click the Install button on the left side just below the tabs. You can then provide the information in the form that pops up. Don't forget to check the Install dependencies checkbox. If you are on a school computer, then make sure to set the path to your u drive in the Install to Library dropdown box.

Loading Packages

Now that your package is installed, you can load it and access the functions and help files in that package. There are two functions library() and require() that load a package into a session of R. If you have administration access then you just need to provide either library() or require() with the name of the package. If you don't have adminstration access then you need to provide a second argument with the location so that R can find the package.

#If you have admin access
library("ggplot2")
require("ggplot2")

#If you don't have admin access
#And install the package to your u drive
library("ggplot2", lib.loc="u:/")
require("ggplot2", lib.loc="u:/")

That is it. You can now use the ggplot2 package. There are thousands of packages that you can install and load (all free!). I will try and add a list of some useful packages soon.

Some useful packages

Below is a short list of packages that I use often.

plyr, dplyr, and tidyr
Helps store and manipulate data.frames.
readxl
Imports Excel files into R.
ggplot2
Creates graphs.
ggmap
Creates maps that display data.
RColorBrewer
Color palettes created by color scientists.
Cairo
Saves graphs as high-quality image or pdf files.
stringr
Manipulates strings.
rmarkdown
Creates documents that includes r code and graphics.
shiny
Creates interactive webpages.


mzinkgraf/BiometricsWWU documentation built on Dec. 9, 2023, 1:07 a.m.