Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying labels as zero-length character vector causes error #906

Open
bmaitner opened this issue Mar 11, 2024 · 0 comments
Open

Specifying labels as zero-length character vector causes error #906

bmaitner opened this issue Mar 11, 2024 · 0 comments

Comments

@bmaitner
Copy link

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 #452 (comment) 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/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant