3

I have a function that creates a leaflet map from data outputs of other functions. One of my outputs is a spatialPolygonsDataFrame, which is the result of a buffer of groups of points using gBuffer, followed by gUnaryUnion to dissolve each group into a single polygon. Finally a data frame is added to it. This output is named clusterBuffer and is called in the following function (simplified for the purpose of the question).

compileLeaflet <- function(clusterBuffer){

 # Create condition to decide whether or not the object gets mapped
 iff <- function(cond,x,y) {
     if(cond) x else y
   }

 map <- leaflet()  


 map <- addProviderTiles(map, "CartoDB.Positron")


 # Plot cluster areas on map
 iff((is.null(clusterBuffer)),
    map,
    map <- addPolygons(map, data = clusterBuffer, color = "red")
    )


 map <<- map

}

My issue is this: the object will map if I run the code in the function line by line (not as a function). However, when I try to create the map object by calling the function, I get:

Error in polygonData.default(data) : Don't know how to get path data from object of class numeric

Is the fact that it gives an error when it's inside the function due to the way I've written the code? Although it's a spatialPolygonsDataFrame, could the use of gUnaryUnion be causing formatting issues?

I render many other objects in this map using similar code all in the same function, but they're either rasters, or just derived from points -- I don't have much experience with polygons in leaflet.

Any advice on trouble shooting (format checks, syntax advice, techniques to isolate the issue, etc.) are greatly appreciated!

Thank you!

gvan
  • 473
  • 4
  • 15

0 Answers0