R packages
What’s on this page?
- How to add packages to base R installation.
- How to select a CRAN mirror site.
- Steps for package installation from selected CRAN mirror site.
- How to load and unload a package.
- How to update installed packages following an upgrade of R.
- List of R packages used in the Mike’s Biostatistics Book.
The instructions on this page chiefly apply to an installation of R on a local computer, Linux, macOS, or winPC. Running R in the cloud (eg, Google CoLab) also requires installing and loading packages, but many of the other instructions on this page do not apply. Look for the CoLab, skip this step: statement; instructions lacking this disclaimer should apply to CoLab. After a skip this step: statement, I’ll start the new section with Applies to all.
Add packages to base R installation
CoLab, skip this step: it is not necessary to set a mirror site for Google CoLab.
Installing R packages is straightforward, assuming the package is part of CRAN. Select a CRAN mirror site, eg, 0-Cloud, RStudio’s mirror site.
chooseCRANmirror()
To find out what CRAN mirror was set for the current session use
findCRANmirror()
A list of mirror sites is stored on your computer once R is installed, see CRAN_mirrors.csv in the doc folder, eg, ~/R-4.3.1/doc.
Applies to all. Once the CRAN mirror is selected, and assuming you have the name of the package, eg, package.name, then
install.packages("package.name")
will work.
Useful additional command options include
install.packages("package.name", dependencies=TRUE)
which will also download and install any additional packages required. And
install.packages("package.name", quiet=TRUE)
which cuts down on the amount of screen output during installation.
If you receive the warning message
Warning: package 'package.name' is not available (for R version 4.3.2)
While it is possible the package has not yet become available, first double-check for typos.
Another warning message may be that a binary version is available, but a more recent source version is available, prompted by the question,
Do you want to install from sources the package which needs compilation?
In most cases, no is the answer. R will install a previous binary version. In order to install from source, RTools must be installed.
Note 1: Do not install RTools if running R in the cloud (eg, Google CoLab). The R environment on CoLab is already set.
RTools contains software tools needed to build some R packages on WinPC.
Start, stop, or remove package
Applies to all. library("package name") is used to start a package. If a package is to be called in a function, then use require("package name"). If the called package is not installed, library() will exit with an error message whereas the function will continue to run if require() is used.
To unload a package without stopping current R session, try detach("package name") or unloadNamespace("package name"). The command remove.packages("package name") will uninstall a package from R.
Update R packages after installing new R version
CoLab, skip this step: After updating to new version of R you’ll need to download and update the user installed packages again. If you are running RStudio, see instructions here. For Win11 users you can download and run a package called installr, for macOS users download and install updateR, which will assist you to update R packages.
I prefer to run a script I modified from R-Bloggers.com. This script works on any operating system, but updates only CRAN packages (eg, not devtools github or Bioconductor packages). For github, try . dtupdate. For Bioconductor, see BiocManager::install() ).
Before installing the new version of base R, start up your current R installation and set your working directory, setwd(). Enter the following script to gather and save all installed R packages. Select CRAN mirror when prompted.
tmp <- installed.packages() installedpkgs <- as.vector(tmp[is.na(tmp[,"Priority"]), 1]) save(installedpkgs, file="installed_old.rda")
Shutdown R, then install and start the new version of R (see Install R for help).
In the new version of R, set your working directory as above. Enter the following script
load(file="installed_old.rda") tmp <- installed.packages() installedpkgs.new <- as.vector(tmp[is.na(tmp[,"Priority"]), 1]) missing <- setdiff(installedpkgs, installedpkgs.new) install.packages(missing) update.packages(ask=FALSE)
Should be good to go. You can remove old R version installation.
Note 2: to check installed packages, just view the object installedpkgs created earlier.
R packages used in Mike’s Biostatistics Book
list updated 12 August 2024
| package | chapter |
|---|---|
| agRee | 16.5 – Instrument reliability and validity |
| ape | 20.11 - Plot a Newick tree |
| baseline | 20.3 - Baseline correction |
| BiocManager | 20.11 - Plot a Newick tree |
| Bioconductor | 20.11 - Plot a Newick tree |
| BiodiversityR | 5.6 - Sampling from Populations |
| boot | 19.2 - Bootstrap sampling |
| bootstrap | 19.1 - Jackknife sampling |
| BSDA | 11.4 - Two sample effect size |
| cairoDevice | 13.3 – Test assumption of normality |
| car | 4.3 - Box plot |
| carData | 4.1 - Bar (column) charts |
| cholera | 2.3 - A brief history of (bio)statistics |
| clipr | 4 – How to report statistics |
| combinat | 6.3 - Combinations and permutations |
| confintr | 19.2 - Bootstrap sampling |
| contingencytables | 9.6 – McNemar’s test |
| correlation | 16.6 - Similarity and Distance |
| cranlogs | 2.2 – Why do we use R Software? |
| datasets | 4.5 - Scatter plots |
| digitize | 12.3 - Fixed effects, random effects, and ICC |
| drc | 20.10 - Growth equations and dose response calculations |
| effectsize | 12.5 – Effect size for ANOVA |
| effsize | 11.4 - Two sample effect size |
| epiR | 5.4 - Clinical trials |
| epitools | 7.4 – Epidemiology: Relative risk and absolute risk, explained |
| exact2x2 | 9.6 – McNemar’s test |
| factoextra | 20.6 – Dimensional analysis |
| findpeaks | 20.2 - Peak detection |
| forecast | 20.5 - Time series |
| geepack | 20.1 - Area under the curve |
| geeM | 20.1 - Area under the curve |
| geodist | 16.6 - Similarity and Distance |
| ggplot2 | 4.1 - Bar (column) charts |
| ggtree | 20.11 - Plot a Newick tree |
| gplots | 4.1 - Bar (column) charts |
| gtools | 6.3 - Combinations and permutations |
| GrapheR | 4.10 - Graph software |
| HH | 12.4 - ANOVA from "sufficient statistics" |
| HistData | 3.2 - Measures of Central Tendency |
| lattice | 4.10 - Graph software |
| lmboot | 19.1 - Jackknife sampling |
| irr | 12.3 - Fixed effects, random effects, and ICC |
| MASS | 12.4 - ANOVA from "sufficient statistics" |
| Matrix | 20.1 - Area under the curve |
| mcp | 12.6 - ANOVA posthoc tests |
| MESS | 20.1 - Area under the curve |
| mlr3misc | 8.2 – The controversy over proper hypothesis testing |
| modeest | 3.2 - Measures of Central Tendency |
| multcomp | 12.6 - ANOVA posthoc tests |
| NCStats | 3.3 - Measures of dispersion |
| nlopt | 20.10 - Growth equations and dose response calculations |
| nortest | 13.3 – Test assumption of normality |
| PairedData | 10.3 – Paired t-test |
| peakDetection | 20.2 - Peak detection |
| Phylotools | 20.11 - Plot a Newick tree |
| Phytools | 20.11 - Plot a Newick tree |
| plotly | 4.10 - Graph software |
| plyr | 4.1 - Bar (column) charts |
| polychor | 16.4 – Spearman and other correlations |
| propCIs | 7.6 - Confidence intervals |
| psa | 20.6 – Dimensional analysis |
| psy | 12.3 - Fixed effects, random effects, and ICC |
| psych | 3.2 - Measures of Central Tendency |
| pwr | 11.5 - Power analysis in R |
| random | 6.6 - Continuous distributions |
| rattle | 13.3 – Test assumption of normality |
| Rcmdr | 1.1 – A quick look at R and R Commander |
| RcmdrMisc | 1.1 – A quick look at R and R Commander |
| RcmdrPlugin.EBM | 4.4 - Mosaic plots |
| RcmdrPlugin.EZR | 11.5 - Power analysis in R |
| RcmdrPlugin.HH | 12.4 - ANOVA from "sufficient statistics">/a> |
| RcmdrPlugin.KMggplot2 | 4.1 - Bar (column) charts |
| RcmdrPlugin.mosaic | 4.4 - Mosaic plots |
| RcmdrPlugin.survival | 20.9 - Survival analysis |
| Rcolorbrewer | 4.4 - Mosaic plots |
| reshape2 | 4.6 - Adding a second Y axis |
| rgl | 18.1 - Multiple Linear Regression |
| Rmisc | 3.5 - Statistics of error |
| ROCR | 20.1 - Area under the curve |
| rptR | 12.3 - Fixed effects, random effects, and ICC |
| RGtk2 | 13.3 – Test assumption of normality |
| season | 20.5 – Time series |
| shotGroups | 3.5 - Statistics of error |
| stats | 4 – How to report statistics |
| survival | 3.1 - Data types |
| tanggle | 20.11 - Plot a Newick tree |
| Ternary | 4.8 - Ternary plots |
| testequavar | 13.4 – Tests for Equal Variances |
| tidyverse | 4.3 - Box plot |
| tigerstats | 8.4 – Tails of a test |
| timeseries | 20.5 – Time series |
| TOSTER | 16.1 – Product moment correlation |
| vegan | 20.8 - Diversity indexes |
| WRS2 | 3.3 – Measures of dispersion |