Fixing Package Dependency Issues

This article explains why sometimes Apps or Shiny Apps experience dependency version clashes and provides a quick fix for when that occurs.

Understanding the pre-built packages

All Workspace R consoles and Built-in Apps come with some pre-installed packages. Some of these are needed to run the R Console or Built-in App itself, whilst others are commonly used R packages. Adding these common packages as part of the workspace makes it easier and quicker for users to run their Shiny Apps and R scripts.

The full list of pre-installed packages can be found in the article "Installed R Packages".

Managing packages in the workspace

If an R package is not already pre-installed, it can be installed from an R console (preferred option) or a Shiny App. R packages that will be used inside a Built-in App should be installed from that App.

In this case, a folder with the name of the package is created under files/R/<-r-version>, where <r-version></r-version> is the version of R run by the R console, App or Shiny App the package is installed from. The same is done for all of the package dependencies.

So, for example, if we installed the abc package from a 4.0.2 R console, we would get the following new folders: image-20210719-125533.png

From then on, all mini-apps and R consoles that use the same R version in that workspace will be able to use the abc package and any of its dependencies, regardless of when the installation was done or which user or Shiny App did the original install.

Pros and Cons

This setup is very useful as it saves storage space and it avoids having to repeatedly pre-install the packages before running an App, Shiny App or a session in the R Console.

However, there may be situations when a Built-in App, Shiny App or R script returns an error because it requires a different version of a package that had been previously installed. The way to fix this will depend on which package is causing the issue.

Troubleshooting Installations

We recommend to follow these steps if you are experiencing a dependency conflict:

Before running an App or script for the first time

  1. Check whether the packages it needs, or any of their dependencies, have already been installed in the workspace. You can do this by running xap.check_installed(c('<package1>', '<package2>',...) ) </package2></package1> in the R console.

  2. If they already exist, and they are the right version, you can run your App or script with the existing packages.

  3. If you require a different version for one of those packages to run your Shiny App, or the package had not been previously installed, start a separate R Console of the same R version the Shiny App runs, and install the required package. Or, if the package is needed in RStudio, install the package in the console inside of RStudio.

If the conflict is with a pre-installed package for a Shiny App

You will need to unload the pre-installed package that is causing the conflict before using the correct one.

  1. From the Shiny App code, locate the script from where libraries are being loaded ( usually app.R, global.R, or ui.R )

  2. Just after the .libPaths() (if used) and before any library() calls, add the following lines of code:

if (packageVersion(<package-name>) != <required-version>) { unloadNamespace(<package-name>) devtools::install_version(</package-name><package-name>, <required-version>) library(<package-name>) } </package-name></required-version></package-name></required-version></package-name>

Where <package-name></package-name> is the name of the package that is experiencing the conflict, and <required-version></required-version> is the version you wish to use.

  1. If you get the error message such as namespace <package-name> is imported by <another-package> so cannot be unloaded </another-package></package-name> you will need to unload the second package’s namespace before unloading the first’s. So for example for:

image-20210719-134141.png

You should: unloadNamespace("dbplyr") unloadNamespace("dplyr")

If the conflict is with a package installed by another Shiny App or in a R Console session

All packages installed from an R console session or another Shiny App in the same workspace are saved into the files/R/<r-version></r-version> folder, where <r-version></r-version> is the version of R used by the console or Shiny App. Packages installed from the RStudio Built-in App is located in files/R/RStudio/<r-version></r-version>.

In this scenario, to solve a version conflict, you could consider installing your Shiny App dependencies in a separate location, adding that folder to the Shiny App’s .libPaths .

From an R console: devtools::install_version('<package-name>', version = '<version>', lib = '<new-location>')</new-location></version></package-name>

The <new-location></new-location> could be, for example, a subfolder in your Shiny App directory.

Then, in the Shiny App code, add .libPaths( c(<new-location>, .libPaths()) )</new-location> before the packages are loaded with either require or library.

In the future this scenario will be best addressed by turning the Shiny App into a containerised app. This functionality is under active development.

We are also actively working on improvements to how package management is handled in the Workspace, if you encounter any issues or have any feedback it would be greatly appreciated if you could send it to our service desk.

Updated on February 23, 2023

Was this article helpful?