0

UPDATE

  1. When I comment out the popup code the duplication goes away...

  2. When I add in the informaiton I want in the popup inside of the walk() function I get what I want without duplication, so I suppose it was taking the assigned Popup and running it through the loop for ever instnace on a point for every loop for however many times the record existed or something.

Below I have the following code which I adapted from the leaflet site of R-Studio, the only thing that is different is my data.

For example, I have a service line of Bariatrics, which should only map four (4) markers on the map, yet instead I get 657 markers on the map.

Is there anything screaming out as way off?

# Popup Code
Popup <- paste(
    "Hospitalist/Private:"
    , joined_data$Hospitalist_Private
    , "Address: "
    , joined_data$FullAddress
    , "Service Line: "
    , joined_data$LIHN_Line
    , "Readmit: "
    , joined_data$Readmit_Count
    , "SOI: "
    , joined_data$SOI
    , "Encounter: "
    , joined_data$PtNo_Num
    , "Payer Group: "
    , joined_data$Payor_Category
  )

# Makes 19 data.frames
LIHNCluster.df <- split(joined_data, joined_data$LIHN_Line) 

# Create the initial map object
ClusterMapLIHN <- leaflet() %>%
  setView(lng = sv_lng, lat = sv_lat, zoom = sv_zoom) %>%
  addTiles() %>%
  addTiles(group = "OSM (default)") %>%
  addProviderTiles("CartoDB.Positron") %>%
  addControl(
    paste("LIHN Service Line Cluster Map for:", MaxRptMonth, MaxRptYr)
    , position = "topright"
    )

# Use purrr::walk to loop through the LIHNCluster.df and add
# markers to the map
names(LIHNCluster.df) %>%
  purrr::walk( function(df) {
    ClusterMapLIHN <<- ClusterMapLIHN %>%
      addMarkers(
        data = LIHNCluster.df[[df]]
        , lng = ~LON
        , lat = ~LAT
        , label = ~as.character(LIHN_Line)
        #, popup = ~as.character(Popup)
        # When I do the following I get the info I want without duplication
        , popup = ~as.character(
          paste(
            sep = "<br/>"
            , "<strong>Encounter: </strong>"
            , PtNo_Num
            , "<strong>Service Line: </strong>"
            , LIHN_Line
            , "<strong>Hospitalist/Private: </strong>"
            , Hospitalist_Private
            , "<strong>Payor Category </strong>"
            , Payor_Category
            , "<strong>SOI: </strong>"
            , SOI
            , "<strong>Readmit: </strong>"
            , Readmit_Count
            , "<strong>Address: </strong>"
            , FullAddress
            )
          )
        , group = df
        , clusterOptions = markerClusterOptions(
          removeOutsideVisibleBounds = F
          , labelOptions = labelOptions(
            noHide = F
            , direction = 'auto'
          )
        )
      )
  }
)

# Add baseGroups and overlayGroups to map
ClusterMapLIHN <- ClusterMapLIHN %>%
  addLayersControl(
    baseGroups = c("OSM (default)", "CartoDB.Positron")
    , overlayGroups = names(LIHNCluster.df)
    , options = layersControlOptions(
      collapsed = TRUE
      )
  )

# Add and awesome marker
ClusterMapLIHN <- ClusterMapLIHN %>%
  addAwesomeMarkers(
    lng = sv_lng
    , lat = sv_lat
    , icon = hospMarker
    , label = "BMHMC"
    , popup = HospPopup      
  )

ClusterMapLIHN
MCP_infiltrator
  • 3,961
  • 10
  • 45
  • 82
  • I don't believe your function using a superassignment operator constitutes as a side effect. – Phil Aug 14 '18 at 03:07
  • I get the same results using `<-` and `<<-` so the regual assignment produces same results – MCP_infiltrator Aug 14 '18 at 12:12
  • - it's so strange to me, every time I add in that `Popup` I get massive duplication, I take it out, duplication goes away. – MCP_infiltrator Aug 14 '18 at 12:18
  • What's the difference between `popup` and `Popup`? How many elements are in each? Can you also post the data? – acylam Aug 14 '18 at 14:46
  • `popup` is the option and `Popup` is the data. The record count for each list made by the `split()` at the top varies from 4 to I think over 100. I will make a dummy dataset. – MCP_infiltrator Aug 14 '18 at 15:06
  • I still don't understand why you're using `purrr::walk`. You're not producing a side effect. – Phil Aug 14 '18 at 17:17
  • I was looking for a framework of looping through various levels of a data.frame, I found an example on r-studio that used purrr::walk, I guess I could just use a for loop. Entirely possible purrr::walk is not approriate here, I personaly don't know if it is or not, sounds like no. – MCP_infiltrator Aug 14 '18 at 19:35

0 Answers0