3

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?

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Rhea
  • 31
  • 2
  • 2
    `map` is a function name. It's bad practice to use that name for a data-object. Also this appears to be susceptible to "recursion-confusion" since you are also using `map` as a name inside the loop. – IRTFM Jan 06 '18 at 17:59
  • I answered a similar question before. Have a look of [**this question**](https://stackoverflow.com/questions/32275213/how-do-i-connect-two-coordinates-with-a-line-using-leaflet-in-r/32281435#32281435). – jazzurro Jan 07 '18 at 02:06

0 Answers0