簡體   English   中英

shinyBS::bsCollapse 和 shiny::conditionalPanel 之間的沖突

[英]Conflict between shinyBS::bsCollapse and shiny::conditionalPanel

我在 shinyBS::bsCollapsePanel 內使用shiny::conditionalPanel shinyBS::bsCollapsePanel 我的應用程序中有邏輯取決於哪個shinyBS 面板處於活動狀態(即展開)。 這工作正常,直到我激活條件面板。 如果我顯示 Shiny 條件面板,那么即使面板處於非活動狀態(即關閉),shinyBS 折疊面板也會卡在活動狀態。

如何修改我的代碼,以便可折疊面板僅在展開時才注冊為活動面板?

在此示例中,有一個文本 output 指示活動面板。 除非顯示條件面板,否則面板之間的切換可以正常工作。

編輯:看來這個錯誤可能已經記錄在案( https://github.com/ebailey78/shinyBS/issues/38 )並且有一個可能的解決方案( https://github.com/ebailey78/shinyBS/pull/68/提交)。

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)

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


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM