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?