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

时间:2016-01-12 12:58:30

标签: r shiny shinyjs

说我有以下代码......

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来隐藏/显示checkboxGroups?据我所知,actionButton只存储一个从0开始的整数,每按一下按钮就会增加1,在这种情况下看起来没什么用。是否有可能有一个条件面板,只有当它的actionButton值是偶数或某些时才显示自己?

1 个答案:

答案 0 :(得分:4)

您可以使用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")
  })
})