21.2: R Packages
- Page ID
- 57818
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)R is free software. In this case, that means two things: it costs nothing to obtain and you are able to modify it as you see fit. this modification can take the form of incorporating the code into an IDE. More frequently, it takes the form of creating packages that extend the basic functionality of R. As of this moment, there are 23,035 packages available at CRAN.
✦•················• ✦ •··················•✦
These packages can be found in a few places: CRAN is the usual place to locate them. However, there are several on GitHub. Also, many focused on bioinformatics and computational biology, can be found at Bioconductor.
Why the three repositories?
CRAN is the official repository for all R packages.
Bioconductor is a dedicated repository for R packages focused on these areas, offering a wide range of tools and pipelines for analyzing -omics data, such as genomics, transcriptomics, and proteomics. While Bioconductor is the main source for bioinformatics-related R packages, it is important to note that the broader Comprehensive R Archive Network (CRAN) also hosts many R packages, some of which may be relevant to biological data analysis.
Platforms like GitHub are frequently used by developers (like me) to host cutting-edge or in-development versions of R packages, including those for biological applications.
While the base R that you installed has many statistical features, it is important to learn how to extend that base R installation. One way to do this is to install and load packages.
This book relies heavily on these packages:
- KnoxStats
- car
- lawstat
- lmtest
- MASS
- quantreg
Note that this book is not a part of the so-called tidyverse. Perhaps a future edition will be, but not yet, not now.
Installing Packages...
... from CRAN
The overall scheme for installing packages is the same for each operating system. The specifics are slightly different to take advantage of what the operating systems allow. That scheme is
- Run the
install.packagesfunction - Select a place to download the package from
- ????
- Profit
For instance, if I want to install the car package, I would run the following code in the Console window
install.packages("car")
Running this will download the car package from the Internet (the mirror site you selected) and install it on your computer. [If you have already specified the mirror site, then step two will not happen.]
It will also download all packages that are needed to run the car functions (called "dependencies" because car depends on them). You can see what other packages are installed by watching the Console window.
... from GitHub
The idea is the same as above, but R needs to use the devtools package to install packages from GitHub.
1. If you do not have devtools already installed, run this in the Console:
install.packages("devtools")
If you do have devtools already installed, skip that step.
2. Now, run the following in the Console window
devtools::install_github("ojforsberg/KnoxStats")
A lot will happen, but you can just sit and watch it all happen.
Once the Console window is done doing its thing, the package is installed on your computer.
... from Bioconductor
While the Comprehensive R Archive Network (CRAN) hosts thousands of essential packages, some specialized tools — particularly for bioinformatics, genomic data analysis, and high-throughput sequencing — are developed and maintained through Bioconductor. This open-source project provides rigorously curated, well-documented packages tailored for biological data science. Installing packages from Bioconductor requires a slightly different procedure than the standard install.packages() command used for CRAN.
The process has been streamlined through the BiocManager package. To begin, you must first install BiocManager from CRAN. Once installed, you can use it to install any package available in the Bioconductor repository. This method ensures that all necessary dependencies are correctly resolved and that you receive a version of the package compatible with your current version of R.
Below is the standard workflow. It is recommended to run these commands directly in your R console, not within a function or a larger script, especially the first time you set up Bioconductor.
# First, install the BiocManager package from CRAN
install.packages("BiocManager")
# Then, use BiocManager to install any desired Bioconductor package.
# Replace 'packagename' with the actual name of the package you need.
BiocManager::install("packagename")
# Example: To install the core 'limma' package for differential expression analysis:
BiocManager::install("limma")
When you run BiocManager::install() for the first time, you may be prompted to update some previously installed packages. You can typically proceed by typing y (for "yes") in the console. To install a specific version of a package or to explore the full catalog, visit the official Bioconductor website.
This method integrates Bioconductor's powerful, domain-specific tools seamlessly into your R workflow, greatly expanding your analytical capabilities for specialized statistical challenges.
Nothing in this course requires anything from Bioconductor. However, if you are interested in bioinformatics, I encourage you to read their website. There is a lot of information there that you should find interesting!
Loading Packages
The installation needs to be done just once for your computer. However, if you need to use the package in a script, you will need to load it into the working memory. Since this is something tied to the script, you should have this line in your script if you are using the car package:
library("car")
Once that line is run, R is able to access all functions (and/or data) in the package. Usually, there are a lot of functions in a package. To see what is available in the package, run this in the Console window:
help(package="car")
After running this, a page will pop up showing all available functions and links to their help pages. This particular package has a lot of functions associated with it. Some other packages have just a couple. The number of functions depends on the purpose of the package.


