3  Troubleshooting R package installation

Author

Eliot McIntire

Published

July 5, 2024

It is common for errors to occur when installing packages in R. The following are issues that we commonly face and their solutions.

In all cases, it is likely that one solution will not totally resolve the issue. More than one may be required.

3.1 General diagnosing tools

The following help diagnose many of the issues mentioned below.

  • loadedNamespaces() shows which packages are actually loaded

  • sessionInfo() shows several other things as well, including package versions. Very useful when called after restarting R to see if there are any packages being pre-loaded

  • packageVersion("<packagename>") shows which version of the package your session currently has access to (already loaded or on disk in the library but not yet loaded). It might not necessarily correspond to the installed version.

3.2 Can’t install a package

3.2.1 Package has already been loaded

Some packages have files that cannot be deleted if they are being used in the current or another R session. This is primarily true for packages with compiled code. This will create several cascading problems, even after restarting the current R session.

3.2.1.1 Potential solutions

  • Restart R – The number one solution for many problems is to restart R. If using RStudio, the keyboard shortcut is commonly CTRL-SHIFT-F10.

  • Close other R or RStudio sessions – If you have more than one session open, each one may have packages loaded. That means you can’t install (update), delete or uninstall a package that has already been loaded elsewhere.

  • Close RStudio and install in a separate R session – By default, RStudio pre-loads packages before user input. This is convenient under some conditions, but is very difficult to deal with when it doesn’t work correctly – e.g. when these packages need to be updated. You may have to close RStudio, open a separate R session (without RStudio) and install the packages there.

  • Update packages in user library – Package versions may collide between the user library and the project library. Try to update the packages in the user library. To determine where this is, you can try:

    userLib <- Sys.getenv("R_LIBS_USER")

    then update the packages there:

    update.packages(userLib, ask = FALSE)

  • Turn off R diagnostics – Alternatively (or in complement) to the solution above, you can turn off R code diagnostics (temporarily or permanently), in your RStudio’s Global Options. This can greatly reduce the number of pre-loaded packages at the expense of losing some RStudio functionality. Via RStudio GUI:

  1. Go to Tools > Global Options > Code

    1. Open the Diagnostics tab, and uncheck “Show diagnostics for R”

    Restart R after doing the above. Try the package installations again. If they fail again, see other solutions and try them.

    Turning of R diagnostics with RStudio – a potential solution to prevent RStudio from pre-loading several R packages.

    Alternatively, you can:

    Code
    install.packages("RStudio.prefs")
    RStudio.prefs::use_RStudio_prefs(
    show_diagnostics_r = FALSE
    )
  • Turn off previous R session reloading – If R is reloading a previously saved session (loading an .Rdata file) this may also be loading the packages attached to this previous session. You can turn off this feature off (temporarily or permanently), in your RStudio’s Global Options. Via the RStudio GUI:

    1. Go to Tools > Global Options > General and uncheck “Restore .RData into workspace at startup:`

    Turning of restoring previous R session with RStudio – a potential solution to prevent R from loading R packages used in a previous session.

3.2.2 00LOCK folder issues

Once a package installation failure happened and interrupted the process, there may be a 00LOCK folder that R cannot remove, nor overwrite. R can’t install anything until that 00LOCK folder is removed.

3.2.2.1 Potential solutions

  • Delete 00LOCK folder(s) – The error message of the package installation failure will indicate if this is the issue and tell you where the 00LOCK folder is located. Make sure all other R sessions are closed and that the current session is restarted (and possibly closed), before deleting the 00LOCK folder. Then try installing the package again. If you can’t delete it, ensure RStudio and R are completely closed and possibly other software such as Git version control software (e.g. GitKraken, Bitbucket).

3.2.3 Missing dependencies

Although this happens less and less because functions like install.packages now default to installing package dependencies, it can still happen that one of the target package’s dependencies is missing or failed to install. The error message will indicate which one, but bear in mind that when installing many packages/dependencies, the reason why the dependency wasn’t installed may be buried in previous messages and warnings. So read the installation output carefully.

3.2.3.1 Potential solutions

The solution will depend on the reason why the dependency package failed to install. See solutions in Package has already been loaded and 00LOCK folder issues.

3.2.4 Package version cannot be met or found

When Require is used to install packages1, it uses various caching mechanisms. If a package can’t be found, or there is an unexplainable installation failure clearing the package cache may fix it.

3.2.4.1 Potential solutions

Try purging the cache with one or more of these:

Code
## Can't find the package remotely
Require::purgeCache()

## Can't install a particular package, e.g., ggplot2
Require::clearRequirePackageCache(packages = "ggplot2")

## The above didn't work, clear ALL packages from local cache
Require::purgeCache(packages = TRUE)

3.3 Installed a package, but it isn’t showing up

3.3.1 You installed it to a different library

As people migrate to using “project” libraries, i.e., a unique R package library for each project, sometimes, the user library (the one that you get by default with a normal R installation) can cause problems.

3.3.1.1 Potential solutions

  • Become aware of user vs. project library – .libPaths() is your friend. It tells you what folder the current R session is using as a package library. The default place for a personal library in R depends on operating system. For windows, it is something like:
Code
> Sys.getenv("R_LIBS_USER") [1] "C:\\Users\\emcintir\\AppData\\Local/R/win-library/4.4"}

For Linux, it is something like:

Code
> Sys.getenv("R_LIBS_USER") [1] "/home/emcintir/R/x86_64-pc-linux-gnu-library/4.3"}

If you have a project-specific library, including if you use SpaDES.project or renv, .libPaths() will point to somewhere else. For SpaDES.project it will be something like:

Code
# Windows 
> .libPaths() [1] "C:/Users/emcintir/AppData/Roaming/R/data/R/SpaDES.project/packages/x86_64-w64-mingw32/4.4"  

# Linux 
> .libPaths() [1] "/home/emcintir/.local/share/R/SpaDES.project/packages/x86_64-pc-linux-gnu/4.3"}

When you install a package, ensure it was installed in the correct location.

3.3.2 Package loaded in RAM collides with package on disk

If you previously loaded a package via e.g., library(ggplot2), then you happen to update it to a newer version, the newer version will be on disk in your library, but packageVersion("ggplot2") will show the older one. Restart R and it will be solved.

3.4 See also

An alternative way to address package management is using the renv package. Some people have great success with this. Some of us find it not very amenable to modular workflows.


  1. For instance, SpaDES.project functions and SpaDES.core::simInit() internally use Require to install package dependencies specified by the user or modules.↩︎