shinyBS观察bsCollapsePanel的切换

时间:2017-04-19 17:46:58

标签: r shiny shinyjs shinybs

我的问题涉及在shinyBS中的bsCollapsePanel中观察切换和取消标头的事件。

让我们考虑以下应用程序作为示例:

library(shiny)
library(shinyBS)
server = function(input, output, session) {
    observeEvent(input$p1Button, ({
      updateCollapse(session, "collapseExample", open = "Panel 1")
    }))
    observeEvent(input$styleSelect, ({
      updateCollapse(session, "collapseExample", style = list("Panel 1" = input$styleSelect))
    }))
    output$randomNumber <- reactive(paste0('some random number'))
  }


ui = fluidPage(
  sidebarLayout(
    sidebarPanel(HTML("This button will open Panel 1 using <code>updateCollapse</code>."),
                 actionButton("p1Button", "Push Me!"),
                 selectInput("styleSelect", "Select style for Panel 1",
                             c("default", "primary", "danger", "warning", "info", "success"))
    ),
    mainPanel(
      bsCollapse(id = "collapseExample", open = "Panel 2",
                 bsCollapsePanel("Panel 1", "This is a panel with just text ",
                                 "and has the default style. You can change the style in ",
                                 "the sidebar.", style = "info")
      ),
      verbatimTextOutput('randomNumber')
    )
  )
)

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

我希望应用能够在每次打开verbatimTextOutput('randomNumber')时通过点击bsCollapsePanel标题在Panel 1字段中打印随机数(使用R闪亮反应)。

我当时认为可能使用shinyjs包,但没有找到这两个包一起使用的很多例子。

2 个答案:

答案 0 :(得分:2)

好的,Mike Wise比我快:) 如果由于某种原因我的解决方案也有帮助让我知道,否则我删除它。

{{1}}

您可以在此处找到的Javascript代码: Get elements by attribute when querySelectorAll is not available without using libraries?

答案 1 :(得分:1)

我不完全确定你想要什么,但这可能很接近。这些是补充:

  • 添加了observeEvent来监控您的Panel 1标头。
  • 添加reactiveValues以保留“随机数”
  • 在推送observeEvent时,在上述Panel 1处理程序中增加该值。

以下是代码:

library(shiny)
library(shinyBS)
server = function(input, output, session) {
  rv <- reactiveValues(number=0)
  observeEvent(input$p1Button, ({
    updateCollapse(session, "collapseExample", open = "Panel 1")
  }))
  observeEvent(input$styleSelect, ({
    updateCollapse(session, "collapseExample", style = list("Panel 1" = input$styleSelect))
  }))
  observeEvent(input$collapseExample, ({
    rv$number <- rv$number+1
  }))
  output$randomNumber <- reactive(rv$number)
}


ui = fluidPage(
  sidebarLayout(
    sidebarPanel(HTML("This button will open Panel 1 using <code>updateCollapse</code>."),
                 actionButton("p1Button", "Push Me!"),
                 selectInput("styleSelect", "Select style for Panel 1",
                           c("default", "primary", "danger", "warning", "info", "success"))
    ),
    mainPanel(
      bsCollapse(id = "collapseExample", open = "Panel 2",
                 bsCollapsePanel("Panel 1", "This is a panel with just text ",
                                 "and has the default style. You can change the style in ",
                                 "the sidebar.", style = "info")
      ),
      verbatimTextOutput('randomNumber')
    )
  )
)
shinyApp(ui = ui, server = server)

屏幕截图:

enter image description here