leaflet icon indicating copy to clipboard operation
leaflet copied to clipboard

Specifying labels as zero-length character vector causes error

Open bmaitner opened this issue 11 months ago • 0 comments

I've come across an error that occurs with empty datasets. As per #452 , leaflet can handle empty data.frames. However, the labels seem to still have an error when encountering an empty character vector. I'll include an example modified from https://github.com/rstudio/leaflet/issues/452#issue-249888246 below.

I would expect that when passing an empty data.frame and empty label vector, it would be treated in the same way as passing an empty data.frame by itself, (i.e., leaflet would fail to show any points). Obviously there are easy workarounds, but it might be nice to handle internally.


library("tidyverse")
library("leaflet")
library("sf")

my_points <- tibble(
  lat = c(10,20,30),
  lon = c(20,40,50),
  labels = c("a", "b", "c")
)

my_sf <- my_points %>%
  st_as_sf(coords = c("lon", "lat"))

# If I filter to a single value, everything works as expected

my_sf %>%
  filter(labels == "c") %>%
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers(label = my_sf %>%
                     filter(labels == "c")%>%
                     pull(labels))



# If I filter to an empty table, things also work as expected

my_sf %>%
  filter(labels == "d") %>%
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers()


# If I filter to an empty table, but include labels, I get an error

my_sf %>%
  filter(labels == "d") %>%
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers(label = my_sf %>%
                     filter(labels == "d")%>%
                     pull(labels))
#> Error in sum(sapply(label, function(x) {: invalid 'type' (list) of argument

Created on 2024-03-11 with [reprex v2.0.2](https://reprex.tidyverse.org/)

bmaitner avatar Mar 11 '24 22:03 bmaitner