【发布时间】:2016-11-03 02:02:10
【问题描述】:
我对 R 比较满意,但对 Shiny 不太满意,尽管这不是我的第一个 Shiny 应用程序。
我有一个数据框,其中包含 lon/lat 以及每个新客户在系统中输入的日期/时间。我还根据 startDate 变量创建了其他变量,例如年、月、周、年月 (ym) 和年周 (yw):
id lat lon startDate year month week ym yw
1 1 45.53814 -73.63672 2014-04-09 2014 4 15 2014-04-01 2014-04-06
2 2 45.51076 -73.61029 2014-06-04 2014 6 23 2014-06-01 2014-06-01
3 3 45.43560 -73.60100 2014-04-30 2014 4 18 2014-04-01 2014-04-27
4 4 45.54332 -73.56000 2014-05-30 2014 5 22 2014-05-01 2014-05-25
5 5 45.52234 -73.59022 2014-05-01 2014 5 18 2014-05-01 2014-04-27
我想用传单映射每个客户(已完成),但我想通过仅显示特定日期范围内的新客户来为我的应用程序设置动画。
我想逐月查看日期(ym 变量:2016-01-01、2016-02-01、2016-03-01...),而不是按天(或已支持的 x 天)因为每月的日期并不总是下个月的 30 天。 这是我目前的申请:
library(shiny)
library(leaflet)
library(dplyr)
df <- data.frame(id = 1:5,
lat = c(45.53814, 45.51076, 45.4356, 45.54332, 45.52234),
lon = c(-73.63672, -73.61029, -73.6010, -73.56000, -73.59022),
startDate = as.Date(c("2014-04-09", "2014-06-04", "2014-04-30", "2014-05-30", "2014-05-01")),
year = c(2014, 2014, 2014, 2014, 2014),
month = c(4, 6, 4, 5, 5),
week = c(15, 23, 18, 22, 18),
ym = as.Date(c("2014-04-01", "2014-06-01", "2014-04-01", "2014-05-01", "2014-05-01")), # Year-Month
yw = as.Date(c("2014-04-06", "2014-06-01", "2014-04-27", "2014-05-25", "2014-04-27")) # Year-Week
)
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width = "83%", height = "100%"),
absolutePanel(
top = 1,
right = 10,
div(
style = "height: 80px;",
sliderInput(
"time",
"Time Slider",
min(df$month),
max(df$month),
value = c(min(df$month), max(df$month)),
step = 1,
animate = animationOptions(interval = 2500)
) # end sliderInput
) # end div
) # end absolutePanel
) # end bootstrapPage
server <- shinyServer(function(input, output, session){
output$map <- renderLeaflet({
leaflet(data = df %>% filter(month >= input$time[1], month <= input$time[2])) %>% addTiles() %>%
addMarkers(~lon, ~lat) %>%
setView(lng = -73.6, lat = 45.52, zoom = 12)
})
})
shinyApp(ui = ui, server = server)
问题:如何使用滑块动画选项过滤数据以切换到下个月等?现在我循环遍历变量月份,但我有 8 年的数据,所以我还需要考虑年份,例如循环遍历 ym 变量。
我看到 here 和 here 完成了一些工作,但要么它没有响应我的需求,要么我不理解提供的 js 代码。如果是这样,如何更改我的代码以反映我的需求?
谢谢。
【问题讨论】: