How do you pass parameters to a shiny app via URL

user3022875 picture user3022875 · Sep 30, 2015 · Viewed 17.1k times · Source

In web browsers you pass parameters to a website like

www.mysite.com/?parameter=1

I have a shiny app and I would like to use the parameter passed in to the site in calculations as an input. So is it possible to do something like www.mysite.com/?parameter=1 and then use input!parameter?

Can you provide any sample code or links?

Thank you

Answer

DeanAttali picture DeanAttali · Sep 30, 2015

You'd have to update the input yourself when the app initializes based on the URL. You would use the session$clientData$url_search variable to get the query parameters. Here's an example, you can easily expand this into your needs

library(shiny)

shinyApp(
  ui = fluidPage(
    textInput("text", "Text", "")
  ),
  server = function(input, output, session) {
    observe({
      query <- parseQueryString(session$clientData$url_search)
      if (!is.null(query[['text']])) {
        updateTextInput(session, "text", value = query[['text']])
      }
    })
  }
)