【问题标题】:React to plotly animation button对情节动画按钮做出反应
【发布时间】:2019-12-30 21:01:15
【问题描述】:

是否可以为情节动画按钮制作闪亮的observeEvent 或类似的东西?

也就是说:如何检测动画按钮何时被按下以在侧面显示其他内容?

我们需要对按钮的引用,但查看 HTML 输出,我找不到按钮的 id。我也尝试使用情节本身来制作动画,但这也不起作用。

查看?animation_opts,我找到了list of possible attributes,但无法弄清楚。我看到了对execute = bool属性的引用,描述为:

true 时,执行 API 方法。当false 时,所有其他行为都相同,并跳过命令执行。这在挂钩到 plotly_buttonclicked 方法并手动执行 API 命令时可能很有用,而不会失去更新菜单通过 methodargs 的规范自动绑定到绘图状态的好处。

但我看不到如何拨打plotly_buttonclicked。而且,实际上,我不想在内部调用它,而只是在用户调用它时observe

所以,基本上,给出这个例子:

library(plotly)
library(shiny)

ui <- fluidPage(
  plotlyOutput("p")
)

server <- function(input, output, session) {
  p <- plot_ly() %>%
    add_pie(values = c(1, 2, 3, 4, 5, 6),
            frame = c(1, 1, 2, 2, 3, 3))

  output$p <- renderPlotly(p)
  observeEvent(p, ignoreInit = TRUE, {
    print(str(p))
  })
}

shinyApp(ui, server)

每当用户按下“播放”时,我都想在控制台上打印一些东西。

【问题讨论】:

    标签: r shiny plotly r-plotly


    【解决方案1】:

    有一个事件plotly_animated,但它似乎只有在动画完成时才会触发(编辑:这不是好事件;请参阅最后的编辑)。

    library(shiny)
    library(plotly)
    library(htmlwidgets)
    
    ui <- fluidPage(
      plotlyOutput("p")
    )
    
    server <- function(input, output, session) {
    
      output$p <- renderPlotly({
        p <- plot_ly() %>%
          add_pie(values = c(1, 2, 3, 4, 5, 6),
                  frame = c(1, 1, 2, 2, 3, 3))
        p %>% onRender("
                       function(el, x){
                         el.on('plotly_animated', function(){
                           Shiny.setInputValue('anim', true)
                         });
                       }")
      })
    
      observeEvent(input$anim, {
        print("ANIMATION")
      })
    
    }
    
    shinyApp(ui, server)
    

    否则,您可以在 Shiny 中使用 Play 按钮,而不是使用 plotly Play 按钮;见this answer


    编辑

    事实上,你找到了好事件。这是plotly_buttonclicked。将我的代码替换为:

    p %>% onRender("
                   function(el, x){
                     el.on('plotly_buttonclicked', function(){
                       Shiny.setInputValue('anim', true)
                     });
                   }")
    

    【讨论】:

      猜你喜欢
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      • 2012-10-22
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多