mod <- lm(mpg ~ am * wt, data = mtcars)
stargazer(mod, type = "html", out = "mod.doc")9 Using Packages
R is available as a series of modules called packages, a few of which were included when you initially installed R. With base R, you can fit basic statistic models and create acceptable plots, but if you want to do anything more complicated or better-looking, you will probably need to install other packages.
You will hear about new packages from resources like this one, from statistics classes, from journal articles (especially for packages that can perform cutting-edge analyses), and from colleagues who have discovered packages that simplify their workflow.
Packages can contain all sorts of objects, but generally they are sources of new functions, datasets, example scripts, and documentation.
Anyone can develop and submit a package to CRAN, the central repository. CRAN packages must meet certain benchmarks to be accepted and distributed. CRAN packages vary considerably in style and the quality of their documentation, even after meeting the CRAN benchmarks. Packages can also be installed from other sources, such as GitHub. See ?remotes::install_github.
9.1 Installing and Using Packages
There are two steps to using a package:
- Installing the package to your computer with
install.packages() - Making packages available to your current R session with
library()
Think of install.packages() like buying a book and placing it on your bookshelf, and library() like taking a book from your bookshelf and putting it on your desk.
Whenever you start R/RStudio, it starts with an empty desk, except for a few basic packages. Whenever you close R/RStudio, it takes all the books on your desk and puts them back on the bookshelf.
Do you need to buy a book every time you need to use it? Of course not. Once you own it, you just need to grab it from your shelf. Similarly, once you have installed a package once with install.packages(), you do not need to reinstall it every time you need it. Simply load it for your current session with library().
Try these steps in this order on your own computer:
- Try to use the
beep()function with the argument2:beep(2). This will fail because the package is not loaded. - Try to load the
beeprpackage, which contains thebeep()function, withlibrary(beepr). Presumably, this will fail because you do not have thebeeprpackage installed. You cannot grab a book from your shelf if it is not there. - Install the
beeprpackage withinstall.packages("beepr"). (Note quotes are required around the package name during installation.) - Run
beep(2). This will fail because the package is installed but not loaded. - Load the
beeprpackage withlibrary(beepr). This code resulted in an error before, but it now works becausebeeprhas been installed. - Run
beep(2). Check the help page with?beepto find more sounds you can try. - Save your open script(s), quit RStudio, and then reopen RStudio.
- Try to use the
beep()function again. This will fail because R unloaded the package when RStudio was closed. - Load the
beeprpackage withlibrary(beepr). This will still work because the package is still installed. - Run
beep(2)to confirm it is loaded.
What did we learn? To use a function, you need to load the package the function is in. To load a package, you need to first install it. Once a package is installed, you can load it and use its functions.
On the SSCC’s servers, you will find that there are many packages already installed for you, and they are updated regularly. You can also install and update packages yourself.
To update R packages, click on the Packages pane in the bottom-right corner. Click on the Update button, and then Select All (or just some of them) and then Install Updates.
9.2 What packages are already installed?
If you are working in RStudio you can see the installed packages in the Packages pane, tabbed in the lower right of RStudio with Files, Plots, and Help. Alternatively, you can just run library(package_name) and see whether this returns an error. If there is no error, the package is already installed.
In the Packages pane, you can scroll through the list, or use the search box in the upper-right corner of the pane. Click on a package name to see a help page listing all of the functions and other objects in that package.
For example, suppose you were looking for documentation on a function to specify the number of cores you want to use for running R in parallel. If we already know it is in the parallel package, we could
- Open the Packages pane
- Search for or scroll down to
parallel - Scroll through the list of functions to find
makeCluster() - Click on the function name to read its help page
Alternatively, if we already knew the function name, we can search for a help page from the Console with the pattern ?function. If we do not already have the function’s package loaded, we can access it with ?package::function. For example, if we have not loaded parallel, we would run ?parallel::makeCluster. If parallel is already loaded, we could simply run ?makeCluster.
9.3 Exercises
Load the
parallelpackage, which has functions for parallel computation. Then rundetectCores(), which will tell you how many cores your computer has. (For more on running tasks in parallel, see Functions and Iteration in R, in particular the section on Parallelization in the chapter on Iteration.)Install the package
stargazer. This package contains a function of the same name,stargazer(), which can write tables of model results and summary statistics to a Word document. After installing and loadingstargazer, run this code, and take a look at the file it produces.