1

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

2
  • It is not clear how you want to filter your dataset. With date_start? date_end?
    – MLavoie
    Commented Jun 25, 2018 at 11:07
  • With date_start sorry
    – Leon
    Commented Jun 25, 2018 at 11:17

1 Answer 1

2

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

1
  • Yeahr this solution is pretty dope! Can you also show me how to get use of the location-field in the same way? I allready tried to create a nwe variable in the data0Plot <- reactive() function with as.character(input$select[1]) but if change the location in the dropdown and submit it via "search" it won't change my table nor my map. I would be very grateful to you :)
    – Leon
    Commented Jun 25, 2018 at 14:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.