From 8d3237d3a6abd413cef5623e5cfa6095cf540c0b Mon Sep 17 00:00:00 2001 From: Jack Davison Date: Fri, 19 Jul 2024 21:27:39 +0100 Subject: [PATCH] fix: marker ordering problems w/ `merge()` --- R/magicIcons.R | 18 ++++++++++++++---- vignettes/leaf-magic.Rmd | 8 +++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/R/magicIcons.R b/R/magicIcons.R index 52eedaa..983d683 100644 --- a/R/magicIcons.R +++ b/R/magicIcons.R @@ -66,7 +66,11 @@ magicIcons <- function(icon = "circle", markerSize = markerSize ) - unique_combos <- unique(combinations) + combinations$rn <- seq_along(combinations$icon) + + unique_combos <- combinations + unique_combos$rn <- NULL + unique_combos <- unique(unique_combos) make_fa_icon <- function(icon, markerColor, iconColor, markerSize) { time <- Sys.time() %>% as.numeric() @@ -135,9 +139,15 @@ magicIcons <- function(icon = "circle", unique_combos$themarker <- icons combinations <- - merge(combinations, - unique_combos, - by = c("icon", "markerColor", "iconColor", "markerSize")) + merge( + combinations, + unique_combos, + sort = TRUE, + by = c("icon", "markerColor", "iconColor", "markerSize") + ) + + combinations <- combinations[order(combinations$rn),] + rownames(combinations) <- NULL do.call(leaflet::iconList, combinations$themarker) } diff --git a/vignettes/leaf-magic.Rmd b/vignettes/leaf-magic.Rmd index 7689a16..af206f0 100644 --- a/vignettes/leaf-magic.Rmd +++ b/vignettes/leaf-magic.Rmd @@ -258,14 +258,16 @@ leaflet(port_talbot) %>% ```{r awesomepal} cols <- names(awesomePalette)[names(awesomePalette) != "white"] -breweries91$color <- sample(cols, nrow(breweries91), replace = TRUE) +breweries91$facolor <- sample(cols, nrow(breweries91), replace = TRUE) + +breweries91$hexcolor <- unlist(use.names = FALSE, awesomePalette[breweries91$facolor]) leaflet(breweries91) %>% addTiles() %>% addAwesomeMarkers( icon = ~ awesomeIcons( icon = "circle", - markerColor = color, + markerColor = facolor, iconColor = "#FFFFFF", library = "fa" ), @@ -274,7 +276,7 @@ leaflet(breweries91) %>% addMarkers( icon = ~ magicIcons( icon = "fas fa-circle", - markerColor = unlist(use.names = FALSE, awesomePalette[color]), + markerColor = hexcolor, iconColor = "#FFFFFF" ), group = "Magic"