leaflet not rendering in flexidashboard

Dear Community,

I have a leaflet map that will render fine from a script file but not from R markdown.

The code is:
data_map %>%
leaflet() %>%
addPolygons(weight = 1,
label = ~hr,
color = ~map_palette(hr)) %>%
addMarkers(data = shared_data,
label = ~paste(pn,
"Number :", dn)) %>%
addLegend(pal = map_palette, values = ~hr, group = "circles", position = "bottomleft", title = "HD")

the error message reads:

Error in validateCoords(lng, lat, funcName) :
addMarkers requires numeric longitude/latitude values
Calls: ... -> addMarkers -> derivePoints -> validateCoords
Execution halted

I have tried to find a solution including updating RStudio but drawn a blank
Does anyone out there have an answer?

Hard to tell without access to your data - but my hunch is that your markdown document is missing a library declaration. Are you loading {sf} or whatever package that provides support for the data format of your shared_data object?

Many thanks for taking the time to reply.

I had overlooked a line of code that was conflicting

1 Like

You're not defining the lat and long in addMarkers(). That might be the issue.

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

It is not strictly necessary to define lat / long - provided that the data object is of a recognized spatial format (one of the {sf} / {sp} realms).

Which is where I got my hunch about a package not attached (leading to the spatial format not being recognized).

2 Likes

To create an unrelated plot I had changed numeric values to factors and overlooked changing it back for the SharedData set required for Crosstalk and for the map to render. There was no need to define lat/long.... just a need to stop staring at the code and sleep!!! As soon as I read it again this morning I saw the error. Just want to add that flexdashboard and crosstalk are fantastic packages for those like me who were taught latin at school instead of java script.
Many thanks

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.