R packages

This page describes basic steps for package installation from a CRAN mirror site, how to load and unload a package and how to update installed packages following installation of a new version of R. See at the end of this page for a list of packages described in Mike’s Biostatistics Book.

Adding packages to base R installation

Installing R packages is straightforward, assuming the package is part of CRAN. Select a CRAN mirror site, e.g., 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, e.g., ~/R-4.3.1/doc.

Once the CRAN mirror is selected, and assuming you have the name of the package, e.g., 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.

Start, stop, or remove package

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

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, modified from R-Bloggers.com. This script works on any operating system, but updates only CRAN packages (e.g., 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: to check installed packages, just view the object installedpkgs created earlier.

R packages used in Mike’s Biostatistics Book

list updated 12 August 2024

packagechapter
agRee16.5 – Instrument reliability and validity
ape20.11 - Plot a Newick tree
baseline20.3 - Baseline correction
BiocManager20.11 - Plot a Newick tree
Bioconductor20.11 - Plot a Newick tree
BiodiversityR5.6 - Sampling from Populations
boot19.2 - Bootstrap sampling
bootstrap19.1 - Jackknife sampling
BSDA11.4 - Two sample effect size
cairoDevice13.3 – Test assumption of normality
car4.3 - Box plot
carData4.1 - Bar (column) charts
cholera2.3 - A brief history of (bio)statistics
clipr4 – How to report statistics
combinat6.3 - Combinations and permutations
confintr19.2 - Bootstrap sampling
contingencytables9.6 – McNemar’s test
correlation16.6 - Similarity and Distance
cranlogs2.2 – Why do we use R Software?
datasets4.5 - Scatter plots
digitize12.3 - Fixed effects, random effects, and ICC
drc20.10 - Growth equations and dose response calculations
effectsize12.5 – Effect size for ANOVA
effsize11.4 - Two sample effect size
epiR5.4 - Clinical trials
epitools7.4 – Epidemiology: Relative risk and absolute risk, explained
exact2x29.6 – McNemar’s test
factoextra20.6 – Dimensional analysis
findpeaks20.2 - Peak detection
forecast20.5 - Time series
geepack20.1 - Area under the curve
geeM20.1 - Area under the curve
geodist16.6 - Similarity and Distance
ggplot24.1 - Bar (column) charts
ggtree20.11 - Plot a Newick tree
gplots4.1 - Bar (column) charts
gtools6.3 - Combinations and permutations
GrapheR 4.10 - Graph software
HH12.4 - ANOVA from "sufficient statistics"
HistData3.2 - Measures of Central Tendency
lattice4.10 - Graph software
lmboot19.1 - Jackknife sampling
irr12.3 - Fixed effects, random effects, and ICC
MASS12.4 - ANOVA from "sufficient statistics"
Matrix20.1 - Area under the curve
mcp12.6 - ANOVA posthoc tests
MESS20.1 - Area under the curve
mlr3misc 8.2 – The controversy over proper hypothesis testing
modeest3.2 - Measures of Central Tendency
multcomp12.6 - ANOVA posthoc tests
NCStats3.3 - Measures of dispersion
nlopt20.10 - Growth equations and dose response calculations
nortest13.3 – Test assumption of normality
PairedData10.3 – Paired t-test
peakDetection20.2 - Peak detection
Phylotools20.11 - Plot a Newick tree
Phytools20.11 - Plot a Newick tree
plotly4.10 - Graph software
plyr4.1 - Bar (column) charts
polychor16.4 – Spearman and other correlations
propCIs7.6 - Confidence intervals
psa20.6 – Dimensional analysis
psy12.3 - Fixed effects, random effects, and ICC
psych3.2 - Measures of Central Tendency
pwr11.5 - Power analysis in R
random6.6 - Continuous distributions
rattle13.3 – Test assumption of normality
Rcmdr1.1 – A quick look at R and R Commander
RcmdrMisc1.1 – A quick look at R and R Commander
RcmdrPlugin.EBM4.4 - Mosaic plots
RcmdrPlugin.EZR11.5 - Power analysis in R
RcmdrPlugin.HH12.4 - ANOVA from "sufficient statistics">/a>
RcmdrPlugin.KMggplot24.1 - Bar (column) charts
RcmdrPlugin.mosaic4.4 - Mosaic plots
RcmdrPlugin.survival20.9 - Survival analysis
Rcolorbrewer 4.4 - Mosaic plots
reshape24.6 - Adding a second Y axis
rgl18.1 - Multiple Linear Regression
Rmisc3.5 - Statistics of error
ROCR20.1 - Area under the curve
rptR12.3 - Fixed effects, random effects, and ICC
RGtk213.3 – Test assumption of normality
season20.5 – Time series
shotGroups3.5 - Statistics of error
stats4 – How to report statistics
survival3.1 - Data types
tanggle20.11 - Plot a Newick tree
Ternary4.8 - Ternary plots
testequavar13.4 – Tests for Equal Variances
tidyverse4.3 - Box plot
tigerstats8.4 – Tails of a test
timeseries20.5 – Time series
TOSTER16.1 – Product moment correlation
vegan20.8 - Diversity indexes
WRS23.3 – Measures of dispersion