春江暮客

春江暮客的个人学习分享网站

How to Install Old Version R Packages in R

2022-03-15 Technology
How to Install Old Version R Packages in R

Why Install Old Version Packages

After upgrading R, one of the most common problems is that older packages have not yet caught up with the new release. The usual symptom is a package manager error telling you that the package is not available for your current R version.

This article records the most practical workaround: download the source package yourself and install it with R CMD INSTALL. It is a manual path, but on servers it is often the most reliable one.

To leverage the efficiency improvements and bug fixes of R version updates, I upgraded R on my server to the latest version (currently 4.1). However, when using some packages, I encountered errors like:

Warning message:
“package ‘clusterProfiler’ is not available for this version of R

or

Warning message:
“package ‘EnrichmentBrowser’ is not available for this version of R
Warning message:
“package ‘qvalue’ is not available for this version of R

These errors occur because clusterProfiler, EnrichmentBrowser, or qvalue have not kept pace with R’s updates. If you need to use them on a newer version, you must use special installation methods.

What to check before installing

Before manually installing a source package, verify three things first:

  1. Your current R version
  2. The source package version you plan to use
  3. Whether the system has a working build toolchain

Useful checks:

R --version
R CMD config CC
R CMD config CXX

If the server does not have a basic compiler toolchain, source installation usually fails halfway through.

Install from Source

Most of the time, this is the most stable method. Although direct installation via R commands isn’t possible, you can install by downloading the source code. For example, qvalue:

  1. First, download the installation package. Find the download link for the qvalue installation package on bioconductor at qvalue and download it:
    wget https://bioconductor.org/packages/release/bioc/src/contrib/qvalue_2.26.0.tar.gz
    
  2. Install Call R’s installation command directly in the shell:
    R CMD INSTALL qvalue_2.26.0.tar.gz
    
    If the tarball is already on disk and you prefer to stay inside an R session, this is the equivalent source-install form:
    install.packages("qvalue_2.26.0.tar.gz", repos = NULL, type = "source")
    
    If successful, you’ll see a correct installation prompt:
    * installing to library ‘/home/teng/R/x86_64-pc-linux-gnu-library/4.1’
    * installing *source* package ‘qvalue’ ...
    ** using staged installation
    ** R
    ** data
    ** inst
    ** byte-compile and prepare package for lazy loading
    ** help
    *** installing help indices
    ** building package indices
    ** installing vignettes
    ** testing if installed package can be loaded from temporary location
    ** testing if installed package can be loaded from final location
    ** testing if installed package keeps a record of temporary installation path
    * DONE (qvalue)
    
    qvalue is successfully installed.

Similarly, you can install other dependencies one by one. Finally, clusterProfiler is installed:

R CMD INSTALL   clusterProfiler_4.2.2.tar.gz
* installing to library ‘/home/teng/R/x86_64-pc-linux-gnu-library/4.1’
* installing *source* package ‘clusterProfiler’ ...
** using staged installation
** R
** data
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (clusterProfiler)

If a dependency is missing, install the dependency source packages first and then return to the target package.

How to verify the installation

Do not stop at the DONE line. It is better to load the package and inspect the active session:

R
library(qvalue)
library(clusterProfiler)
sessionInfo()

If library() loads successfully, the package is usually installed correctly in the current R environment.

Common issues

1. Installation fails because the compiler or system libraries are missing

This is the most common source-install failure. The package tarball downloads correctly, but compilation fails because gcc, g++, make, or a required system development library is missing.

2. The target package fails because a dependency was not installed

For packages like clusterProfiler, the real problem is often a dependency rather than the target package itself. The most reliable approach is to install dependencies one by one in the order shown by the error messages.

3. The package was installed, but R still cannot find it

Different R versions often use different library paths. Check the active library search path with:

R -q -e '.libPaths()'

This helps avoid the common mistake of installing into one R library path and loading from another.

Summary

This article describes how to install various packages on the latest version of R. Unlike Python, R’s installation method requires many dependencies for the latest version. This time, I downloaded dozens of source codes and installed them one by one. If you don’t want to go through this hassle, it’s better to stick with older R versions. R 3.5 and 3.6 are currently the most convenient for installing various packages, and I recommend using them.

R

友情链接

其它