代码之家  ›  专栏  ›  技术社区  ›  Ulgur

r使用操作按钮隐藏/显示复选框

  •  1
  • Ulgur  · 技术社区  · 9 年前

    假设我有以下代码。。。

    用户界面

    library(shiny)
    
        ui <- fluidPage(
          actionButton("fishButton", label = "Fish"),
          checkboxGroupInput("Check1",label=h4 ("Fish:"), choices = c("Bass","Shark","Tuna")),
          actionButton("reptileButton", label = "Reptile"),
          checkboxGroupInput("Check2",label=h4 ("Reptile:"), choices = c("Komodo","Skink","Snake")),
          actionButton("mammalButton", label = "Mammal"),
          checkboxGroupInput("Check3",label=h4 ("Mammals:"), choices = c("Dog","Cat","Bear")),
          actionButton("birdButton", label = "Bird"),
          checkboxGroupInput("Check4",label=h4 ("Birds:"), choices = c("Budgie","Parrot","Cockatiel")),
          actionButton("amphibianButton", label = "Amphibian"),
          checkboxGroupInput("Check5",label=h4 ("Amphibian:"), choices = c("Frog","Toad","Salamander"))
        )
    

    有没有办法通过单击相应的actionButton来使用条件面板隐藏/显示复选框组?据我所知,actionButton只存储一个从0开始的整数,每次单击按钮时会增加1,这在本例中似乎没有太大帮助。有没有可能有一个条件面板,它只在actionButton值为偶数或类似数字时显示自己?

    1 回复  |  直到 9 年前
        1
  •  4
  •   Icaro Bombonato    9 年前

    您可以使用 shinyJS 包裹:

    install.packages("shinyjs")
    

    在ui中,将字段初始化为隐藏,并在单击按钮时调用切换

    用户界面 :

    library(shiny)
    library(shinyjs)
    
    shinyUI(fluidPage(
    
      shinyjs::useShinyjs(),
    
      actionButton("fishButton", label = "Fish"),
      hidden(
        checkboxGroupInput("Check1",label=h4 ("Fish:"), choices = c("Bass","Shark","Tuna"))
      )
    ))
    

    服务器.R

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