试图在R传单中添加时间滑块

时间:2019-11-28 15:23:57

标签: r leaflet

我正在尝试分析CT中的啤酒厂。我想要做的是制作一个啤酒厂地图,并添加一个滑块,该滑块会根据其开业年份在地图上显示每个啤酒厂的图标。我还想在地图上添加一个计数器,该计数器在您移动年份滑块时显示啤酒厂的总数。我在R中使用Leaflet包制作地图

这是数据的一部分:

    Name  City      Latitude    Longitude     Opened   YearOpened
1   BAR New Haven   41.30629    -72.93042   1995-01-01  1995
2   Thomas Hooker Brewing Co.   Bloomfield  41.79564    -72.52945   1996-01-01  1996
3   Southport Brewing Company (SBC Restaurant & Brewery)    Southport   41.13949    -73.27795   1997-01-01  1997
4   Willimantic Brewing Co. Willimantic 41.71315    -72.21715   1997-01-01  1997
5   Cottrell Brewing Co.    Pawcatuck   41.37010    -71.83370   1997-02-28  1997
6   City Steam Brewery  Hartford    41.76814    -72.67283   1997-11-01  1997
7   Olde Burnside Brewing Company   East Hartford   41.78485    -72.60069   2000-01-01  2000

这是我用来制作地图的代码:

 beer_icon <- makeAwesomeIcon(icon = 'beer', library = 'fa', markerColor = 'beige')
 map <- breweries_subset %>%
      leaflet() %>%
      addProviderTiles(provider = 'Esri.WorldStreetMap') %>%
      addAwesomeMarkers(icon = beer_icon,
                        group = 'Breweries',
                        popup = ~ Name)

1 个答案:

答案 0 :(得分:0)

如果您对shiny解决方案感兴趣:

library(shiny)
library(leaflet)

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(bottom = 30, right = 10,
                textOutput("Counter"),
                sliderInput("Year", "Year", 1990, 2000, value = 1995, step = 1, sep = "")
  )
)

server <- function(input, output, session) {

  sliderData <- reactive({
    breweries_subset %>%
      filter(YearOpened <= input$Year)
  })

  output$Counter <- renderText(
    paste("Number Breweries: ", nrow(sliderData()))
  )

  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      fitBounds(min(breweries_subset$Longitude), min(breweries_subset$Latitude),
                max(breweries_subset$Longitude), max(breweries_subset$Latitude))
  })

  observe({
    leafletProxy("map", data = sliderData()) %>%
        clearMarkers() %>%
        addProviderTiles(provider = 'Esri.WorldStreetMap') %>%
        addAwesomeMarkers(icon = beer_icon,
                          group = 'Breweries',
                          popup = ~ Name)
  })
}

shinyApp(ui = ui, server = server)

breweries in CT