Search code examples
rshinyr-leaflet

Using the R Shiny to show all data points as the default


I want to map the spatial distributions of some data points using R Shiny. When I add the filter for different regions into the R script, it cannot show all the data points for all the regions at the default map.

Any suggestions on how to improve the script?

The below is my R script with an example data:

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

# generate the data
dat <- data.frame(
  region=sample(c("A","B","C"),100,replace=T),
  x=rnorm(100, mean=50, sd=10),
  y=rnorm(100, mean=40, sd=10)
)

# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
  theme = shinytheme("united"),
  # Application title
  titlePanel("Test"),
  
  # Sidebar
  sidebarLayout(
    sidebarPanel(
      selectizeInput(
        "regionInput", label=h3("Region"),choices=c("A","B","C"))
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      leafletOutput(outputId = 'map')
    )
  )
)
)

# Define server logic to map the distribution
server <- shinyServer(function(input, output) {
  
  output$map <- renderLeaflet({
    leaflet(dat) %>%
      addProviderTiles("Esri.WorldImagery") %>%
      addCircleMarkers(color = "blue", radius = 2, stroke = FALSE, fillOpacity = 0.5, lng=dat$x[which(dat$region == input$regionInput)], lat=dat$y[which(dat$region == input$regionInput)])
  })
  
  
})

# Run the application 
shinyApp(ui = ui, server = server)

Solution

  • You need to add a (All) element to your dropdown and add the logic in your render funciton like this:

    library(shiny)
    library(shinythemes)
    library(leaflet)
    
    # generate the data
    dat <- data.frame(
      region = sample(c("A", "B", "C"), 100, replace = TRUE),
      x = rnorm(100, mean = 50, sd = 10),
      y = rnorm(100, mean = 40, sd = 10)
    )
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
      theme = shinytheme("united"),
      # Application title
      titlePanel("Test"),
      
      # Sidebar
      sidebarLayout(
        sidebarPanel(
          selectizeInput(
            "regionInput", 
            label = h3("Region"),
            choices = c("(All)", "A", "B", "C"))
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
          leafletOutput(outputId = "map")
        )
      )
    )
    
    
    # Define server logic to map the distribution
    server <- shinyServer(function(input, output) {
      
      output$map <- renderLeaflet({
        if (input$regionInput == "(All)") {
          idx <- rep(TRUE, nrow(dat))
        } else {
          idx <- dat$region == input$regionInput
        }
        leaflet(dat) %>%
          addProviderTiles("Esri.WorldImagery") %>%
          addCircleMarkers(color = "blue",
                           radius = 2, 
                           stroke = FALSE, 
                           fillOpacity = 0.5, 
                           lng = dat[idx, "x"], 
                           lat = dat[idx, "y"])
      })
    })
    
    # Run the application 
    shinyApp(ui, server)
    

    Another Option would be to allow for multiple selects in your selectizeInput:

    selectizeInput(
           "regionInput", 
           label = h3("Region"),
           selected = c("A", "B", "C"),
           choices = c("A", "B", "C"),
           multiple = TRUE)
    

    and then change the logic in the server to:

    idx <- dat$region %in% input$regionInput
    

    Then you can select multiple regions (for example all).