12 Shiny

12.1 User meta-data

Shiny applications can access the username and groups of the current user through the session parameter of the shinyServer function.

Group meta-data is populated when using most authentication providers, except LDAP. This happens because group memberships are only stored in the LDAP server.

shinyServer(function(input, output, session) {
  output$username <- reactive({
    session$user
  })
  
  output$groups <- reactive({
    session$groups
  })
})

Your application could use this information to display customized messages or to enable functionality for a specific subset of users.

12.2 Bookmarks

Version 0.14 of the shiny package introduced bookmarkable state. RStudio Connect includes full support for Shiny’s bookmarkable state features. Bookmarkable state allows users to create and share custom URLs to Shiny applications that will automatically restore the inputs and outputs of an application. For example, a user may select a set of inputs that produces an interesting plot. Rather than instructing another user to click through the same input steps to view the plot, they can create a bookmarked URL and share the URL with others. For more information, visit the Shiny Development Center.

12.3 Plot Caching

Version 1.2 of the shiny package introduced plot caching. Plot caching can greatly improve the performance of an application visited by multiple users. Plot caching prevents the R process from re-creating identical plots for each visitor.

To use plot caching, simply replace calls to shiny::renderPlot with a call to shiny::renderCachedPlot and include a shiny::cacheKeyExpr. View the help page for shiny::renderCachedPlot for details (run ?shiny::renderCachedPlot from R).

In addition to this change, applications deployed on RStudio Connect should use a disk cache and specify a subdirectory of the application directory as the location for the cache. To do so, add this code to the top of your application:

library(shiny)
shinyOptions(cache = diskCache("./cache"))

This option ensures that cached plots will be saved and used across the multiple R processes that RStudio Connect runs in support of an application. In addition, this configuration results in the cache being deleted and reset when new versions of the application are deployed.

12.4 Load Testing

RStudio Connect exposes runtime settings that can be changed by administrators or publishers to impact how processes are added to scale content. For more information, visit this support article. Details for each setting are included in the Job Scheduler configuration appendix of the RStudio Connect Admin Guide.

Shiny applications can be load tested using the shinyloadtest R package. The package includes support for applications deployed on RStudio Connect and applications that require authentication.

12.5 Shiny Application Usage

Connect also exposes usage information about shiny applications that will tell you which ones have been used, by whom and for how long. It is available via an API that may be used by administrators or publishers. Publishers may retrieve information about their own applications only.

More information about using the API is included in the historical information chapter of the RStudio Connect Admin Guide as well as within the RStudio Connect User Guide’s user activity section of the API Cookbook.