【问题标题】:r Using action buttons to hide/show checkboxesr 使用操作按钮隐藏/显示复选框
【发布时间】:2016-04-17 02:36:53
【问题描述】:

假设我有以下代码...

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 值为偶数或类似数字时才显示的条件面板?

【问题讨论】:

  • 您可以在 ui 端使用 uiOutput() 和在服务器端(可能)生成的 checkboxGroupInput() 来执行此操作。我没有做过这个具体的事情,但一直使用服务器端输入。

标签: r shiny shinyjs


【解决方案1】:

您可以使用shinyJS 包:

install.packages("shinyjs")

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

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

【讨论】:

  • 您好,我使用了您的解决方案,但现在过滤数据时出现问题:if (exists(input$Check1)){datas &lt;- subset(datas, City == input$Check1)} 您能帮我解决这个问题吗?我认为exist 函数不会在这里工作
  • 一个可重现的示例会有所帮助,但我猜您正在尝试根据标记的复选框对数据进行子集化。可能这会起作用: df
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2016-09-04
  • 2013-07-06
  • 2013-03-23
相关资源
最近更新 更多