Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nclJoshCowley opened this issue Oct 17, 2024 · 0 comments

Comments

@nclJoshCowley
Copy link

nclJoshCowley commented Oct 17, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant