Packages in R

Packages are a method of adding new functionalities to R. However, they do not come installed in R. But there is a function for that. Additionally, once packages are installed, they must be loaded into R every session you need it. Packages come with additional functions that are not in the standard version of R. However, some packages even rewrite old functions once they are loaded, so be careful. However, once you close R, the old packages must be reloaded for your next session. This means that the original design of the function is left intact.

We will go over an example of using and getting familiar with packages for Windows and Mac users. But first we will go over everything in general. If you would prefer to watch this in a video format, click on the video below. Otherwise, read on!

 

Installing

Installing function is pretty straightforward if you are running a Windows or Mac. Simply use the function

install.packages(“PACKAGE_NAME_HERE”)

If you are using Linux, however, you must specify where you are downloading the package to. You must also use the library() command with the function name and its destination.

Loading

To load a package, simple use the command library() in this way

library(‘PACKAGE_NAME_HERE’)

It works the same way with Linux.

Documentation

To get more information about the packages you install, it is helpful to read the documentation for the package. All the official R packages can be found here. To find the documentation, simply locate the package of desire, and then click on it. You should be able to see some preliminary information about the package. But the reference manual will show you all the functions that are in that package.

Let us now see an example of installing, loading, and viewing the documentation of a package.

 

Example I

Install and load the abc package. View the official documentation.

Answer I

We provide the code for this example in steps for clarity.

  1. First we will install the package. Below we provide the code to do this step as well as the output once we hit enter in the R console.
install.packages('abc')
Figure 1
Figure 1
  1. We need to now pick from a list of servers from where you would like to download the package from. You want to pick one that is closest to you.
  2. Once you pick the desired location, it will start to download the package. Sometime you might have to type in additional commands such as yes or no (which might require you to type Y or N). However, it should be done relatively quickly. You should not have other applications running at this time as it will severely slow down this process. Here is a picture of what it looks like when it is done.
Figure 2
Figure 2
  1. Now we must load the package.
library('abc')
Figure 3
Figure 3
  1. Now that we have the package ready to go, we need to know how to use it! Click here, find the package abc.
  2. Click on abc.
Figure 4
Figure 4
  1. Scroll down to the park where it says documentation.
Figure 5
Figure 5
  1. Click on the .pdf file. You should now see the documentation.
Figure 6
Figure 6