2.3 Increasing functionality with packages
One awesome versatility with R is the ability to add packages - these packages extend the functionality of R with contributed, specialized code. These are similar to apps on your phone. You can get packages from a few different places:
- CRAN, which stands for Comprehensive R Archive Network. This is the clearing house for many contributed packages - and allows for easy cross platform functonality.
One key package is tidyverse
, which is actually a collection of packages. If you take an introductory data science course you will most likely be learning more about this package, but to install this at the command line you type the following:
install.packages("tidyverse")
Typing this line will connect to the CRAN download mirrors and install this set of packages locally to your computer. It make take some time, but be patient.
Another package you should install is devtools
:
install.packages("devtools")
Sometimes when you are installing packages you may be prompted to install additional packages. In this case just say yes.
- Github. This is another place where people can share code and packages (including myself!). The code here has not been vetted through CRAN for compatibility, but if you trust the person sharing the code, it should work.
For this textbook I have written a collection of functions and data that we will use. This package name is called demodelr
(Differential Equations and Models in R). To install this package you will run the following line.
::install_github("jmzobitz/demodelr", build = TRUE, build_opts = c("--no-resave-data", "--no-manual"), force = TRUE) devtools
The start of this command devtools::
calls the function install_github
from the devtools
library. This is super handy when you just want to call one function from a library. What this command will do is pull in the package structure from my github page and install it locally.
Here is the good news: you only need to install a package once before using it! To load the package up into your workspace you use the command library
:
library(tidyverse)
library(demodelr)
You need to load up your these libraries each time you restart your R session. This is part of the benefit of a script file - at the start I always declare the libraries that I will need at the start of the script file, as shown in the following figure:
Also notice here that the first few lines of the script file I used comments (prefaced with #
) to denote the basic purpose of the file, who wrote it, and the date it was last revised. This type of information is good programming practice at the start.