Thanks @jlacko I have changed it to use leaflet
and now it prints the NAME
upon clicking on the map. Do you know how I can change the dropdown to be the same as what is selected in the map?
library(shiny)
library(sf)
library(tidyverse)
library(leaflet)
# data in the sf package
nc <- st_read(system.file("shape/nc.shp", package="sf")) %>%
select(NAME, AREA)
# unique names
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(leafletOutput("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 <- renderLeaflet({
leaflet() %>%
addProviderTiles("Stamen.TonerHybrid") %>%
addPolygons(data = nc,
fillColor = NULL,
color = "grey",
weight = 0.1,
layerId = ~NAME) %>%
addPolygons(data = shp_selected(),
fillColor = "blue",
color = "grey",
fillOpacity = 0.5,
layerId = ~NAME)
})
#click on polygon
observe({
event <- input$map_shape_click
print(event$id)
})
# map selection - not sure if this is the way to do it - still needs to be assigned to be the same as the dropdown
output$map_selection <- reactive({
req(event$id)
event$id
})
}
shinyApp(ui, server)