r shine hide / show all checked checkbox

时间:2018-05-07 13:24:30

标签: r checkbox shiny

我想隐藏或显示R中闪烁的所有选中复选框,以下代码可用作参考:

ui <- shinyUI({
  library(shiny)
  library(shinyjs)

  shinyUI(fluidPage(

    shinyjs::useShinyjs(),

    actionButton(inputId = "fishButton",  label = "Fish"),
    checkboxInput(inputId = "hide_show_checked", label = "Hide/Show all checked checkbozes"),
    hidden(
      checkboxGroupInput(inputId = "Check1",
                         label   = h4("Fish:"), 
                         choices = c("Bass", "Shark", "Tuna"))
    )
  ))
})


serv <- shinyServer({
  library(shiny)
  library(shinyjs)

  shinyServer(function(input, output) {
    observeEvent(input$fishButton, {
      toggle("Check1")
    })
  })
})

shinyApp(ui,serv)

我确信我需要在服务器部分添加一个观察事件,但我倾向于通过创建重复的复选框组并显示复选框组1(如果选中)并显示复选框组2(如果未选中)来过度思考。这会导致我的实际代码中出现大量重复(我有超过1000个复选框组)

谢谢!

1 个答案:

答案 0 :(得分:0)

ui <- fluidPage(

    checkboxInput(inputId = "hide_show_checked", label = "Hide/Show all checked checkbozes"),
    checkboxGroupInput(inputId = "Check1",
                 label   = h4("Fish:"), 
                 choices = c("Bass", "Shark", "Tuna")),
    verbatimTextOutput("value")

)

server <- function(input, output, session) {

   output$value <- renderPrint({ input$Check1})

   observe({

      if(input$hide_show_checked) {

             #x <- ifelse(data %in% c("Bass", "Shark", "Tuna"), data, "NA")

             data <- isolate(input$Check1)
             y <- c("Bass", "Shark", "Tuna")
             x <- y[!y %in% data]
   #y[-match(data,y)] OR setdiff(data,y)

  updateCheckboxGroupInput(session, "Check1",label = paste("Checkboxgroup label", length(x)),choices = x,selected = x)
} else {
  updateCheckboxGroupInput(session, "Check1",label = "Checkboxgroup label", choices = c("Bass", "Shark", "Tuna"),selected = NULL)
 }
   })
}

shinyApp(ui, server)  

我希望它有所帮助;)