Learn R Programming

shinyURL (version 0.0.35)

shinyURL.Server: Save and restore the view state of a Shiny app

Description

Encode the state of Shiny app's widgets into an URL query string, and use parameters from the URL query string to initialize the app.

Usage

shinyURL.server(session, options)
shinyURL.ui(display = TRUE, label = "Share URL", width = "100%", copyURL = TRUE, tinyURL = TRUE, ZeroClipboard.swf)

Arguments

session
Typically the same as the optional parameter passed into the Shiny server function as an argument; if missing defaults to getDefaultReactiveDomain()
options
Named list of options
display
logical, should the shinyURL widget be displayed
label
Label for the URL field
width
The width of the URL text field, e.g. '100%', or '400px'; see validateCssUnit.
copyURL
Include a ‘Copy’ button for convenient copying to clipboard
tinyURL
Use the TinyURL web service for shortening the URL
ZeroClipboard.swf
URL of the file ZeroClipboard.swf, as passed to the ‘swfPath’ parameter of ‘ZeroClipboard.config’; if missing defaults to the local copy distributed with shinyURL

Value

shinyURL.server returns a reactive expression evaluating to the app's URL.

ShinyURL options

Quick setup

To start using shinyURL in your Shiny app, follow these three steps:
  1. Load the package in both 'server.R' an ui.R': library("shinyURL")
  2. Add a call to shinyURL.server() inside the server function
  3. Add the shinyURL.ui() widget to the user interface

Details

The shinyURL.server method contains server logic for encoding and restoring the widgets' values. It is called from inside the app's server script, and can take the session objects as argument. The argument options can contain a named list of options. These are set by a call to options as ‘shinyURL.name’. See below for a list of available options.

The shinyURL.ui widget consists of a text field containing an URL to the app's current view state. By default it also features the convenience ‘Copy’ button for copying the URL to clipboard, and a ‘TinyURL’ button for querying the URL shortening web service. The inclusion of these buttons is optional and can be controlled by the copyURL and tinyURL arguments, respectively. The ‘Copy’ feature uses the ZeroClipboard library, which provides an easy way to copy text to the clipboard using an invisible Adobe Flash movie and JavaScript. shinyURL includes the JavaScript code to your app automatically, but you also need to have the “ZeroClipboard.swf” available to the browser. By default shinyURL uses a local copy distributed with the package; you can override this by setting the ZeroClipboard.swf argument to shinyURL.ui, for example, use "//cdnjs.cloudflare.com/ajax/libs/zeroclipboard/2.2.0/ZeroClipboard.swf" for a file hosted on jsDelivr CDN.

Examples

Run this code
if (interactive()) {
  library("shiny")
  
  ## A Simple Shiny App
   
  shinyApp(
    ui = fluidPage(
      titlePanel("Hello Shiny!"),
      sidebarLayout(
        sidebarPanel(
          sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30),
          shinyURL.ui()
        ),
        mainPanel(
          plotOutput("plot")
        )
      )
    ),
    server = function(input, output, session) {
      shinyURL.server(session)
      output$plot <- renderPlot({
        x <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
      })
    }
  )

  ## Shiny Widgets Demo
  shinyAppDir( system.file('examples', 'widgets', package='shinyURL') )

  ## Tabsets Demo
  shinyAppDir( system.file('examples', 'tabsets', package='shinyURL') )
  
  ## Showcase demo available live at https://gallery.shinyapps.io/shinyURL
  shinyAppDir( system.file('examples', 'showcase', package='shinyURL') )
  
  ## Interactive R Markdown document which uses a QR code to encode the URL
  if (require("rmarkdown") && require("qrcode"))
    run( system.file('examples', 'qrcode', 'qrcode.Rmd', package='shinyURL') )
    
  ## Use with dynamic user interface created by renderUI()
  shinyAppDir( system.file('examples', 'dynamicUI', package='shinyURL') )
}

Run the code above in your browser using DataLab