【问题标题】:shinyBS observe toggling of bsCollapsePanelshinyBS 观察 bsCollapsePanel 的切换
【发布时间】:2017-09-16 02:18:56
【问题描述】:

我的问题与观察在 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)

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

我在想也许可以使用shinyjs 包,但没有找到很多这两个包一起使用的例子。

【问题讨论】:

    标签: javascript r shiny shinyjs shinybs


    【解决方案1】:

    好的,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?

    【讨论】:

    • 我认为这是有用的代码,尽管对于这种特殊情况来说可能有点矫枉过正。不过,从长远来看,可能更适用于他的意图。
    • 我同意我也尝试过首先与input$collapseExample 合作,但不知何故我似乎不得不错过 smthg,因为它对我不起作用。 +1 为您提供更优雅的解决方案 :)。
    • 为您提供更有趣的解决方案。实际研究一下,总有一天会用到的。
    • 出色的解决方案。我喜欢shinyjs 和shinyBS 的结合。肯定对我有用。
    【解决方案2】:

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

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

    代码如下:

    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)
    

    还有一个屏幕截图:

    【讨论】:

    • 优雅的解决方案。即使在运行它之后,我也意识到我希望在关闭面板后更改数字。但由于我只提到在我的问题中打开面板并且您是第一个回答的人,所以我接受它是有道理的。
    • 我会很感激的。并且随时发布有关其他要求的另一个问题,我相信有人会解决它 - 如果我有时间的话,包括我。
    猜你喜欢
    • 1970-01-01
    • 2019-12-28
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多