How do you pass parameters to a shiny app via URL

后端 未结 2 1067
抹茶落季
抹茶落季 2020-11-30 01:46

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 s

相关标签:
2条回答
  • 2020-11-30 02:23

    Building off of daattali, this takes any number of inputs and does the assigning of values for you for a few different types of inputs:

    ui.R:

    library(shiny)
    
    shinyUI(fluidPage(
    textInput("symbol", "Symbol Entry", ""),
    
    dateInput("date_start", h4("Start Date"), value = "2005-01-01" ,startview = "year"),
    
    selectInput("period_select", label = h4("Frequency of Updates"),
                c("Monthly" = 1,
                  "Quarterly" = 2,
                  "Weekly" = 3,
                  "Daily" = 4)),
    
    sliderInput("smaLen", label = "SMA Len",min = 1, max = 200, value = 115),br(),
    
    checkboxInput("usema", "Use MA", FALSE)
    
    ))
    

    server.R:

    shinyServer(function(input, output,session) {
    observe({
     query <- parseQueryString(session$clientData$url_search)
    
     for (i in 1:(length(reactiveValuesToList(input)))) {
      nameval = names(reactiveValuesToList(input)[i])
      valuetoupdate = query[[nameval]]
    
      if (!is.null(query[[nameval]])) {
        if (is.na(as.numeric(valuetoupdate))) {
          updateTextInput(session, nameval, value = valuetoupdate)
        }
        else {
          updateTextInput(session, nameval, value = as.numeric(valuetoupdate))
        }
      }
    
     }
    
     })
    })
    

    Example URL to test: 127.0.0.1:5767/?symbol=BBB,AAA,CCC,DDD&date_start=2005-01-02&period_select=2&smaLen=153&usema=1

    0 讨论(0)
  • 2020-11-30 02:41

    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']])
          }
        })
      }
    )
    
    0 讨论(0)
提交回复
热议问题