.RData load not reflected upon leafletproxy call

Hi everyone,

I've been struggling to get my leaflet marker popups to reflect newly loaded values in my Shiny application. One of several .RData files is loaded based upon app initialisation. But a different .RData file can be loaded based on input from the user.
I've used the print function to confirm that the new values are in fact loaded, however when I call leafletproxy, the first preloaded values are still displayed.
What am I doing wrong?
I'm going crazy trying to figure this out...
Thanks in advance!

The two data files can be found here: https://drive.google.com/file/d/1sNeaBhsqJkfRbV22BDiQvsurgS1GBpDs/view?usp=sharing
https://drive.google.com/file/d/1SHWbjtMvJmUZMvLwG19UpA1LmHXEHL6L/view?usp=sharing
Here is a simplified example code:

library(shiny)
library(leaflet) 


# preload lastest .RData file containing several SpatialPointsDataFrames tn.twd tn.gro ...
today    <- lubridate::today()
preloadfile <- paste0(today,".RData")
load(preloadfile) 

group1   <- " gro"
group2   <- "twd"
choices <- c(group1, group2)

ui <- fluidPage(
  titlePanel("Example"),
  wellPanel(
    uiOutput("date1_UI"),
    uiOutput("select1_UI"),
    leafletOutput("map1", height = "560px", width = "100%"),
  )
)



server <- function(input, output, session) {
  
  # ui ####
  output$date1_UI <- renderUI(
    dateInput(
      "date1",
      label =   "Dates:",
      value     = today,
      min = "2020-02-06",
      max = "2020-02-07",
      startview = "days",
      width = "50%"
    )
  )
  
  
  output$select1_UI <- renderUI({
    selectizeInput("select1", "Select data:", 
                   choices  = choices, 
                   multiple = T, 
                   selected = NULL,
                   width    = "50%",
                   options  = list(maxItems = 4,
                                   placeholder = "Select var"),
    )
  })
  
  # intialise leaflet map1 ####
  output$map1 <- renderLeaflet({
    leaflet(options = leafletOptions(zoomControl = F)) %>%
      htmlwidgets::onRender("function(el, x) {L.control.zoom({ position: 'topright' }).addTo(this)}"
      ) %>% 
      setView(lng = 8.2275 , #8.2275 centre CH
              lat = 46.8182,
              zoom = 8) %>%
      addTiles(
        urlTemplate = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}',
        attribution = 'Example',
        options = providerTileOptions(noWrap = T),
        group = "background"
      )
  })
  
  # load data when new date1 selected ####
  observeEvent(input$date1, {
    req(input$date1)
    newfile <- paste0(input$date1,".RData")
    load(newfile)
    print(paste0(newfile, " loaded"))
    # print(tn$twdmax[1:10]) # can see that data is loaded
  })
  
  # refresh layers map1 #####
  observeEvent(c(input$select1, input$date1), {
    # req(input$select1)
    map1_proxy <- leafletProxy("map1", session) %>% 
      clearMarkers() %>% 
      clearPopups()
    
    if (any(input$select1 == group2)) {
      # tn.twd ####
      map1_proxy <- map1_proxy %>%
        addMarkers(
          data = tn,
          layerId = paste0("twd-", 1:length(tn)),
          clusterId = "twd-cl",
          group = group2,
          popup = paste(
            group2, ": ", tn$twdmax, " <br>")
        )
    }
    # tn.gro ####
    if (any(input$select1 == group1)) {
      map1_proxy <- map1_proxy %>%
        addMarkers(
          data = tn,
          layerId = paste0("gro-", 1:length(tn)),
          clusterId = "gro-cl",
          group = group1,
          popup = paste(
            "Tree growth: ", tn$grosum, " <br>")
        )
    }
  }
)
  
}

shinyApp(ui, server) 
1 Like

Welcome to reactive hell! Debugging situations like this is not easy, and not always worth the effort of doing "right".

My preferred pattern in situations like this - I am uncertain how applicable it would be in your base scenario; it would handle your simplified reprex easily - is to have my leaflet as a separate reactive object, updated on each change in condition such as dates or other variables. This object is then input for renderLeaflet.

Redrawing the leaflet from scratch may not be computationally optimal, but it is definitely easier to implement than modyfing an object in place.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.