R packages

This page describes basic steps for package installation from a CRAN mirror site 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.

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 github or Bioconductor).

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

packagechapter
Rcmdr1.1 – A quick look at R and R Commander
RcmdrMisc3.2 - Measures of Central Tendency
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.survival20.9 - Survival analysis
cholera2.3 - A brief history of (bio)statistics
psych3.2 - Measures of Central Tendency
modeest3.2 - Measures of Central Tendency
HistData3.2 - Measures of Central Tendency
NCStats3.3 - Measures of dispersion
shotGrous3.5 - Statistics of error
Rmisc3.5 - Statistics of error
carData4.1 - Bar (column) charts
gplot4.1 - Bar (column) charts
ggplot4.1 - Bar (column) charts
plyr4.1 - Bar (column) charts
car4.3 - Box plot
tidyverse4.3 - Box plot
datasets4.5 - Scatter plots
reshape24.6 - Adding a second Y axis
Ternary4.8 - Ternary plots
lattice4.10 - Graph software
BiodiversityR5.6 - Sampling from Populations
gtools6.3 - Combinations and permutations
combinat6.3 - Combinations and permutations
random6.6 - Continuous distributions
propCIs7.6 - Confidence intervals
clipr4 – How to report statistics
BSDA11.4 - Two sample effect size
effsize11.4 - Two sample effect size
pwr11.5 - Power analysis in R
digitize12.3 - Fixed effects, random effects, and ICC
HH12.4 - ANOVA from "sufficient statistics"
MASS12.4 - ANOVA from "sufficient statistics"
multcomp12.6 - ANOVA posthoc tests
correlation16.6 - Similarity and Distance
rgl18.1 - Multiple Linear Regression
bootstrap19.1 - Jackknife sampling
lmboot19.1 - Jackknife sampling
boot19.2 - Bootstrap sampling
confintr19.2 - Bootstrap sampling
MESS20.1 - Area under the curve
peakDetection20.2 - Peak detection
findpeaks20.2 - Peak detection
baseline20.3 - Baseline correction
forecast20.5 - Time series
vegan20.8 - Diversity indexes
drc20.10 - Growth equations and dose response calculations
ape20.11 - Plot a Newick tree
Phylotools20.11 - Plot a Newick tree
Phytools20.11 - Plot a Newick tree
tanggle20.11 - Plot a Newick tree
ggtree20.11 - Plot a Newick tree
Bioconductor20.11 - Plot a Newick tree