A.10: R packages
( \newcommand{\kernel}{\mathrm{null}\,}\)
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)
cuts down on the amount of screen output during installation.
If you receive the following warning message,
Warning: package 'package.name' is not available (for R version 4.3.2)
it may be possible that the package has not yet become available, but 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, the answer is no. 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.
To check installed packages, just view the object installedpkgs
created earlier.
R packages used in Mike’s Biostatistics Book
list updated 12 August 2024