【发布时间】: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 会处理所有“反应性”的东西,但在这种情况下似乎没有这样做。