I have a shiny app with lots of points that need to be selected to display data in a table. I love the functionality displayed in this question here. Unfortunately implementing this in shiny produces the error message Ignoring prepended content; prependContent can't be used in a Shiny render call
. My shiny app is rather large and requires internal data so I will post the example from the above linked question. If someone can get this working in shiny I can adapt it to my app.
library(leaflet)
library(crosstalk)
library(DT)
library(dplyr)
library(htmltools)
library(summarywidget)
data_2 <- data.frame(ID=c(1:8),
Name1 = c("A", "A", "A", "C", "B", "B", "A", "B"),
Name2 = c("a", "b", "b", "a", "b", "a", "b", "c"),
Value1 = c(12,43,54,34,23,77,44,22),
Value2 = c(6,5,2,7,5,6,4,3),
Lat = c(51.1, 51.6, 57.3, 52.4, 56.3, 54.3, 60.4, 49.2),
Lon = c(5, -3, -2, -1, 4, 3, -5, 0))
data_2<-data_2 %>%
mutate(
lab_DB = case_when(
Name1 == unique(data_2$Name1)[1] ~ "blue",
Name1 == unique(data_2$Name1)[2] ~ "green",
Name1 == unique(data_2$Name1)[3] ~ "red"
)
)
sdf <- SharedData$new(data_2, key=~ID, group="SharedDataqwertyui")
lmap <- leaflet() %>%
addTiles() %>%
addMarkers(data=sdf, group="test", layerId = ~ID) %>%
htmlwidgets::prependContent(tags$script(src="https://unpkg.com/[email protected]/dist/leaflet-lasso.umd.min.js")) %>%
htmlwidgets::onRender("
function(el, x) {
var sheet = window.document.styleSheets[0];
sheet.insertRule('.selectedMarker { filter: hue-rotate(135deg); }', sheet.cssRules.length);
var map = this;
const lassoControl = L.control.lasso(options={'position':'topleft'}).addTo(map);
function resetSelectedState() {
map.eachLayer(layer => {
if (layer instanceof L.Marker) {
layer.setIcon(new L.Icon.Default());
} else if (layer instanceof L.Path) {
layer.setStyle({ color: '#3388ff' });
}
});
}
function setSelectedLayers(layers) {
resetSelectedState();
let ids = [];
layers.forEach(layer => {
if (layer instanceof L.Marker) {
layer.setIcon(new L.Icon.Default({ className: 'selected selectedMarker'}));
} else if (layer instanceof L.Path) {
layer.setStyle({ color: '#ff4620' });
}
ids.push(layer.options.layerId);
});
ct_filter.set(ids);
}
var ct_filter = new crosstalk.FilterHandle('SharedDataqwertyui');
ct_filter.setGroup('SharedDataqwertyui');
var ct_sel = new crosstalk.SelectionHandle('SharedDataqwertyui');
ct_sel.setGroup('SharedDataqwertyui');
map.on('mousedown', () => {
ct_filter.clear();
ct_sel.clear();
resetSelectedState();
});
map.on('lasso.finished', event => {
setSelectedLayers(event.layers);
});
lassoControl.setOptions({ intersect: true});
var clearSel = function(){
ct_filter.clear();
ct_sel.clear();
resetSelectedState();
}
document.getElementById('clearbutton').onclick = clearSel;
}") %>%
addEasyButton(
easyButton(
icon = "fa-ban",
title = "Clear Selection",
id="clearbutton",
onClick = JS("function(btn, map){
return
}")
)
)
dtable <- datatable(sdf , width = "100%",editable=TRUE, caption=tags$caption("Mean of Value1: ",summarywidget(sdf, statistic='mean', column='Value1')))
bscols( widths=c(6,6,0), lmap, dtable, htmltools::p(summarywidget(sdf, statistic='mean', column='Value1'), style="display:none;"))