These functions can be used to recreate an existing library on a new installation of R.
The SavePackageDetails
function writes the details of installed packages to a file.
And the RecreateLibrary
function reads this file and downloads and installs any
‘missing’ packages from the Comprehensive R Archive Network (CRAN),
CRAN-like and local repositories, and GitHub.
RecreateLibrary(file = "R-packages.tsv", lib = .libPaths()[1],
repos = getOption("repos"), snapshot = FALSE, local = NULL,
versions = FALSE, github = FALSE, quiet = FALSE)SavePackageDetails(file = "R-packages.tsv", lib = .libPaths(), pkg = NULL)
'character'.
Name of the file for reading (or writing) the list of package details.
For a file name that does not contain an absolute path,
the name is assumed relative to the current working directory [see getwd()
command].
A .gz
file extension indicates the file is compressed by gzip.
'character'.
The library tree(s) to search through when locating installed packages (see .libPaths
),
or the library directory where to install packages.
'character'.
Vector of base URL(s) of the CRAN-like repositories (and including CRAN) to use when installing packages.
For example, the URL of the RStudio sponsored CRAN mirror is "https://cloud.r-project.org/"
.
And the URL of the Geological Survey R Archive Network (GRAN) is "https://owi.usgs.gov/R"
.
'logical', 'character', or 'Date'.
Calendar date for a CRAN snapshot in time,
see the Microsoft R Application Network
(MRAN) website for details.
If true, the snapshot date is read from the first line of file
.
A snapshot date can also be specified directly using the required date format, "%Y-%m-%d"
.
This argument masks all CRAN mirrors in repos
.
'character'.
Vector of paths to local repositories.
Packages are installed from local files in these directories.
Files can contain binary builds of packages (.zip
on Windows and .tgz
on macOS)
or be source packages (.tar.gz
).
'logical'.
If true, installed package versions will be identical to version numbers stored in file
.
Only applies to packages from CRAN-like repositories and local files.
Requires that the devtools package is available,
see install_version
function.
'logical'.
If true, an attempt is made to install a subset of packages from GitHub repositories.
Only applies to packages missing from the CRAN-like repositories (see repos
argument).
Requires that the githubinstall package is available,
see gh_install_packages
function.
Note that locating R packages hosted on GitHub using nothing but the package name can be difficult.
The user will be prompted with suggested repository names to identify the correct package to install.
An example of an R package that is only available on GitHub is AnomalyDetection,
located at twitter/AnomalyDetection.
Package vignettes are not built using this option.
'logical'. If true, reduce the amount of output.
'character'.
One or more names of packages located under lib
.
Only packages in pkg
, and the packages that pkg
depend on/link to/import/suggest,
are included in the package details file
.
The SavePackageDetails
function returns (invisibly) the MD5 checksum of the file
content.
Any changes in the file content will produce a different MD5 checksum.
Use the md5sum
function to verify that the file has not been tampered with.
The RecreateLibrary
function returns (invisibly) NULL
.
A typical workflow is as follows:
Run the SavePackageDetails()
command on an older version of R.
It will print to a text file a complete list of details for packages located under your current R library tree(s).
Uninstall the older version of R if no longer needed.
Then, on a freshly installed version of R with the inlmisc package available,
run the RecreateLibrary()
command.
It will download and install the packages listed in the package description file
.
The type of package to download and install from CRAN-like repositories is
binary on Windows and some macOS builds, and source on all others.
Package installation from a GitHub repository or local .tar.gz
file is always a source installation.
If a package is installed from source, and it contains code that needs compiling,
you must have a working development environment.
On Windows, install the Rtools collection
and have the PATH environment variable set up as required by Rtools.
On macOS, install Xcode from the Mac App Store.
And on Linux, install a compiler and various development libraries.
Daily snapshots of CRAN are stored on MRAN and available as far back as September 17, 2014.
Use the snapshot
argument to install older package versions from MRAN.
Note that newer versions of R may not be compatible with older versions of packages.
To avoid any package installation issues,
install the R version that was available from CRAN on the
snapshot date.
The package details file
is of the following format:
# Date modified: YYYY-MM-DD HH:MM:SS UTC # R version 9.9.9 (YYYY-MM-DD)
Package | Version |
name | 9.9.9 |
The format is flexible enough to add additional extraneous metadata and table fields. For example,
# Date modified: 2017-08-12 05:14:33 UTC # R version 3.4.1 (2017-06-30) # Running under: Windows 10 x64 (build 14393) # Platform: x86_64-w64-mingw32
Package | Version | Priority | Depends | Imports |
akima | 0.6-2 | NA | R (>= 2.0.0) | sp |
# NOT RUN {
# Run on old version of R
SavePackageDetails()
# }
# NOT RUN {
# Run on new version of R, and ensure 'inlmisc' package is available.
repos <- c(CRAN = "https://cloud.r-project.org/", GRAN = "https://owi.usgs.gov/R")
if (!requireNamespace("inlmisc", quietly = TRUE))
utils::install.packages("inlmisc", repos = repos["CRAN"], dependencies = TRUE)
inlmisc::RecreateLibrary(repos = repos, github = TRUE)
# }
# NOT RUN {
# Clean up example
unlink("R-packages.tsv")
# }
Run the code above in your browser using DataLab