Plotly 和 Shiny Dashboard- 使用 daterangeinput 更改绘图图中日期轴上的范围

Plotly and Shiny Dashboard- use daterangeinput to change range on date axis in a plotly graph

我在 R 中制作了一个 Plotly 图,其中包含线条和散点图以及填充区域变量。 X 轴是 as.POSIXCT 格式的日期和时间,格式为 =“%Y-%m-%d %H:%M:%S”。以 10 分钟为间隔有 2 个月的数据。 Y 轴是单位数。

我正在将图表集成到 Shiny Dashboard 中。我想使用 daterangeInput 来选择 X 轴的日期范围。不过,我不知道如何将 daterangeInput 集成为 reactive/interactive。

如有任何帮助,我们将不胜感激!

综合图:

graph <- plot_ly(master, x = date, y = arrival.one, name = 'One Arrival',    visible = "legendonly") %>%
add_trace(x= date, y = arrival.two, name = 'Two Arrival', mode = 'lines', visible = "legendonly" ) %>%
layout( xaxis = list(title= "Date and Time",
      rangeslider = list (type = "date"))) 

UI:

ui <- dashboardPage(skin="black",
                dashboardHeader(title = "Rack Filling"),
                dashboardSidebar(sidebarMenu(
                  menuItem("Dashboard", tabName = "dashboard", icon =     icon("line-chart")),
                  menuItem("Staffing", tabName = "staffing", icon = icon("male")),
                  dateRangeInput("inDateRange", "Date range input:")
                )),

服务器:

server <- function(input, output) {  
output$graph <- renderPlotly({graph               
})
output$event <- renderPrint({
??????????

在您的 server 函数中,您必须创建一个反应式,每次更改 DateRange 时都会过滤数据集。你称之为反应来绘制你的情节:

server <- function(input, output) {  
    reactiveMaster <- reactive({
        master %>% filter(date>=input$inDateRange[1] & date<input$inDateRange[2])
        })

    output$graph <- renderPlotly({
        plot_ly(reactiveMaster(), x = date, y = arrival.one, name = 'One Arrival',  visible = "legendonly") %>%
            add_trace(x= date, y = arrival.two, name = 'Two Arrival', mode = 'lines', visible = "legendonly" ) %>%
            layout( xaxis = list(title= "Date and Time",
                    rangeslider = list (type = "date"))) 
        })
}