Packages

CSULB Intro to R

April 13, 2018

Packages

Terminology

Installing Packages

There are three main ways to install a package in R:

  1. Installing from CRAN: install a package directly from the repository
    • Using R studio: Tools > Install Packages...
    • From R console: install.packages("package name")
  2. Installing from source: first download the add-on R package and then type the following in your console:
    • install.packages("path_to_file", repos = NULL, type = "source")
  3. Installing a variety of sources using the devtools package (must have it installed first!)
    • Github: devtools::install_github("repo name")
    • URL: devtools::install_url("url")
    • Other methods available, too

Using Packages

There are two ways to access code and data from an installed package:

  1. Loading the package to the current R workspace with the library() function
    • library(package) loads all contents of package and makes them accessible until R restarts
    • This is the recommended way to use packages
  2. Directly referencing the code/data with the :: operator
    • Instead of loading an entire package, we can instead temporarily access its contents
    • package::name accesses name from package for a one-time use
    • Useful when you only need to use a single object once or twice

Install some packages from CRAN

Let’s install devtools, here, and car from CRAN using the console:

install.packages("devtools", dependencies = TRUE)
install.packages("here", dependencies = TRUE)
install.packages("car", dependencies = TRUE)

Using a Package

Now, let’s use devtools to install BRRR from Github (more info here).

Method 1:

library(devtools)  # load the devtools package
install_github("brooke-watson/BRRR")  # use the function install_github() to install BRRR

Method 2:

devtools::install_github("brooke-watson/BRRR")  # use the function install_github() from devtools to install BRRR

Play around with BRRR!

Helpful Resources

DAY 2 - April 27