library(cmu202)

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.align = "center",
  warning = FALSE,
  message = FALSE,
  echo = FALSE,
  fig.width = 7,
  height = 6
)

Packages

The functionality of R is bolstered by user-developed segments of code known as packages. While base R is installed with 15 required packages, thousands of packages are created each year to streamline complicated processes in a reproducible manner. In a package the author will create functions, blocks of code that perform a specific task for a certain input. Such functions remove the burden of calculation from the user -- allowing long calculations such as least squares regression to be done in a matter of seconds with no programming knowledge. Packages also contain data sets -- as such data is now stored in a single location.

Installation and Loading

One can install R packages to their computer with a single call, after which the package may be loaded for use at any time thereafter. One must load each installed package with the library() command to then use the package. This mechanism is designed to not create a memory burden on the computer -- as the package will then take up space in the environment after being loaded.

To summarize:

Package must only be installed once. For example:

# This example is incredibly bad style, as you would always install a package in the terminal

install.packages("ggplot2")

To load ggplot2 in a new R session, one must run (at the start of each file that uses the package):

library(ggplot2)

Typically most library() calls are performed at the top of each .R or .Rmd file. For an .Rmd file, the library() calls are performed in the first code chunk -- making them available for all following code chunks.

One must load each needed package at the start of every file

Package Management

To view the installed packages, one may explore the "packages" tab in the lower-right pane. From this location the user may view documentation, install new packages and remove packages. For new users, this interface is highly recommended for the installation of packages.



frank113/cmu202 documentation built on July 17, 2020, 9:31 p.m.