Allowing different libraries for different apps on Shiny Server, Shiny Server Pro, and RStudio Connect
By: Jeff Allen
This article describes two ways to configure applications to use different sets of libraries in Shiny Server, Shiny Server Pro, and RStudio Connect.
Different libraries for different apps on RStudio Connect
RStudio Connect installs the R package dependencies of Shiny applications using the
packrat R packages to identify the target source code and enumerate its dependencies. That information is bundled into an archive (
.tar.gz) file and uploaded to RStudio Connect. This is done on a per-app basis, therefore two apps on RStudio Connect can use different packages or different versions of the same packages. The
packrat package attempts to re-use R packages whenever possible, so if two apps share the same version of a package as a dependency, they can take advantage of previously-installed packages, hence making deployment faster.
Different libraries for different apps on Shiny Server and Shiny Server Pro
Allowing for different libraries for different apps on Shiny Server and Shiny Server Pro requires a different approach. In the remainder of this article we describe two ways to configure applications to use different sets of libraries on Shiny Server and Shiny Server Pro.
- Method 1 relies on Shiny Server’s
exec_supervisorfeature. It will only work with Shiny Server Pro.
- Method 2 relies on Shiny Server’s
run_asfeature. It will work with both Shiny Server and Shiny Server Pro.
Method 1 - exec_supervisor
With Shiny Server Pro, you can run R sessions under a program supervisor that modifies the environment of the sessions. You can use this supervisor to set the
R_LIBS_USER environmental variable, which controls which libraries a session may use.
Section 3.6 of the Shiny Server Administrator’s Guide explains how to add a program supervisor. You add an
exec_supervisor setting to your server config file to specify a supervisor (and the arguments which control its behavior).
The file below uses
exec_supervior to modify the default /etc/shiny-server/shiny-server.conf file that ships with Shiny Server.
exec_supervisor partitions the applications up by setting the
R_LIBS_USER environment variable.
Near the bottom of the file, a
/finance sub-location is defined for apps that use a specific set of libraries. Beneath that, a specific app is given its own set of libraries.
In this case, you could set up as many different libraries as you want and specify a different library for each location, or even each application that you want to deploy. This would give you fine-grained control over each of your applications.
Method 2 - run_as
Shiny Server (and Shiny Server Pro) use a
run_as setting to determine which user should spawn each R Shiny processes. The user setting determines which library R will look in for packages (as well as which directories the app will be able to read and write to).
run_as setting can be configured globally, or for a particular server or location. As a result, you can set up different locations for hosting apps that use different packages. Each location can be affiliated with its own user—each of which presumably has a different set of libraries specified with a ~/.Rprofile file.
With this approach, you would probably have only a handful of users that you create that each maintain their own separate libraries.
The file below uses
run_as to modify the default /etc/shiny-server/shiny-server.conf file that ships with Shiny Server.
run_asdefines a global user,
shiny, for the server. It then defines a different user for the
shinyFinance. The finance department can deploy apps in this location. Those apps will be restricted to the packages in the library of
shinyFinance, which may be different than the packages in the library of the user named
If you have questions about this article or would like to discuss ideas presented here, please post on RStudio Community. Our developers monitor these forums and answer questions periodically. See help for more help with all things Shiny.