Given the following reproducible example and referring back to these good threads:
https://stackoverflow.com/a/48758864/3585429
https://stackoverflow.com/a/34577359/3585429
library(leaflet)
library(geosphere)
mydf <- data.frame(lat_o = c(43.3, 43.1, 45.2),
lon_o = c(10.1, 13.3, 11.2),
lat_d = c(45.4, 47.4, 46.9),
lon_d = c(12.5, 12.8, 11.5),
n=1:3,
route=LETTERS[1:3])
# origin matrix
Om <- as.matrix(mydf[,c("lon_o","lat_o")]) # pay attention to list lng before lat
# destination matrix
Dm <- as.matrix(mydf[,c("lon_d","lat_d")]) # the same here
gcIntermediate(Om, Dm,
n=100,
addStartEnd=TRUE,
sp=TRUE) %>%
leaflet() %>%
addTiles() %>%
addPolylines(label = mydf$n,
popup = mydf$route,
weight = mydf$n*10
)
I'm here asking for a more robust and safer method to "bring along" some attributes of the original data frame mydf
to the new object wpts
(a formal class SpatialLines - a list), to be directly queried inside the leaflet function addPolylines
.
As you can see I just workaround the problem by feeding into the arguments label
, popup
, and weight
the attributes from the original data frame mydf
.
It seems to work somehow but I got a feeling it's not an entirely "bug-free" solution, especially in the case of a "real and big" data frame.
Do you have any suggestions for that?