Search code examples
rshinyshiny-servershinydashboard

How do you pass parameters to a shiny app via URL


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


Solution

  • 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']])
          }
        })
      }
    )