R Shiny date slider animation by month (currently by day)

后端 未结 2 2038
悲&欢浪女
悲&欢浪女 2021-01-01 00:38

I\'m somewhat comfortable with R, lot less with Shiny, though it\'s not my first Shiny application.

I have a data frame with lon/lat and the date/time of the entry i

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-01 00:49

    Victorp solution works great, kudos! I'll post the code of the final solution integrated with the op. If anyone else wants to run this code, don't forget to include Victorp's sliderValues function.

    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
    )
    
    # List of months
    choices_month <- seq.Date(from = as.Date("2014-01-01"), by = "month", length.out = 36)
    
    # ui
    ui <- bootstrapPage(
      tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
    
      leafletOutput("map", width = "75%", height = "100%"),
    
      absolutePanel(
    top = 1,
    right = 10,
    
    div(
      style = "height: 180px;",
    # custom slider function
    sliderValues(
      inputId = "test", label = "Month", width = "100%",
      values = choices_month[4:6], 
      from = choices_month[4], to = choices_month[6],
      grid = FALSE, animate = animationOptions(interval = 1500)
    ), # end sliderInput
    verbatimTextOutput("res")
        ) # end div
      ) # end absolutePanel
    ) # end bootstrapPage
    
    server <- shinyServer(function(input, output, session){
    
      output$map <- renderLeaflet({
    #    leaflet(data = df %>% filter(ym > as.Date(input$test[1]), ym < as.Date(input$test[2]))) %>% addTiles() %>% 
     leaflet(data = df %>% filter(ym == input$test[1])) %>% addTiles() %>% 
      addMarkers(~lon, ~lat) %>% 
      setView(lng = -73.6, lat = 45.52, zoom = 12)
      }) # end map
    
      output$res <- renderPrint({
        print(input$test) # you have to split manually the result by ";"
        print(as.Date(unlist(strsplit(input$test, ";"))))
        }) # end res
    }) # end server
    
    # App
    shinyApp(ui = ui, server = server)
    

提交回复
热议问题