I am following this R tutorial here (towards the end of the page) : https://glin.github.io/reactable/articles/examples.html. I would like to make an interactive map that allows you to filter points on the map - i.e. when you click on the table rows, points on the map appear and disappear.
I decided to generate my own data for this problem. I am following the last part where they make a map/table:
library(leaflet)
library(htmltools)
library(crosstalk)
library(reactable)
library(htmlwidgets)
myFun <- function(n = 5000) {
a <- do.call(paste0, replicate(5, sample(LETTERS, n, TRUE), FALSE))
paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE))
}
id = 1:1000
long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
my_data_1 = data.frame(id, lat, long, var1 = myFun(1000), var2 = myFun(1000), var3 = myFun(1000) )
Then, I manipulated the data according
brew_sp <- SharedData$new(my_data_1, group = "breweries")
brew_data <- as_tibble(my_data_1) %>%
select(var1, var2, var3) %>%
SharedData$new(group = "breweries")
I then tried to make the map (I added a small change to the map):
map <- leaflet(brew_sp) %>%
addTiles() %>%
addMarkers(clusterOption=markerClusterOptions())
And then the table:
tbl <- reactable(
brew_data ,
selection = "multiple",
filterable = TRUE,
searchable = TRUE,
onClick = "select",
rowStyle = list(cursor = "pointer"),
minRows = 10
)
Then, I put them together and saved the result - this worked fine:
final_file = htmltools::browsable(
htmltools::tagList(map, tbl)
)
htmltools::save_html(tagList(map, tbl), file = "sample.html")
However, here is the problem:
I noticed that when I click on only one of the rows, I notice that everything else still appears on the map. Shouldn't all the other points disappear when only one item is selected?
- Could someone show me how to make this map/table look the same way it appears in the tutorial?
Thank you!
leaflet
package. See?leaflet::breweries91
clusterOption=markerClusterOptions()
. When you remove that you get your desired result as in the tutorial.leaflet
. So I don't know whether this is possible. But there is a related issue on GitHub github.com/rstudio/leaflet/issues/478 from which I would suspect that it's not possible as of the moment.