简体   繁体   中英

How to pass multiple values from URL to r shiny checkbox input

Please see the sample code below, what I want is just pass multiple chk values from url as shiny check box input values; it works for single value like: http://127.0.0.1:3471/?chk=1 but it does not work for multiple values like http://127.0.0.1:3471/?chk=1,2 . Thanks in advance.

    library(shiny)

    shinyApp(
    ui = shinyUI(fluidPage(    
    checkboxGroupInput("chk", "Check box", c("Monthly" = 1,
                                           "Quarterly" = 2,
                                           "Weekly" = 3,
                                           "Daily" = 4))

    )),

      server = shinyServer(function(input, output,session) {
      observe({
      query <- parseQueryString(session$clientData$url_search)

      if (!is.null(query[['chk']])) {
        print(query[['chk']])
        updateTextInput(session, "chk", value = query[['chk']])
      }
     })
    })
    )

I believe that you may want to use updateCheckboxGroupInput to update your checkboxes instead of updateTextInput .

The extracted query[['chk']] will be a string separated by comma containing elements you wish to update in your checkboxes. You can use strsplit to separate them (and unlist since result would be a list).

server = shinyServer(function(input, output,session) {
  observe({
    query <- parseQueryString(session$clientData$url_search)
    if (!is.null(query[['chk']])) {
      print(query[['chk']])
      updateCheckboxGroupInput(session, "chk", selected = unlist(strsplit(query[['chk']], ",")))
    }
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM