Shiny

Shiny is a web application framework for R that can help turn your analyses into interactive web applications. Get started by following the Shiny tutorial then visit the gallery for inspiration to use in your own dynamic visualizations.

Example Shiny application.

User meta-data

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

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.

User and Group uniqueness

Most environments have unique usernames where each session$user identifies a single user and session$groups the name of the groups the user is a member of.

However, in large organizations with hundreds of users and groups, this may not be true. See the Admin Guide sections Credentials for Content for more information.

Bookmarks

Version 0.14 of the shiny package introduced bookmarkable state. Posit 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.

Caching

Version 1.6 of the shiny package introduced caching for reactives and render* functions. Caching can greatly improve the performance of an application visited by multiple users by preventing the R process from re-creating identical objects for each visitor.

More information on how to using caching is available on the Shiny website.

In addition to this change, applications deployed on Posit 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 = cachem::cache_disk("./app_cache/cache/"))

This option ensures that cached objects will be saved and used across the multiple R processes that Posit 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.

Load Testing

The runtime settings of a Shiny application control how processes are managed: when to start and stop processes in reaction to traffic demands.

This support article explains process scheduling and provides some guidelines when changing your own application settings. Details for each setting are included in the Runtime/Scheduler configuration appendix of the Posit Connect Admin Guide.

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

Tracking Visits

Connect records visits to Shiny applications and lets you see:

  • Which applications were viewed
  • When the applications were viewed
  • Who viewed the application
  • The length of each session

An overview of recent activity is available in the Posit Connect dashboard. See the Content Settings Panel section to learn more.

Details about each visit are available through Connect Server API instrumentation endpoints. Perform your own analysis using the results from these endpoints. Code examples showing how to obtain this data can be found in the User Activity section of the Posit Connect: Server API Cookbook.

Specifying Protocols

Posit Connect provides a wide variety of techniques to keep the data in the web browser synchronized. The preferred technique, and the one most widely used, is the use of WebSockets. If WebSockets are not supported, either by some intermediate network between the server and your client or by your client’s web browser, then a fallback protocol will be used.

To change the available protocols from the client, open a Shiny application and press the keyboard shortcut: Ctrl+Alt+Shift+A (or, from a Mac: control+option+shift+A). This will open a window that will allow you to select or deselect any of the available protocols. After you confirm the changes, these settings will be saved in your browser for future visits to this server.

If you are actively visiting an application, refresh the browser page or tab in order for the new settings to take effect within your session. These settings will last until you explicitly change them again; they will only have an effect on the browser in which this action was performed.