I have the below ui.R and server.R
I have a simple dataset with 3 columns: Type of person, Longitude, Latitude
I want the Leaflet Map to react to a change in "Type of Person" <<< This is where i am stuck. I know i need to use some sort of observeEvent but not sure how to implement this.
ui.R
ui <- fluidPage(
selectInput(inputId = "Selector",
label = "Please Select Type of Person",
choices = c("Please Select:",
"Type A",
"Type B"),
selected = "Please Select:"),
leafletOutput("myMap")
)
shinyApp(ui=ui, server=server)
server.R
library(shiny)
server <- function(input,output){
output$myMap <- renderLeaflet({
leaflet() %>%
addTiles()%>%
addMarkers(data=SampleData,
~Longitude,
~Latitude)
})
}
Not exactly sure what you want your map to do, but it sounds like you are looking for leafletProxy
. For example, suppose you want to clear your markers and add new ones. When you use addMarkers
, be sure to include the group
parameter:
(Edited to provide a complete reproducible example. Note that the answer I gave previously is unchanged, I just added the data and the rest of the shiny app structure. Note also that the 'Please Select:' part is treated as a valid choice here, there are better ways to handle the select, but that is not what this question is about.)
library(shiny)
library(leaflet)
SampleData <- data.frame(type = c('Type A', 'Type B', 'Type A', 'Type B'),
Longitude = c(80.1, 80.2, 80.3, 80.4),
Latitude = c(40.1, 40.2, 40.3, 40.4))
ui <- fluidPage(
selectInput(inputId = "Selector",
label = "Please Select Type of Person",
choices = c("Please Select:",
"Type A",
"Type B"),
selected = "Please Select:"),
leafletOutput("myMap")
)
server <- function(input,output){
output$myMap <- renderLeaflet({
leaflet() %>%
addTiles()%>%
addMarkers(data=SampleData,
~Longitude,
~Latitude,
group = "myMarkers")
})
observeEvent(input$Selector, {
leafletProxy("myMap") %>%
clearGroup("myMarkers") %>%
addMarkers(data = SampleData[SampleData$type == input$Selector, ],
~Longitude,
~Latitude,
group = "myMarkers")
})
}
shinyApp(ui, server)