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

addLegend and NA work but only with a workaround #485

Closed
Demetrio92 opened this issue Jan 18, 2018 · 4 comments
Closed

addLegend and NA work but only with a workaround #485

Demetrio92 opened this issue Jan 18, 2018 · 4 comments

Comments

@Demetrio92
Copy link

Demetrio92 commented Jan 18, 2018

Providing addLegend with values in the fashion values = df$variable rather than values =~variable solves an occurrence of the error:

Error in UseMethod("metaData") : 
  no applicable method for 'metaData' applied to an object of class "NULL"

which happens if variable has NA's.

I don't see why isn't this the default behaviour.

source:
https://stackoverflow.com/a/32011289/3494126

p.s. for instance, just following this official tutorial with my data I produced the abovementioned error.


Ok, @jcheng5 thanks for the explanation, now I can produce a working/non-working example:

library(leaflet)
library(sf)

N = 10
center = c(16.37, 48.21)  # Vienna approx center
random_event = list()
random_event$lat = rnorm(N, center[1], sd = 0.001)
random_event$lon = rnorm(N, center[2], sd = 0.001)
random_event = as.data.frame(random_event)
random_event = st_as_sf(random_event, coords = c('lat', 'lon'), crs = 4326)

random_event$random_intensity = rbinom(N, 5, 0.4)

pal = colorFactor('Greens', domain = random_event$random_intensity)

leaflet_plot = (

    leaflet() %>%
    addProviderTiles(leaflet::providers$CartoDB.Positron) %>%
    addCircles(
        data = random_event$geometry,
        weight = 2, opacity = 0.5, color = pal(random_event$random_intensity),
        radius = 5.5, fill = T
    )
    %>%
    addLegend('bottomright',
        pal = pal,
        # values = random_event$random_intensity,  # works fine
        values = ~random_intensity   # doesn't work
    )

)
print(leaflet_plot)

Indeed, the mistake here is using leaflet() %>% instead of leaflet(random_event) %>% which then works with both versions.


I still thing this is a minor bug. But feel free to disregard it. I will try to improve the SO issue, so that anyone who does the same in the future understands why it is not a bug. Although, with your explanation it is definitely more informative.

@jcheng5
Copy link
Member

jcheng5 commented Jan 19, 2018

Can you provide a reproducible example that's giving you that error? The tutorial examples work for me (other than the fact that the data has moved to https://github.com/datasets/geo-boundaries-world-110m/raw/master/countries.geojson, which I hadn't noticed). Thanks.

@jcheng5
Copy link
Member

jcheng5 commented Jan 19, 2018

Let me also note that this error basically means, "you used the formula syntax which implies you gave us some data--but I didn't find any data". I suspect the fact that you're seeing this with addLegend means your map doesn't have any data (i.e. you didn't pass any data to leaflet()), only a specific marker/shape layer was given data. (It looks like addLegend doesn't itself have an explicit data parameter, which is probably an oversight in the library.)

@Demetrio92
Copy link
Author

SOLUTION: don't use

leaflet() %>% otherStuff() 

always provide it with the data (as intended), e.g.

leaflet(my_data) %>% otherStuff() 

@fabiofernandez
Copy link

had that problem when using fillColor, and the way I solved it was using pal(...) instead of ~pal(...), not sure why though

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