rshinygisr-leaflet

How to use a selectInput to map a subset of a dataframe?


enter image description here

I have a df containing the following columns: Species | latitude | longitude. I would like to create an app that allows the user to select a species, using selectInput, and have the long/lat information of that species plotted.

library(shiny)
library(leaflet)


fixInvase <- read.csv("fixed2011_buff_invasives.csv")

### Subsetted data that I would like to map

bTrefoil <- subset(fixInvase, Species == "Birdsfoot Trefoil", 
                   select = c(Species, latitude, longitude))
cThistle <- subset(fixInvase, Species == "Canada Thistle", 
                   select = c(Species, latitude, longitude))
cheatgrass <- subset(fixInvase, Species == "Cheatgrass", 
                     select = c(Species, latitude, longitude))
cBuckthorn <- subset(fixInvase, Species == "Common Buckthorn", 
                     select = c(Species, latitude, longitude))

...



ui <- fluidPage(
  titlePanel("2011 NWCA Invasive Species"),
  mainPanel(
    leafletOutput("map"),
    br(), br(),
    tableOutput("results")),
  sidebarPanel(
    ### User chooses the species to map
    selectInput("speciesInput", "Species",
                c("Birdsfoot Trefoil" = "bTrefoil", 
                  "Canada Thistle" = "cThistle",
                  "Cheatgrass" = "cheatgrass",
                  "Common Buckthorn" = "cBuckthorn"))
  ))

server <- function(input, output, session){

  ####
  output$map <- renderLeaflet({
    filtered <-
      fixInvase %>%
      filter(Species== input$speciesInput
      )
    leaflet(filtered) %>% addTiles() %>%
      fitBounds(~min(longitude), ~min(latitude), ~max(longitude), ~max(latitude))
  })
  output$results <- renderTable({
    filtered <-
      fixInvase %>%
      filter(Species == input$speciesInput
      )
    filtered
  })
  
}

shinyApp(ui, server)

In leaflet, I am able to plot a subset of the dataframe using:

bTrefoil <- subset(fixInvase, Species == "Birdsfoot Trefoil", 
                   select = c(Species, latitude, longitude))
leaflet(data = bTrefoil) %>% addTiles() %>%
  addMarkers(~longitude, ~latitude, popup = ~as.character(Species), label = ~as.character(Species))

But when I try to use selectInput to subset and plot the data I encounter errors.

Is there a way to subset the df based on the users selection in selectInput, and have leaflet map only those points?


Solution

  • Do not know why you subset before you run the app.

    I just build an example df (next time please provide a sample with dput())

    library(shiny)
    library(leaflet)
    library(dplyr)
    
    fixInvase <- structure(list(Species = structure(c(1L, 1L, 2L, 2L, 3L, 4L, 5L), .Label = c("Birdsfood Trefoil", "Canada Thistle", "Cheatgrass", "Common Buckthorn", "Common Reed"), class = "factor"), latitude = c("30.271", "48.288", "46.118", "36.976", "36.976", "42.416", "36.722"), longitude = c("-87.74", "-122.377", "-98.877", "-104.478", "-104.478", "-90.411", "-75.947")), .Names = c("Species", "latitude", "longitude"), row.names = c(NA, -7L), class = "data.frame")
    
    
    
    ui <- fluidPage(
      titlePanel("2011 NWCA Invasive Species"),
      mainPanel(
        leafletOutput("map"),
        br(), br(),
        tableOutput("results")),
      sidebarPanel(
        ### User chooses the species to map
        selectInput("speciesInput", "Species",
                    unique(fixInvase$Species))
      ))
    
    server <- function(input, output, session){
    
      ####
      output$map <- renderLeaflet({
        filtered <-
          fixInvase %>%
          filter(Species== input$speciesInput
          )
        leaflet(filtered) %>% addTiles() %>%
          fitBounds(~min(longitude), ~min(latitude), ~max(longitude), ~max(latitude))%>% 
          addMarkers(~as.numeric(longitude), ~as.numeric(latitude), popup = ~as.character(Species), label = ~as.character(Species))
      })
      output$results <- renderTable({
        filtered <-
          fixInvase %>%
          filter(Species == input$speciesInput
          )
        filtered
      })
    
    }
    
    shinyApp(ui, server)
    

    Also added addMarkers to show where the species is found