Search code examples
rshinyleafletr-leaflet

Creating interactive Leaflet map in R with shiny


I created a shiny app with leaflet and it works pretty well.

library(shiny)
library(shinythemes)
library(leaflet)

ui2 <- fluidPage(theme = shinytheme("united"), tabsetPanel(

  tabPanel(
           titlePanel("titel"),
           
           mainPanel(
             
             leafletOutput(outputId = "mymap")),
           
           sidebarPanel(
             fluidRow(
               
               dateRangeInput("a", h4("date"),language = "en",separator = " to "),
               selectInput("select", h4("location"),
                           c(data8$city)),
               submitButton("search"))
           ))
  )
)

server <- function(input, output) {
  
  popupa <- paste(titel)
    
  output$mymap <- renderLeaflet({
    leaflet(data8) %>%
      addTiles() %>%
      addMarkers(lng = ~lng, lat = ~lat, popup = popupa)
  }) 
}
shinyApp(ui2, server)

But at the moment I am trying to add a dateRangeInput to filter(date_start) on my shown locations. But I don't know how to connect my dateRangeInput and the selectInput to my leaflet-function in the server-part. Furthermore, below the map there should be a table with the filtered locations from the map - is this possible at all?

My used dataframe looks like following:

title=c("Event1","Event2")
lng=c(23.3, 23.3)
lat=c(30, 40)
city=c("Berlin", "Hamburg" )
zip=c(39282, 27373)
date_start=c("2018-05-28","2018-05-28")
date_end=c("2018-06-27","2018-08-03")
data8 <- data.frame(title, lng, lat, city, zip, date_start, date_end)

Does anyone know how to get this done? Thanks for every help! regards


Solution

  • You could try this:

    ui2 <- fluidPage(theme = shinytheme("united"), tabsetPanel(
    
      tabPanel(
        titlePanel("titel"),
    
        mainPanel(
    
          leafletOutput(outputId = "mymap"),
          dataTableOutput("mytable")),
    
        sidebarPanel(
          fluidRow(
    
            dateRangeInput("a", h4("date"),language = "en",separator = " to "),
            selectInput("selectLoc", h4("location"),
                        as.character(data8$city)),
            submitButton("search"))
        ))
    )
    )
    
    server <- function(input, output) {
    
      popupa <- paste("titel")
    
      datatoPlot <- reactive({
    
    
        date_start <- as.character(input$a[1])
        date_end <- as.character(input$a[2])
    
        data8$date_start <- as.Date(data8$date_start, format = "%Y-%m-%d")
        data8 <- data8[as.Date(data8$date_start) >= date_start & as.Date(data8$date_start) <= date_end, ]
        data8 <- data8 %>% dplyr::filter(city == input$selectLoc)
    
      })
    
      output$mymap <- renderLeaflet({
        leaflet(datatoPlot()) %>%
          addTiles() %>%
          addMarkers(lng = ~lng, lat = ~lat, popup = popupa)
      }) 
    
    
      output$mytable <- renderDataTable(datatoPlot())
    
    }
    shinyApp(ui2, server)
    

    enter image description here