在Leaflet和Shiny中按日期过滤数据

时间:2019-05-21 23:10:15

标签: r shiny leaflet react-leaflet

我正在使用仪表板,想按日期过滤传单地图。

当前,我尝试使用Change data on leaflet plot based on inputted date (R, Shiny)中提供的示例按日期隔离数据,但到目前为止仅绘制了所有数据点。请稍微缩小绘图以查看绘图。

以前,我尝试将日期转换为字符,但也没有运气。

编辑:下面的代码现在可以工作并按日期过滤。

### Generate a dataset ###
start_date <- as.Date('2018-01-01')  
end_date <- as.Date('2019-05-10')   
set.seed(1984)
date1 <- as.Date(sample( as.numeric(start_date): as.numeric(end_date), 988, 
                replace = T), origin = '1970-01-01')
group <- rep(letters[1:26], each = 38)
x1 <- runif(n = 988, min = 3.26, max = 10)
latitude <- runif(988,40.75042,50.75042)
longitude <- runif(988,-73.98928,-63.98928)
dataframe <- cbind(data.frame(date1,group,x1,latitude,longitude))

library(lubridate)
dataframe$date <- ymd(dataframe$date1)

library(shiny)
library(leaflet)
dataframe$defectrateLvl <- cut(dataframe$x1, 
                         c(3.26,6,100), include.lowest = T,
                         labels = c('3.26-6x','6x+')) 
beatCol <- colorFactor(palette = c('yellow', 'red'), dataframe$defectrateLvl)


ui <- fluidPage(
  dateInput(inputId = "date", label="Select a date", value = "2019-03-01", min = "2018-01-01", max = "2019-05-10",
            format = "yyyy-mm-dd", startview = "month",
            language = "en", width = NULL),
  leafletOutput("map")

)

server <- shinyServer(function (input, output) {
  dailyData <- reactive(dataframe[dataframe$date == format(input$date, '%Y/%m/%d'), ] )
  output$map <- renderLeaflet({
  dataframe <- dailyData()  # Added this in attempt to integrate
    dataframe %>% leaflet() %>% 
      setView(lng = -73.98928, lat = 40.75042, zoom = 10) %>%
      addProviderTiles("CartoDB.Positron", options = providerTileOptions(noWrap = TRUE)) %>%
      addCircleMarkers(
        lng=~dataframe$longitude, # Longitude coordinates
        lat=~dataframe$latitude, # Latitude coordinates
        #radius=~defectrateLvl, # Total count
        popup =~ dataframe$group,
        color = ~beatCol(dataframe$defectrateLvl),
        fillOpacity=0.5 # Circle Fill Opacity
      )
    #)
  })
})

shinyApp(ui, server)

I expect the "select a date" mechanism to work. I.e. plot on the data associated with the respective date. Thank you for your time. 

0 个答案:

没有答案