Open bdbmax opened 2 years ago
Technically this is the intended behaviour as the index is used/returned in the onClick
functions from here
var eventInfo = {
index: info.index,
color: info.color,
object: info.object,
layerId: info.layer_id,
lat: info.coordinate[1], // deck.gl 8.4 - https://deck.gl/docs/upgrade-guide#upgrading-from-deckgl-v83-to-v84
lon: info.coordinate[0]
};
eventInfo = JSON.stringify( eventInfo );
Shiny.onInputChange(map_id + "_" + layer + "_click", eventInfo);
I think the actual issue is the id
argument in the R functions; I'm not sure they do anything and may be left over from a very early iteration of this library.
But thanks for bringing this up; the documentation certainly needs to be clearer.
Good, interesting. I got mixed up between the different output of the onClick event depending on the layers used.
add_polygon
offers the possibility to retrieve the supplied ID (from the id argument in the R function) from a click through jsonlite::fromJSON(input$map_polygon_click)$object$properties$id
, which is a great feature we highly rely on.
library(mapdeck)
library(shiny)
sf <- geojsonsf::geojson_sf("https://symbolixau.github.io/data/geojson/SA2_2016_VIC.json")
ui <- fluidPage(
mapdeckOutput("map"),
htmlOutput("selection")
)
server <- function(input, output, session) {
# Map
output$map <- renderMapdeck(mapdeck() |>
add_polygon(
data = sf,
id = "SA2_MAIN16",
tooltip = "SA2_MAIN16"))
# Selection
select_id <- eventReactive(input$map_polygon_click, {
jsonlite::fromJSON(input$map_polygon_click)$object$properties$id
})
output$selection <- renderText(paste("Selected output: ", select_id()))
}
shinyApp(ui = ui, server = server)
A click on any polygon return its id (its SA2_MAIN16 value in this example). Reclicking on the same polygon then returns NULL, signaling a deselection.
The same functionality would be interesting for point data!
Oh that's interesting; the scatterplot object returned from deck.gl does not contain the object
, (first console.log() in the screenshot), whereas the object returned for the polygon does contain the object
, which is why you get the Id
returned.
Describe the bug Within shiny, when a point is clicked on a scatterplot, it retrieves the wrong id supplied through add_scatterplot. It seems it is retrieving the row number -1.
To Reproduce
Expected behaviour That the
jsonlite::fromJSON(input$map_scatterplot_click)$index
returns the supplied id (inadd_scatterplot
) of the point clicked on.Screenshots
Versions 0.3.4