Skip to content

Difficulty passing layerId to addPolygons (on click polygon ID in Shiny) #494

@nclJoshCowley

Description

@nclJoshCowley

I'm new to this space so apologies if this is user error.

Suppose we want to display mapview::franconia in Shiny and get the ID of the polygon that a user clicks.

Using leaflet, one could pass layerId to the addPolygons function. For example,

ui <-
  shiny::bootstrapPage(
    shiny::tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
    leaflet::leafletOutput("map", width = "100%", height = "100%")
  )

server <- function(input, output, session) {
  output$map <- leaflet::renderLeaflet({
    mapview::franconia |>
      leaflet::leaflet() |>
      leaflet::addProviderTiles("CartoDB.Positron") |>
      leaflet::addPolygons(layerId = mapview::franconia$NUTS_ID)
  })

  shiny::observeEvent(input$map_shape_click, {
    # Looking for the `$id` element to be "DE268" or similar
    print(input$map_shape_click)
  })
}

shiny::shinyApp(ui, server)

According to the docs on ?mapview, we should be able to pass this argument to addPolygons. That is,

ui <-
  shiny::bootstrapPage(
    shiny::tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
    leaflet::leafletOutput("map", width = "100%", height = "100%")
  )

server <- function(input, output, session) {
  output$map <- leaflet::renderLeaflet({
    mv <-
      mapview::mapview(mapview::franconia, layerId = mapview::franconia$NUTS_ID)

    mv@map
  })

  shiny::observeEvent(input$map_shape_click, {
    # Looking for the `$id` element to be "DE268" or similar
    print(input$map_shape_click)
  })
}

shiny::shinyApp(ui, server)

However,

  • Passing no extra arguments, the click information returns id as NULL
  • Passing the layerId results in

    Error in validateScalarName: Invalid argument 'name' (must be a non-empty character string and contain no '/' or '')

One workaround would be use the latitude and longitude information returned but this is not ideal.

Edit: Another workaround would be to use mapview::mapviewOptions(fgb = FALSE), is it possible to get the same functionality with fbg?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions