Code
install.packages("RStudio.prefs")
::use_RStudio_prefs(
RStudio.prefsshow_diagnostics_r = FALSE
)
Eliot McIntire
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.
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.
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.
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:
Go to Tools > Global Options > Code
Restart R after doing the above. Try the package installations again. If they fail again, see other solutions and try them.
Alternatively, you can:
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:
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.
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.
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.
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.
Try purging the cache with one or more of these:
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.
.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:For Linux, it is something like:
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:
When you install a package, ensure it was installed in the correct location.
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.
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.
For instance, SpaDES.project
functions and SpaDES.core::simInit()
internally use Require
to install package dependencies specified by the user or modules.↩︎