8000 Leaflet returns an error for empty "sf" "dataframe" objects · Issue #452 · rstudio/leaflet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Leaflet returns an error for empty "sf" "dataframe" objects #452

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

Closed
charliejhadley opened this issue Aug 13, 2017 · 4 comments
Closed

Leaflet returns an error for empty "sf" "dataframe" objects #452

charliejhadley opened this issue Aug 13, 2017 · 4 comments

Comments

@charliejhadley
Copy link

Leaflet handles empty tibbles happily:

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

my_points <- tibble(
  lat = c(10,20,30),
  lon = c(20,40,50),
  labels = c("a", "b", "c")
)
## This generates a map
my_points %>%
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers()

## So does this
my_points %>%
  filter(lat < 5) %>%
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers()

However, an sf object containing zero observations (0 rows) returns an error without a leaflet object:

library("sf")

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

my_sf %>%
  filter(labels == "d") %>%
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers()
## Error: is.matrix(x) || inherits(x, "XY") is not TRUE
@tim-salabim
Copy link
Contributor

Are you sure this error is generated by leaflet? I think this is an error message from sf.

@charliejhadley
Copy link
Author
charliejhadley commented Aug 14, 2017

Could well be. There's an issue here from sf for handling empty data frames for plot - r-spatial/sf#436 but the sf library explicitly provides support for base R plotting, whereas you folks explicitly provide support in leaflet.

The package creator @edzer was responsible for fixing issue 436, I'll link these two issues together.

@edzer
Copy link
edzer commented Aug 25, 2017

The error message seems to come from leaflet:::sf_coords; it assumes the outcome is a matrix, but rbind-ing the elements of an empty list doesn't give you that.

black-tea added a commit to black-tea/VZDash-DOT that referenced this issue Aug 30, 2017
… overlay

There seems to be an issue when the sf package is plotting on a leaflet
map a spatial
dataframe where there are no points. Every time I performed a subset of
the LAPD data
where there were no points in the subsetted region, I would get the
message:
"Error: is.matrix(x) || inherits(x, "XY") is not TRUE." After research,
it seems to be
related to this rstudio/leaflet#452 which is
still not resolved
as of 8/29.

To get around this, I created an observe() event paired with an if/else
to check
whether the length of the subset was > 0. If so, it will add Markers to
the plot, and
if not, won't do anything. I set the observeEvent to changes in the
input geography name.
@jcheng5 jcheng5 closed this as completed in 50ea199 Sep 1, 2017
jcheng5 added a commit that referenced this issue Sep 1, 2017
Fix #452: Leaflet returns an error for empty "sf" "dataframe" objects
@jcheng5
Copy link
Member
jcheng5 commented Sep 1, 2017

Thanks for the input, everyone. This should be all set, AFAICT.

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

4 participants
0