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

时间:2016-12-29 17:22:32

标签: r shiny plotly dashboard interactive

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

我将图表整合到闪亮的仪表板中。我想使用daterangeInput来选择X轴的日期范围。但我无法弄清楚如何将daterangeInput集成为反应性/交互式。

任何帮助将不胜感激!

Plotly图表,已合并:

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 <- 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({
??????????

1 个答案:

答案 0 :(得分:2)

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"))) 
        })
}