r 使用操作按钮 hide/show 复选框

r Using action buttons to hide/show checkboxes

假设我有以下代码...

ui.r

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 来使用条件面板 hide/show checkboxGroups?据我了解,actionButton 仅存储一个整数,该整数从 0 开始,每次单击按钮时增加 1,这在这种情况下似乎没有多大帮助。有没有可能有一个条件面板,只在它的 actionButton 值是偶数或类似的时候显示自己?

您可以使用 shinyJS 包:

install.packages("shinyjs")

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

ui.R:

library(shiny)
library(shinyjs)

shinyUI(fluidPage(

  shinyjs::useShinyjs(),

  actionButton("fishButton", label = "Fish"),
  hidden(
    checkboxGroupInput("Check1",label=h4 ("Fish:"), choices = c("Bass","Shark","Tuna"))
  )
))

server.R

library(shiny)
library(shinyjs)

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