【问题标题】:Shiny and Plotly update buttons are not updating with correct dataShiny 和 Plotly 更新按钮未使用正确的数据进行更新
【发布时间】:2020-03-31 14:23:15
【问题描述】:

https://plot.ly/r/custom-buttons/

你好!第一次在这里发帖。我对 R Shiny 和 Plotly 比较陌生。我正在尝试使用 Plotly 根据 Plotly 中的“更新”按钮选项从本质上过滤数据。参考代码在上面的链接中。下面是我的代码,我很接近,按钮会过滤数据,但不正确,所以我觉得我在 Plotly 的 args 中缺少注释。

感谢您的反馈!

library(shiny)
library(shinydashboard)
library(plotly)
library(RODBC)
library(tidyverse)
library(lubridate)
library(htmlwidgets)
################
# ui
################
    ui <- fluidRow(plotlyOutput("plot", width = '100%', height = '800px'))




################
# Server
################
server <- function(input, output, session) {
  load(file = "/mnt/data/shinyAppsData/ltsang/B2.dat")
    margin <- list(
    l = 150,
    r = 50,
    b = 100,
    t = 100,
    pad = 4
  )

    build_annotations <- list(
      x=B2$DATETIME, 
      y=B2$WL_BUILD)
    release_annotations <- list(
      x=B2$DATETIME, 
      y=B2$WL_RELEASE)
    automation_annotations <- list(
      x=B2$DATETIME, 
      y=B2$AUTOMATION_RCVD)


  output$plot <- renderPlotly({B2 %>% 
      plot_ly(type = 'scatter', mode = 'lines') %>%
      add_trace(x=~DATETIME, y = ~WL_BUILD, source="A", name = ~NODES, line = list(color = ~NODES, width = 4)) %>%
      add_trace(x=~DATETIME, y = ~WL_RELEASE, source="B", name = ~NODES, line = list(color = ~NODES, width = 4, dash = 'dash')) %>%
      add_trace(x=~DATETIME, y = ~AUTOMATION_RCVD, source= "C", name = ~NODES, line = list(color = ~NODES, width = 4, dash = 'dot')) %>%
      layout(autosize = T, title = "NIGHT PRODUCTION", margin=margin,
             xaxis = list(title = "TIME, DATE", tickformat = "%H - %m/%d/%Y (%a)", ticks= "inside", dtick=3600000, rangeslider = list(type = "date"),
             rangeselector = list(
                buttons = list(
                  list(
                    count = 1,
                    label = "1 Day",
                    step = "day",
                    stepmode = "backward"),
                  list(
                    count = 2, 
                    label = "2 Days",
                    step = "day",
                    stepmode = "backward"),
                  list(
                    count = 3, 
                    label = "3 Days",
                    step = "day",
                    stepmode = "backward"),
                  list(step = "all", label = "all")
                )
            )),
            yaxis = list(title = "ACCESSION COUNT"),
            updatemenus = list(
              list(
                x = -0.1, 
                y = 0.8,
                showactive=TRUE,
                active = -1,
                bgcolor = "lightblue",
                type = 'buttons', 
                buttons = list(
                  list(
                    label = "WL_BUILD", 
                    method = "update",
                    args = list(list(visible = c(TRUE,FALSE,FALSE)),
                                list(title = "WORKLIST BUILD", source="A"))),
                                     #annotations = list(build_annotations,release_annotations,automation_annotations )))), 
                  list(
                    label = "WL_RELEASE", 
                    method = "update",
                    args = list(list(visible = c(FALSE,TRUE,FALSE)),
                                list(title = "WORKLIST RELEASE", source="B"))),
                                     #annotations = list(build_annotations,release_annotations,automation_annotations)))), 
                  list(
                    label = "AUTOMATION_RCVD", 
                    method = "update",
                    args = list(list(visible = c(FALSE,FALSE,TRUE)),
                                list(title = "AUTOMATION RECEIVED", source="C"))),
                                     #annotations = list(build_annotations,release_annotations,automation_annotations)))),
                  list(
                    label = "VIEW ALL", 
                    method = "update",
                    args = list(list(visible = c(TRUE,TRUE,TRUE)),
                                list(title = "AUTOMATION RECEIVED & WORKLIST BUILD/RELEASE")))
                                     #annotations = list(release_annotations,build_annotations, automation_annotations))))
                )
              )
            )
      )}
  )
  #observeEvent(event_data("plotly_relayout"), {
  #plotlyProxy("plot", session) %>%
  #plotlyProxyInvoke("relayout")
  #})

}

shinyApp(ui, server, enableBookmarking = "url")


【问题讨论】:

  • 您目前将计数设置为 1、3 和 4,尽管被标记为 1 天、2 天和 3 天。这是你想要的吗?
  • 我将更新它,感谢您发现它,它清理了我的范围滑块,但对使用“更新”方法的 Plotly 按钮没有任何作用。
  • 您可以尝试将您的第一行添加为单独的add_trace()add_line() 吗?
  • 我只是在做了一些研究之后才这样做,我将更新我的代码以反映该更改,但仍然不会根据单击的按钮更新我的图表。我认为 plotly 会处理所有“反应性”的东西,但在这种情况下似乎没有这样做。

标签: r shiny plotly


【解决方案1】:

我们想通了。下面更新了代码,列表是我存在的基础。

server <- function(input, output, session) {
  load(file = "/mnt/data/shinyAppsData/ltsang/B2.dat")
    margin <- list(
    l = 150,
    r = 50,
    b = 100,
    t = 100,
    pad = 4
  )

    chem<-length(c("COBAS7S", "COBAS7S2","COBAS7U","COBAS5S","COBASIA", "COBAS8LV","LIAISON","ARCTECT","CPL CBC","CPL UA","A1C","COAG1","RPR", "sg"))
    release<-length(c("COBAS7S", "COBAS7S2","COBAS7U","COBAS5S","COBASIA", "COBAS8LV","LIAISON","ARCTECT","CPL CBC","CPL UA","A1C","COAG1","RPR"))
    auto<-length(c("Automate3", "Automate5" ,"Automate6","BIM1", "BIM2", "BIM3","MUT2","P612-1", "P612-2","P612-3","P612-4", "P612-5","P612-6", "P612-7", "sg"))


    output$plot <- renderPlotly({B2 %>% 
      plot_ly(type = 'scatter', mode = 'lines') %>%
      add_lines(x=~DATETIME, y = ~WL_BUILD, name = ~NODES, line = list(color = ~NODES, width = 4)) %>%
      add_lines(x=~DATETIME, y = ~WL_RELEASE, name = ~NODES, line = list(color = ~NODES, width = 4, dash = 'dash')) %>%
      add_lines(x=~DATETIME, y = ~AUTOMATION_RCVD, name = ~NODES, line = list(color = ~NODES, width = 4, dash = 'dot')) %>%
      layout(autosize = T, title = "NIGHT PRODUCTION", margin=margin,
             xaxis = list(title = "TIME, DATE", tickformat = "%H - %m/%d/%Y (%a)", ticks= "inside", dtick=3600000, rangeslider = list(type = "date"),
             rangeselector = list(
                buttons = list(
                  list(
                    count = 1,
                    label = "1 Day",
                    step = "day",
                    stepmode = "backward"),
                  list(
                    count = 2, 
                    label = "2 Days",
                    step = "day",
                    stepmode = "backward"),
                  list(
                    count = 3, 
                    label = "3 Days",
                    step = "day",
                    stepmode = "backward"),
                  list(step = "all", label = "all")
                )
            )),
            yaxis = list(title = "ACCESSION COUNT"),
            updatemenus = list(
              list(
                x = -0.1, 
                y = 0.8,
                showactive=TRUE,
                active = -1,
                bgcolor = "lightblue",
                type = 'buttons', 
                buttons = list(
                  list(
                    label = "WL_BUILD", 
                    method = "update",
                    args = list(list(visible = c(rep_len(TRUE, chem), rep_len(FALSE, release), rep_len(FALSE, auto))),
                                list(title = "WORKLIST BUILD", key="A"))),

                  list(
                    label = "WL_RELEASE", 
                    method = "update",
                    args = list(list(visible = c(rep_len(FALSE, chem), rep_len(TRUE, release), rep_len(FALSE, auto))),
                                list(title = "WORKLIST RELEASE", key="B"))),

                  list(
                    label = "AUTOMATION_RCVD", 
                    method = "update",
                    args = list(list(visible = c(rep_len(FALSE, chem), rep_len(FALSE, release), rep_len(TRUE, auto))),
                                list(title = "AUTOMATION RECEIVED", key="C"))),

                  list(
                    label = "VIEW ALL", 
                    method = "update",
                    args = list(list(visible = c(rep_len(TRUE, chem), rep_len(TRUE, release), rep_len(TRUE, auto))),
                                list(title = "AUTOMATION RECEIVED & WORKLIST BUILD/RELEASE")))

                )
              )
            )
      )}
  )
}

shinyApp(ui, server, enableBookmarking = "url")

【讨论】:

    猜你喜欢
    • 2014-01-09
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多