rshinyr-leaflet

integrating leaflet map in RShiny - inputselect by country and symptom


I am attempting to create a map within Shiny R, with leaflet. I have several questions:

  1. How to create the map by selecting Symptoms by Country? as you can see data contains countries and symptoms in rows (see link on GitHub provided bellow). if I want to filter by a certain country and certain symptom how do I do this with leaflet in Shiny r?

  2. I want created a draggable drop down menu (where symptoms can be chosen - see question 1) since I cannot adjust the map on the entire screen. An example of a draggable drop down menu, called 'Zip Explored' I have tried to replicate, with no success is here - https://shiny.rstudio.com/gallery/superzip-example.html

  3. I could not make the map to show on the entire screen. Is there a way to show the map on the entire screen? Just like the example in the web-link on 2nd point.

here is the code:

library(shiny)
library(cvindia)
library(tidyverse)
library(shinydashboard)


server = function(input, output, session){}




    ui <- fluidPage(
    
        # Application title
        h1("Symptoms accross the world"),
    
        # Sidebar with a slider input for number of bins 
        selectInput("productCategory", "Select Country", c( "Bangladesh", "India", "Nigeria", "Pakistan", "United Kingdom")), 
        selectInput("productCategory", "Symptom", c("Chills", "Cough", "Muscle Ache"))
    )
    
    server <- function(input, output) {
        
        
    }
    
    
    # Run the application 
    shinyApp(ui = ui, server = server)

If the above code is run, then I have easily managed to create the selectInput by country and symptom.

Here is the second code I have which I do not understand how it should interact with the server, having in mind the values I am interested, and presume it should interact with user interface are in the rows:

leaflet() %>%
  addTiles()

map <- leaflet(gather_divided) %>% addTiles() %>% addMarkers(clusterOptions = markerClusterOptions())

map

Sample of a dataset is on my GitHub as I haven't found a more elegant way of deploying it to stack overflow:

https://github.com/gabrielburcea/stackoverflow_fake_data/tree/master


Solution

  • Here is a brief demo that I hope will be helpful.

    A few notes:

    This also might be helpful reference for using leaflet with shiny:

    https://rstudio.github.io/leaflet/shiny.html

    Let me know if this is what you had in mind.

    library(shiny)
    library(tidyverse)
    library(leaflet)
    
    fake_data <- read.csv("https://raw.githubusercontent.com/gabrielburcea/stackoverflow_fake_data/master/gather_divided.csv")
    
    ui <- fluidPage(
      
      # Application title
      h1("Symptoms accross the world"),
      
      # Inputs for country and symptom 
      selectInput("country", "Select Country", c("Bangladesh", "India", "Nigeria", "Pakistan", "United Kingdom"), multiple = TRUE), 
      selectInput("symptom", "Symptom", c("Chills", "Cough", "Muscle Ache"), multiple = TRUE),
      
      # Output with map
      h2("Map"),
      leafletOutput("map")
      
    )
    
    server <- function(input, output) {
      
      filtered_data <- reactive({
        fake_data %>%
          filter(Country %in% input$country,
                 Symptom %in% input$symptom)
      })
      
      output$map <- renderLeaflet({
        leaflet() %>%
          addTiles() %>%
          addMarkers(data = filtered_data())
      })
      
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    Edit:

    To make your selectInput appear in a floating, draggable menu, you can use absolutePanel (as in the example you referenced).

    Note that the example uses custom .css which can improve the appearance of your shiny app further.

    ui <- fluidPage(
      
      h1("Symptoms accross the world"),
      leafletOutput("map"),
      
      # Floating panel
      absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
                    draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
                    width = 330, height = "auto",
                    
                    h2("Data Explorer"),
                    
                    # Inputs for country and symptom 
                    selectInput("country", "Select Country", c("Bangladesh", "India", "Nigeria", "Pakistan", "United Kingdom"), multiple = TRUE), 
                    selectInput("symptom", "Symptom", c("Chills", "Cough", "Muscle Ache"), multiple = TRUE)
      )
    )