Shiny for R

Shiny for R 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 server function.

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

  output$groups <- reactive({
    session$groups
  })
})

Your application can 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 identifies 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.

Deployed file names

Connect runs your Shiny application with shiny::runApp() and specifies the directory containing your application as the appDir.

shiny::runApp(appDir = getwd(), ...)

When you deploy your Shiny application to Connect, it should either:

  • Contain app.R
  • Contain server.R, plus either ui.R or a www directory that contains the file index.html.

As a convenience, RStudio push-button deployment renames a single-file Shiny application to app.R as the content is being published. It is recommended that you use the app.R name for single-file Shiny applications rather than rely on this rename.

Bookmarks

Version 0.14 of the shiny package introduced bookmarkable state. Posit Connect includes full support for Shiny bookmarkable state features. Bookmarkable state allows users to create and share vanity URLs to Shiny applications that 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 Bookmarking section of the Shiny website.

Caching

Version 1.6 of the shiny package introduced caching for reactive 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 in the Caching section of the Shiny website.

In addition to this change, applications deployed on 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 are saved and used across the multiple R processes that 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, including 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 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 Connect dashboard. See the Content Settings panel section to learn more.

Example recipes are available in the Cookbook.

Specifying protocols

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 opens a window that allows you to select or deselect any of the available protocols. After you confirm the changes, these settings are 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 last until you explicitly change them again. They only effect the browser in which this action is performed.