title: Error in polygonData/default(data) : Dont know how to get path data from object of class numeric
body: <p>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).</p>
  
  <pre><code>compileLeaflet &lt;- function(clusterBuffer){
  
   # Create condition to decide whether or not the object gets mapped
   iff &lt;- function(cond,x,y) {
       if(cond) x else y
     }
  
   map &lt;- leaflet()  
  
  
   map &lt;- addProviderTiles(map, "CartoDB.Positron")
  
  
   # Plot cluster areas on map
   iff((is.null(clusterBuffer)),
      map,
      map &lt;- addPolygons(map, data = clusterBuffer, color = "red")
      )
  
  
   map &lt;&lt;- map
  
  }
  </code></pre>
  
  <p>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 <code>map</code> object by calling the function, I get:</p>
  
  <p><code>Error in polygonData.default(data) :  Don't know how to get path data from object of class numeric</code> </p>
  
  <p>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 <code>gUnaryUnion</code> be causing formatting issues? </p>
  
  <p>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. </p>
  
  <p>Any advice on trouble shooting (format checks, syntax advice, techniques to isolate the issue, etc.) are greatly appreciated! </p>
  
  <p>Thank you! </p>