I have come across a reproducible example that I found at the following link: https://bl.ocks.org/timelyportfolio/5ab450e90ee510f4df9758b9ec5a8ad0.
library(sf)
library(plotly)
library(leaflet)
library(crosstalk)
library(htmltools)
boroughs_data <- st_read("http://services5.arcgis.com/GfwWNkhOj9bNBqoJ/arcgis/rest/services/nybb/FeatureServer/0/query?where=1=1&outFields=*&outSR=4326&f=geojson")
boroughs_data$x <- seq(1:5)
boroughs_data$y <- seq(2,10,2)
boroughs_shared_data <- SharedData$new(
boroughs_data,
key=~BoroCode,
group = "boroughs"
)
map_visualization <- leaflet(boroughs_shared_data) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons(
data=boroughs_data,
layerId = ~BoroCode,
color = "#444444",
weight = 1,
smoothFactor = 0.5,
opacity = 1.0,
fillOpacity = 0.5,
fillColor = ~colorQuantile("Greens", x)(x)
)
add_select_script <- function(lf, styleFalse, styleTrue, ns="") {
// JavaScript function for toggling selection
}
browsable(
tagList(
tags$div(
style = "float:left; width: 49%;",
add_select_script(
map_visualization,
styleFalse = list(fillOpacity = 0.2, weight = 1, opacity = 0.4, color="black"),
styleTrue = list(fillOpacity = 0.7, weight = 3, opacity = 0.7, color="blue")
)
),
tags$div(
style = "float:left; width: 49%;",
plot_ly(boroughs_shared_data, x = ~x, y = ~y) %>%
add_markers(alpha = 0.5,text = ~paste('Borough: ', BoroName)) %>%
highlight(on = "plotly_selected")
)
)
)
I made a small adjustment to the code compared to the original source so that now polygons are highlighted on mouseover instead of click.
Since my knowledge of JavaScript is limited, I am unsure what modifications are necessary to ensure that the selection of polygons is not constant (meaning the highlight effect only appears during mouseover and does not persist after the mouse moves away from the polygon). Could you provide some guidance on this?