5

For some reason, the popups assigned to the polygons on the Shiny leaflet app are pulling up the wrong information that should be assigned to the polygon. It seems that the Polygons have been placed on the map, then assigned the polygon attributes in a different order.

Any ideas would be greatly appreciated.

server<-function(input,output,session)({

  output$map<-renderLeaflet({
    leaflet() %>%
        addTiles() %>%
        addPolygons(data = spatial_merge, 
          layerId = spatial_merge@data$PolygonNo, 
          stroke= TRUE, fillOpacity = 0.5, smoothFactor = 0.5,     weight=1,
          color = "grey", opacity = 1, popup=paste(sep= "<br/>",
               "<b>ForestName: </b>", spatial_merge$ForestName,
               "<b>Project Name: </b>", spatial_merge$Project_Name,
               "<b>Treatment Year: </b>", spatial_merge$Project_Year,
               "<b>Treatment Type: </b>", spatial_merge$Treatment_Type,
               "<b>Area Treated: </b>", spatial_merge$Polygon_Area,
               "<b>Polygon Cost: </b>", spatial_merge$TotalCost,
               "<b>Contractor Name: </b>", spatial_merge$Project_Contractor)
  })
})    

shinyApp(ui,server)

> class(spatial_merge)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"

> names(spatial_merge)
 [1] "Primary_Key"        "Project_Name"       "PolygonNo"          "WMA"                    "ContractName"       "Project_Year"      
 [7] "Treatment_Type"     "Project_Contractor" "Polygon_Area"           "TotalCost"          "OBJECTID_12"        "FA_NAME"           
[13] "Comments"           "Source"             "DataCaptureDate"        "DataCapturedBy"     "Block_ID"           "SubBlock_ID"       
[19] "SHAPE_Length"       "SHAPE_Area"   

> dim(spatial_merge)
[1] 647  20

Thanks for looking at my question! K

AlphaKevy
  • 187
  • 2
  • 14
  • It's hard to know for sure without seeing the structure of your data, but I'm not sure if you need the `$` in the popup code. Can you try without it? – Jim Leach Sep 21 '16 at 16:14
  • @Jim Leach I updated with the type of data. The $ didn't manage to solve the problem unfortunately – AlphaKevy Sep 21 '16 at 16:22
  • Try creating the popup first as a separate object: `popup <- paste(sep = ..., )` and then using that object directly in the `addPolygons()` call. I'm afraid I've only ever used leaflet with `data.frame` data, so I'm not sure how it behaves with sp data. – Jim Leach Sep 21 '16 at 16:25
  • haha man, I also tried that method. It seems that the sp data is acting a little differently... – AlphaKevy Sep 21 '16 at 16:45
  • Your code inside the `renderLeaflet` call seems fine. Obviously I don't have your data, but with a data set from mapview the feature IDs of the polygons are fine. See this gist: https://gist.github.com/tim-salabim/9d132266609f49ae13688595eab04d21 – TimSalabim Sep 21 '16 at 17:12
  • Are you able to share the data, or something that looks like it? – Jim Leach Sep 23 '16 at 12:26
  • 1
    @JimLeach I'll try to put something together. It's quite odd though, TimSalabim was totally right that the code works using the mapview data...but mine doesn't. It's almost like the polygons are being shuffled and then its' associated attribute table is being shuffled in a different way and then being put on the map. I'll try to get you some data. Do you have an email? – AlphaKevy Sep 23 '16 at 17:53
  • You can reach me on jim at thedatagent dot com – Jim Leach Sep 27 '16 at 10:11
  • Was this problem actually solved? I would like to see the resolution. – Konrad Dec 21 '16 at 22:02
  • @Konrad Unfortunately no, I spent a decent amount of time trying to figure it out, but then had to move onto other things at work :(. I'm still super interested in figuring it out though. – AlphaKevy Dec 22 '16 at 23:10
  • @AlphaKevy OK, I will have a look. It may be useful to update your post with the details on how the `spatial_merge` is created. For instance, I presume that the advisable way to merge the data into a `sp` object would involve making use of the `sp::merge`. – Konrad Dec 23 '16 at 08:10
  • Has this ever been resolved? I'm facing the same issue when using SF library then leaflet. Mapview shows the correct polygon labels, but leaflet does not. https://stackoverflow.com/questions/74976789/r-leaflet-incorrect-polygon-label-after-st-intersection – T2029 Jan 24 '23 at 18:42

0 Answers0