0

I'm doing a Shiny App in RStudio. Now, I'm creating a map using Leaflet package. The code works properly, and shows my modeling when I ask, and all is almost ok but for one point: When I try to do another modeling, I click the button to show my new data in the leaflet map, but it only can show the last one modeled. If I close the App, then it refreshes the information, and shows me the new map.

The maps are divided in possibilities can happen, if chlorine is more/less than 1. That is written in a loop if/else. The App is capable to detect what Leaflet map has to print, but doesn't show it after the simulation. I have to restart the App (or reload it througout the button in R), and I would like a related response of the button: If I put new characteristics and try a new simulation, the leaflet map would have to show the related results.

The piece of code is:

ui <- fluidpage(
        tabItem(tabName = "resultats",
                wellPanel(  
                       actionButton("refresh", label = "Mostra Zona Simulada"),
                       leafletOutput("mymap", width = "500px", height = "250px")
                       )
                    )
                 )
server <- function(input, output, session) {
   observeEvent(input$refresh, {
    if(empty(Nodesover)){
      output$mymap <- renderLeaflet({
        leaflet(DadesNodesDibuix) %>%
          addTiles() %>%
          addCircleMarkers(
            lng = Nodesguai$ycoord, 
            lat = Nodesguai$xcoord, 
            color = 'green', 
            radius = 1,
            opacity = 100,
            popup = paste('<strong>', "ID node:", '</strong>', Nodesguai$idnode, "<br>", 
                          '<strong>', "Concentracio Clor:", '</strong>', round(Nodesguai$clor,3), "<br>")) %>% 
          addCircleMarkers(
            lng = Nodeslow$ycoord, 
            lat = Nodeslow$xcoord, 
            color = 'red', 
            radius = 1,
            opacity = 100,
            popup = paste('<strong>', "Informacio node:", '</strong>', Nodeslow$idnode, "<br>",
                          '<strong>', "Concentracio Clor:", '</strong>', round(Nodeslow$clor,3), "<br>")) %>%
          addLegend(
            position = c("bottomleft"), 
            colors = c("green","red"), 
            title = "Concentracio Clor",
            labels = c("Correcte", "Baix"))
      })
    }else{
      output$mymap <- renderLeaflet({
        leaflet() %>%
          addTiles() %>%
          addCircleMarkers(
            lng = Nodesguai$ycoord, 
            lat = Nodesguai$xcoord, 
            color = 'green', 
            radius = 1,
            opacity = 100,
            popup = paste('<strong>', "Informacio node:", '</strong>', Nodesguai$idnode, "<br>",
                          '<strong>', "Concentracio Clor:", '</strong>', round(Nodesguai$clor,3), "<br>")) %>% 
          addCircleMarkers(
            lng = Nodeslow$ycoord, 
            lat = Nodeslow$xcoord, 
            color = 'red', 
            radius = 1,
            opacity = 100,
            popup = Nodeslow$clor) %>%
          addCircleMarkers(
            lng = Nodesover$ycoord, 
            lat = Nodesover$xcoord, 
            color = 'black', 
            radius = 1,
            opacity = 100,
            popup = paste('<strong>', "Informacio node:", '</strong>', Nodesover$idnode, "<br>",
                          '<strong>', "Concentracio Clor:", '</strong>', round(Nodesover$clor,3), "<br>")) %>%
          addLegend(
            position = c("bottomleft"), 
            colors = c("green","red","black"), 
            title = "Concentracio Clor",
            labels = c("Correcte", "Baix","Sense"))
          })
            if(empty(Nodeslow)){
              output$mymap <- renderLeaflet({
                leaflet() %>%
                  addTiles() %>%
                  addCircleMarkers(
                    lng = Nodesguai$ycoord,
                    lat = Nodesguai$xcoord,
                    color = 'green',
                    radius = 1,
                    opacity = 100,
                    popup = paste('<strong>', "Informacio node:", '</strong>', Nodesguai$idnode, "<br>", 
                                  '<strong>', "Concentracio Clor:", '</strong>', round(Nodesguai$clor,3), "<br>")) %>%
                  addCircleMarkers(
                    lng = Nodesover$ycoord,
                    lat = Nodesover$xcoord,
                    color = 'black',
                    radius = 1,
                    opacity = 100,
                    popup = paste('<strong>', "Informacio node:", '</strong>', Nodesover$idnode, "<br>",
                                  '<strong>', "Concentracio Clor:", '</strong>', round(Nodesover$clor,3), "<br>")) %>%
                addLegend(
                    position = c("bottomleft"), 
                    colors = c("green","black"), 
                    title = "Concentracio Clor",
                    labels = c("Correcte", "Sense"))
                })
              }else{
                output$mymap <- renderLeaflet({
                  leaflet() %>%
                    addTiles() %>%
                    addCircleMarkers(
                      lng = Nodesguai$ycoord,
                      lat = Nodesguai$xcoord,
                      color = 'green',
                      radius = 1,
                      opacity = 100,
                      popup = paste('<strong>', "Informacio node:", '</strong>', Nodesguai$idnode, "<br>", 
                                    '<strong>', "Concentracio Clor:", '</strong>', round(Nodesguai$clor,3), "<br>")) %>%
                    addLegend(
                      position = c("bottomleft"), 
                      colors = c("green"), 
                      title = "Concentracio Clor",
                      labels = c("Correcte"))
                  })
                if(empty(Nodesguai)){
                  output$mymap <- renderLeaflet({
                    leaflet() %>%
                      addTiles() %>%
                      addCircleMarkers(
                        lng = Nodeslow$ycoord,
                        lat = Nodeslow$xcoord,
                        color = 'red',
                        radius = 1,
                        opacity = 100,
                        popup = paste('<strong>', "Informacio node:", '</strong>', Nodeslow$idnode, "<br>", 
                                      '<strong>', "Concentracio Clor:", '</strong>', round(Nodeslow$clor,3), "<br>")) %>%
                      addCircleMarkers(
                        lng = Nodesover$ycoord,
                        lat = Nodesover$xcoord,
                        color = 'black',
                        radius = 1,
                        opacity = 100,
                        popup = paste('<strong>', "Informacio node:", '</strong>', Nodesover$idnode, "<br>", 
                                      "<em>", "Concentracio Clor:", '</strong>', round(Nodesover$clor,3), "<br>")) %>%
                      addLegend(
                        position = c("bottomleft"), 
                        colors = c("red","black"), 
                        title = "Concentracio Clor",
                        labels = c("Baix", "Sense"))
                  })
                }
              }
         }
   }) 
}    

What I'm doing wrong? The code works instead of that.

Thank you very much for your time and your patience. :)

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 1
    I believe you have to use `leafletProxy()` to update the map. https://rdrr.io/cran/leaflet/man/leafletProxy.html & https://stackoverflow.com/questions/47465896/having-trouble-with-leafletproxy-observeevent-and-shiny-leaflet-application – Tung May 27 '21 at 16:27

1 Answers1

0

I found my answer a few minutes ago, and I hope It will be able to help someone else with this solution.

The problem I had was the Leaflet order could not read the new results of every simulation I did.

The solution has been ADD the input files INSIDE the "observeEvent(input$refresh, { output$mymap <- renderLeaflet({...", as I explain below:

 observeEvent(input$Refresh, {
        output$mymap <- renderLeaflet({
        #You have to add the files inside the output. This files are changing in
        #every simulation I did in my shiny app, and now the loop CAN READ the 
        #changes!
          DadesNodes<-read.csv("./data/DadesNodes.csv",header = TRUE)
          DadesNodesLow<-read.csv("./results/Nodeslow.csv",header = TRUE)
          DadesNodesOver<-read.csv("./results/Nodesover.csv",header = TRUE)
          DadesNodesGuai<-read.csv("./results/Nodesguai.csv",header = TRUE)
          DadesNodesTreball<-read.csv("./data/DadesNodesTreball.csv",header = TRUE)
          Nodeslow  <- DadesNodesLow
          Nodesover <- DadesNodesOver
          Nodesguai <- DadesNodesGuai
        if(empty(Nodesover)){
            leaflet("mymap") %>%
              addTiles() %>%
              addCircleMarkers(
                lng = Nodesguai$ycoord, 
                lat = Nodesguai$xcoord, 
                color = 'green', 
                radius = 1,
                opacity = 100,
                popup = paste('<strong>', "ID node:", '</strong>', Nodesguai$idnode, "<br>", 
                              '<strong>', "Concentracio Clor:", '</strong>', round(Nodesguai$clor,3), "<br>")) %>% 
              addCircleMarkers(
                lng = Nodeslow$ycoord, 
                lat = Nodeslow$xcoord,  
                color = 'red', 
                radius = 1,
                opacity = 100,
                popup = paste('<strong>', "Informacio node:", '</strong>', Nodeslow$idnode, "<br>",
                              '<strong>', "Concentracio Clor:", '</strong>', round(Nodeslow$clor,3), "<br>")) %>%
              addLegend(
                position = c("bottomleft"), 
                colors = c("green","red","black"), 
                title = "Concentracio Clor (mg/L)",
                opacity = 100,
                labels = c("Correcte", "Baix","Alt"))
          #})
        }else{
          if(empty(Nodeslow)){
            leaflet("mymap") %>%
                  addTiles() %>%
                  addCircleMarkers(
                    lng = Nodesguai$ycoord, 
                    lat = Nodesguai$xcoord,
                    color = 'green',
                    radius = 1,
                    opacity = 100,
                    popup = paste('<strong>', "Informacio node:", '</strong>', Nodesguai$idnode, "<br>",
                                  '<strong>', "Concentracio Clor:", '</strong>', round(Nodesguai$clor,3), "<br>")) %>%
                  addCircleMarkers(
                    lng = Nodesover$ycoord, 
                    lat = Nodesover$xcoord, 
                    color = 'black',
                    radius = 1,
                    opacity = 100,
                    popup = paste('<strong>', "Informacio node:", '</strong>', Nodesover$idnode, "<br>",
                                  '<strong>', "Concentracio Clor:", '</strong>', round(Nodesover$clor,3), "<br>")) %>%
                  addLegend(
                    position = c("bottomleft"),
                    colors = c("green","red","black"),
                    title = "Concentracio Clor (mg/L)",
                    opacity = 100,
                    labels = c("Correcte", "Baix","Alt"))
            }else{
              if(empty(Nodesguai)){
                leaflet("mymap") %>%
                    addTiles() %>%
                    addCircleMarkers(
                      lng = Nodeslow$ycoord, 
                      lat = Nodeslow$xcoord,
                      color = 'red',
                      radius = 1,
                      opacity = 100,
                      popup = paste('<strong>', "Informacio node:", '</strong>', Nodeslow$idnode, "<br>",
                                    '<strong>', "Concentracio Clor:", '</strong>', round(Nodeslow$clor,3), "<br>")) %>%
                    addCircleMarkers(
                      lng = Nodesover$ycoord, 
                      lat = Nodesover$xcoord,
                      color = 'black',
                      radius = 1,
                      opacity = 100,
                      popup = paste('<strong>', "Informacio node:", '</strong>', Nodesover$idnode, "<br>",
                                    "<em>", "Concentracio Clor:", '</strong>', round(Nodesover$clor,3), "<br>")) %>%
                    addLegend(
                      position = c("bottomleft"),
                      colors = c("green","red","black"),
                      title = "Concentracio Clor (mg/L)",
                      opacity = 100,
                      labels = c("Correcte", "Baix","Alt"))
              }else{
                leaflet("mymap") %>%
                  addTiles() %>%
                  addCircleMarkers(
                    lng = Nodesguai$ycoord,
                    lat = Nodesguai$xcoord,
                    color = 'green',
                    radius = 1,
                    opacity = 100,
                    popup = paste('<strong>', "Informacio node:", '</strong>', Nodesguai$idnode, "<br>",
                                  '<strong>', "Concentracio Clor:", '</strong>', round(Nodesguai$clor,3), "<br>")) %>%
            addCircleMarkers(
              lng = Nodeslow$ycoord, 
              lat = Nodeslow$xcoord, 
              color = 'red',
              radius = 1,
              opacity = 100,
              popup = Nodeslow$clor) %>%
            addCircleMarkers(
              lng = Nodesover$ycoord, 
              lat = Nodesover$xcoord, 
              color = 'black',
              radius = 1,
              opacity = 100,
              popup = paste('<strong>', "Informacio node:", '</strong>', Nodesover$idnode, "<br>",
                            '<strong>', "Concentracio Clor:", '</strong>', round(Nodesover$clor,3), "<br>")) %>%
            addLegend(
              position = c("bottomleft"),
              colors = c("green","red","black"),
              title = "Concentracio Clor (mg/L)",
              opacity = 100,
              labels = c("Correcte", "Baix","Alt"))

            }
          }
        }     
      })
    })
    

Cheeers!