【问题标题】:Conflict between shinyBS::bsCollapse and shiny::conditionalPanelshinyBS::bsCollapse 和 shiny::conditionalPanel 之间的冲突
【发布时间】:2020-01-20 17:23:35
【问题描述】:

我在shinyBS::bsCollapsePanel 中使用shiny::conditionalPanel。我的应用程序中有逻辑取决于哪个shinyBS 面板处于活动状态(即展开)。这工作正常,直到我激活条件面板。如果我显示 Shiny 条件面板,那么即使面板处于非活动状态(即关闭),shinyBS 折叠面板也会卡在活动状态。

如何修改我的代码,使可折叠面板仅在展开时才注册为活动面板?

在此示例中,有一个文本输出指示活动面板。除非显示条件面板,否则面板之间的切换可以正常工作。

编辑:看来这个错误可能已经记录在案 (https://github.com/ebailey78/shinyBS/issues/38) 并且有可能的解决方案 (https://github.com/ebailey78/shinyBS/pull/68/commits)。

library(shiny)
library(shinyBS)

# Define UI logic
ui <- fluidPage(

    htmlOutput("activePanel"),

    shinyBS::bsCollapse(
        id = "bsPanels",
        shinyBS::bsCollapsePanel(
            "Panel A",
            value = "panelA",
            checkboxInput("showPanelA",
                          "Show panel",
                          value = FALSE),
            conditionalPanel(
                condition = "input.showPanelA",
                helpText("Panel A conditional content")
            ),
            helpText("Panel A main content")
        ),
        shinyBS::bsCollapsePanel(
            "Panel B",
            value = "panelB",
            checkboxInput("showPanelB",
                          "Show panel",
                          value = FALSE),
            conditionalPanel(
                condition = "input.showPanelB",
                helpText("Panel B conditional content")
            ),
            helpText("Panel B main content")
        )
    )
)

# Define server logic 
server <- function(input, output) {

    output$activePanel <- renderText({
        paste("<b>Active Panel:</b>", paste(input$bsPanels, collapse = ", "))
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 我尝试通过在observeEvent() 中使用updateCheckboxInput() 来解决问题,当面板更改时,复选框会被取消选中...虽然没有解决该错误,很高兴你找到了一个可能的解决方案!

标签: r shiny shiny-reactivity


【解决方案1】:

shinyBS 项目页面 (https://github.com/ebailey78/shinyBS/issues/38) 对此问题进行了一些讨论。但是,我对所提出的解决方案的成功有限。

我发现的最佳解决方案是使用shinyjs::showElementshinyjs::hideElement

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

# Define UI logic
ui <- fluidPage(

    useShinyjs(),

    htmlOutput("activePanel"),

    shinyBS::bsCollapse(
        id = "bsPanels",
        shinyBS::bsCollapsePanel(
            "Panel A",
            value = "panelA",
            checkboxInput("showPanelA",
                          "Show panel",
                          value = FALSE),
            uiOutput("condPanelA"),
            helpText("Panel A main content")
        ),
        shinyBS::bsCollapsePanel(
            "Panel B",
            value = "panelB",
            checkboxInput("showPanelB",
                          "Show panel",
                          value = FALSE),
            uiOutput("condPanelB"),
            helpText("Panel B main content")
        )
    )
)

# Define server logic 
server <- function(input, output) {

    output$activePanel <- renderText({
        paste("<b>Active Panel:</b>", paste(input$bsPanels, collapse = ", "))
    })

    # Logic for conditional panels
    output$condPanelA <- renderUI({
      helpText("Panel A conditional content")
    })
    observe({
      if(input$showPanelA) {
        show("condPanelA")
      } else {
        hide("condPanelA")
      }
    })
    output$condPanelB <- renderUI({
      helpText("Panel B conditional content")
    })
    observe({
      if(input$showPanelB) {
        show("condPanelB")
      } else {
        hide("condPanelB")
      }
    })

}

# Run the application 
shinyApp(ui = ui, server = server)


【讨论】:

    猜你喜欢
    • 2016-07-31
    • 1970-01-01
    • 2012-06-27
    • 2012-03-06
    • 2011-05-02
    • 2012-11-25
    • 2013-10-27
    • 2014-03-02
    • 2016-11-15
    相关资源
    最近更新 更多