My objective is to draw lines in Leaflet with data from a csv file.
Example data:
point lat long SiteName group colour endlat endlong id
A 52.169868 4.66844 Kruisweg-Vriezekoop L1 green 52.22576 4.676024 1
For now, I have a working example, by drawing one single line:
map <- leaflet(data = myDF) %>%
addTiles() %>%
addPolylines(data = myDF[1,], lat = ~c(lat, endlat), lng = ~c(long,
endlong), color = "red") %>%
addPopups(data = myDF, lat = ~lat, lng = ~long, popup = ~SiteName)
map
My intention is to loop through the number of rows by using the following loop:
map <- leaflet(data = myDF) %>%
addTiles() %>%
for (i in nrow(myDF$id)) {
map <- addPolylines(map, data = myDF[i,], lat = ~c(lat, endlat), lng =
~c(long, endlong), color = "red")
}
addPopups(data = myDF, lat = ~lat, lng = ~long, popup = ~SiteName)
map
I tried adding this loop inside the Leaflet function, but I keep getting this error:
Error in inherits(map, "leaflet") : argument "map" is missing, with no default
And after that, I get a null result.
How can I use a loop within my Leaflet function? Or is there a more efficient way?