I have a multipolygon in a Leaflet map in a R Shiny app. There is always one subpolygon selected to be displayed while the other subpolygons are hidden (i.e. it's fill is transparent and the others are filled white). Additionally, the selected subpolygon's border is drawn thicker than those of the others. In the end, it shall look like this:

So far, so good. The thing is, the line thickness of the selected subpolygon deviates: along shared borders, it seems, the thick line can only appear once (meaning, if there is a thick line on subpolygon South Australia, the neighboring polygon Queensland will have a thin line along this shared border) so that the thick line would appear exactly once across the whole subpolygon. I know, sounds bizarre, but as far as I have seen...

The multipolygon was created and processed in QGIS and (besides this line thickness thingy) appears quite normal. When inspecting the borders in QGIS, the connected points along the shared border seemed regular. I tried to deduplicate potentially duplicate shared borders (there were non) and concluded, it's not a problem of transparency settings.

The MWE comes with an exemplary multipolygon of the Australian provinces, which behave the same as my multipolygon (couldn't figure out where I could savely upload it, sorry):


              destfile = "~/lakes.gpkg")
downunder <- st_read("~/lakes.gpkg")
downunder <- st_transform(downunder, "+proj=longlat +datum=WGS84")

ui <- fluidPage(
    "choose district",
    choices = downunder$Province,
    selected = sample(downunder$Province,
                      size = 1)
                width = "800px",
                height = "800px")

server <- function(input, output) {
  # transparency for selected district
  transparency_react <- eventReactive(input$district, {
    transparency <- rep(1, length(downunder$Province))
    transparency[grep(input$district, downunder$Province)] <- 0

  # border thicker for selected district
  outline_react <- eventReactive(input$district, {
    outline <- rep(1, length(downunder$Province))
    outline[grep(input$district, downunder$Province)] <- 12

  output$map <- renderLeaflet({
    leaflet() |>
      addTiles() |>
      addPolygons(data = downunder,
                  fillColor = "white",
                  fillOpacity = ~ transparency_react(),
                  color = "black",
                  opacity = 1,
                  weight = outline_react())

shinyApp(ui = ui, server = server)

Can someone give me a hint what is wrong here?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.