Search code examples
rshinyr-leaflet

How to make slider inputs with Leaflet and shiny in R?


I have two dataframes as follows. I have only put partial data for simplicity:

**order**
Location     Order_count    Longitude   Latitude
New York      12            74.0060     40.7128
San Francisco 7             122.4194    37.7749
Miami         13            80.1918     25.7617
Texas         20            99.9018     31.9686
Dallas        1             96.7970     32.7767
Mary          0             27.6648     81.5158
Whitney       9             32.3182     86.9023

**arrivals**
Cust_id  Arrival_time   Location
1         3             New York
2         18            Miami
3         20            Texas
4          1            New York
5          0            Dallas
6         18            Miami
7         20            Dallas
8          1            New York
  1. At first, I mapped the locations on a leaflet using symbols by the longitude and latitude from order dataframe.

  2. Then I have put a selection slider that selects arrival_time. Based on the time selected, the number of customers on that location (on the map) should change. The map also shows total arrivals over all times in that particular location. For example, if I select Arrival_time=1, if I click New York on the map, it should say,
    "New York",

    "Number of customers for the selected time: 2",

    "Total number of customers: 3".

I have coded in the following ways:

**ui**
shinyUI(fluidpage(
  titlePanel("Time"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("time", "Hours selected ", min=0, max=24, value=12)
    ), 
    
    mainPanel()),
  leafletOutput("mymap", height = "900", width = "900")))

**server**


shinyServer(function(input, output){
  
  data_input <- reactive({arrivals%>% 
      filter(Arrival_time== input$time) %>% 
      group_by(Location) %>% count()
  })
  
  output$mymap <- renderLeaflet({
    leaflet(data=order) %>% addTiles() %>% 
      
      addMarkers(~Longitude, ~Latitude, popup = paste(order$Location,
      "Total arrivals for the selected time: ", arrivals$data_input," 
       order$Order_count))
  })})

The map does not change with the input provided from the slider. I don't know where I'm doing wrong.


Solution

  • Replace arrivals$data_input by data_input()$n

    EDIT : based on our comments, your initial problem looks more like merging your different datasets

    library(shiny)
    library(leaflet)
    library(dplyr)
    
    order <- data.frame(
      Location = c('New York', 'San Francisco', 'Miami', 'Texas', 'Dallas', 'Mary', 'Whitney'),
      Order_count = c(12, 7, 13, 20, 1, 0 ,9),   
      Longitude = c(-74.0060, -122.4194, -80.1918, -99.9018, -96.7970, -81.5158, -86.9023),
      Latitude = c(40.7128, 37.7749, 25.7617, 31.9686, 32.7767, 27.6648, 32.3182)
    )
    
    arrivals = data.frame(
      Cust_id  = c(1,2,3,4,5,6,7,8),
      Arrival_time = c(3,18,20,1,0,18,20,1),
      Location = c('New York', 'Miami', 'Texas', 'New York', 'Dallas', 'Miami', 'Dallas', 'New York')
    )
    
    
    # Make a dataframe containing needed variables by merging data
    join_data <- order %>%
      inner_join(arrivals %>% 
                  group_by(Location, Arrival_time) %>% 
                  summarise(number_of_customer = n()),
                by = "Location") %>%
      left_join(arrivals %>% 
                  group_by(Location) %>% 
                  summarise(total_number_of_customer = n()),
                by = "Location")
    
    ui <- shinyUI(fixedPage(
      titlePanel("Time"),
      sidebarLayout(
        sidebarPanel(
          sliderInput("time", "Hours selected ", min=0, max=24, value=1)
        ), 
        
        mainPanel()),
      leafletOutput("mymap", height = "900", width = "900")))
    
    
    
    server <- shinyServer(function(input, output, session){
      
      data_input <- reactive({
        join_data %>%
          filter(Arrival_time==input$time)
      })
      
      output$mymap <- renderLeaflet({
        leaflet(data=order) %>% 
          addTiles() %>% 
          addMarkers(
            ~Longitude, 
            ~Latitude, 
            popup = paste(
              order$Location, "<br>",
              "Number of customers for the selected time ", data_input()$number_of_customer," 
              <br>","Total number of customers: ", data_input()$total_number_of_customer))
      })})
    
    shinyApp(ui, server)