1

我的问题与观察在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包,但没有找到很多这两个包一起使用的例子。

4

2 回答 2

2

好的,Mike Wise 比我快 :) 如果由于某种原因我的解决方案也有帮助,请告诉我,否则我将其删除。

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

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

server = function(input, output, session) {
  observeEvent(input$p1Button, ({
    updateCollapse(session, "collapseExample", open = "Panel1")
  }))
  observeEvent(input$styleSelect, ({
    updateCollapse(session, "collapseExample", style = list("Panel1" = input$styleSelect))
  }))

  observe({
    runjs("function getAllElementsWithAttribute(attribute){
              var matchingElements = [];
              var allElements = document.getElementsByTagName('*');
              for (var i = 0, n = allElements.length; i < n; i++)
              {
              if (allElements[i].getAttribute(attribute) !== null)
              {
              // Element exists with attribute. Add to array.
              matchingElements.push(allElements[i]);
              }
              }
              return matchingElements;
              };
              ahref = getAllElementsWithAttribute('data-toggle');
              ahref[0].onclick = function() { 
                var nmbr = Math.random();
                Shiny.onInputChange('randomNumber', nmbr);
              };
          ")
  })
  output$randomNumber <- reactive(paste0(input$randomNumber))
}




shinyApp(ui = ui, server = server)

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

于 2017-04-19T18:50:45.590 回答
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)

和一个屏幕截图:

在此处输入图像描述

于 2017-04-19T18:36:26.543 回答