Is there a way to click on a polygon on a map in a shiny app to select an item, thus changing the item already selected in the dropdown? Ideally, the polygon selected will change the input$name
.
In the code below, I have just tested the observeEvent
function, but couldn't work out how to do what I want. The coordinates are just to see the output.
Example:
library(shiny)
library(sf)
library(tmap)
library(tidyverse)
library(leaflet)
# data in the sf package
nc <- st_read(system.file("shape/nc.shp", package="sf")) %>%
select(NAME, AREA)
nc_names <- nc %>%
st_set_geometry(NULL) %>%
distinct(NAME) %>%
pull()
# Define UI for application that draws a histogram
ui <- fluidPage(
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("name",
"name",
nc_names
)
),
# Create a spot for the barplot
mainPanel(
mainPanel(tmapOutput("map") )
)
)
)
server <- function(input, output) {
# filter polygon for map based on location selected
shp_selected <- reactive({
req(input$name)
nc %>% filter(NAME == input$name)
})
output$map <- renderTmap({
tmap_mode("view")
tm_shape(nc) +
tm_polygons() +
tm_shape(shp_selected()) +
tm_fill(col = "blue", alpha = 0.3)
})
# just testing here
observeEvent(input$map_shape_click, {
p <- input$map_shape_click # map because that is the name of the output
text <- paste("lat ", p$lat," lon ", p$lng) # shows lat/lon in console
print(text)
})
}
shinyApp(ui, server)