我用 R 制作了一个 Plotly 图表,其中包含线条、散点以及填充区域变量。 X 轴是采用 as.POSIXCT 格式的日期和时间,格式为“%Y-%m-%d %H:%M:%S”。每隔 10 分钟就有 2 个月的数据。 Y 轴是单位数。
我正在将图表集成到 Shiny 的仪表板中。我想使用 daterangeInput 来选择 X 轴的日期范围。不过,我不知道如何将 daterangeInput 集成为响应式(Reactive)/交互式的。
如有任何帮助,我们将不胜感激!
综合 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({
??????????
最佳答案
在您的 server
函数中,您必须创建一个响应式函数,以便在每次 DateRange 更改时过滤数据集。你称其为响应式(Reactive)来绘制你的 plotly :
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")))
})
}
关于r - Plotly 和 Shiny Dashboard - 使用 daterangeinput 更改绘图中日期轴上的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41384393/