在Shiny中显示/隐藏bsCollapse面板

时间:2017-11-28 15:37:57

标签: shiny shinyjs shinybs

我正在使用ShinyBS和Shinyjs软件包构建一个Shiny App,我想隐藏/显示bscollapse面板,具体取决于另一个bscollapse面板中单选按钮的输入。

当在Parent1中选择“AAA”或“BBB”选项时,我想在Parent2中仅显示“AAAA”和“BBBB”collapsePanel。

当在Parent1中选择“CCC”或“DDD”选项时,我想在Parent2中仅显示“CCCC”和“DDDD”collapsePanels。

我正在使用来自shinyjs包的show()和hide()命令,但我无法使其工作。当选择“AAA”或“BBB”时,Parent2中没有出现任何内容,这不是我想要的。

以下是重现问题的代码:

library(shiny)
library(shinyBS)
library(shinyjs)

server = function(input, output, session) {

observeEvent(input$TypeRadio,{
if (input$TypeRadio == "AAA" || input$TypeRadio == "BBB")
  shinyjs::show("collapse1")
  shinyjs::hide("collapse2")
})

observeEvent(input$TypeRadio,{
if (input$TypeRadio == "CCC" || input$TypeRadio == "DDD")
  shinyjs::show("collapse2")
  shinyjs::hide("collapse1")})
}


ui = fluidPage(
shinyjs::useShinyjs(),

bsCollapse(id = "collapseExample", multiple = FALSE,
bsCollapsePanel("Parent1","",

              radioButtons("TypeRadio", "",
                           choices = list("AAA" = "AAA",
                                          "BBB" = "BBB",
                                          "CCC" = "CCC",
                                          "DDD" = "DDD"),
                           inline = FALSE)),


bsCollapsePanel("Parent2","",

              bsCollapse(id = "collapse1",   
                         bsCollapsePanel("AAAA", ""),
                         bsCollapsePanel("BBBB", "")),

              bsCollapse(id = "collapse2",   
                         bsCollapsePanel("CCCC", ""),
                         bsCollapsePanel("DDDD", ""))

                         )
 )                 
 )
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

library(shiny)
library(shinyBS)
library(shinyjs)

if(interactive()){
  shinyApp(
    ui <- fluidPage(
      useShinyjs(),
      bsCollapse(id = "collapseExample", multiple = FALSE,
                 bsCollapsePanel("Parent1","",
                                 radioButtons("TypeRadio", "",
                                              choices = list("AAA" = "AAA",
                                                             "BBB" = "BBB",
                                                             "CCC" = "CCC",
                                                             "DDD" = "DDD"),
                                              inline = FALSE)),
                 bsCollapsePanel("Parent2","",
                                 bsCollapse(id = "collapse1",   
                                            bsCollapsePanel("AAAA", ""),
                                            bsCollapsePanel("BBBB", "")),

                                 bsCollapse(id = "collapse2",   
                                            bsCollapsePanel("CCCC", ""),
                                            bsCollapsePanel("DDDD", ""))
                                 )
      )
    ),

    server = function(input, output, session){

      observeEvent(input$TypeRadio,{
        if("AAA" == input$TypeRadio | "BBB" == input$TypeRadio){
          shinyjs::show("collapse1")
          shinyjs::hide("collapse2")
        }

        if("CCC" == input$TypeRadio | "DDD" == input$TypeRadio){
          shinyjs::hide("collapse1")
          shinyjs::show("collapse2")
        }
      })

    }
  )
}