简体   繁体   中英

r Using action buttons to hide/show checkboxes

Say I had the following code...

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"))
    )

Is there a way to use conditional panels to hide/show the checkboxGroups by clicking on the appropriate actionButton? As I understand it the actionButton only stores an integer that starts at 0 and increases by 1 with each click of the button, which doesn´t seem very helpful in this case. Would it be possible to have a conditional panel that only showed itself when it´s actionButton value was an even number or somesuch?

You can do using shinyJS package:

install.packages("shinyjs")

In the ui, init the field as hidden and call the toggle on the button click

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")
  })
})

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