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. :)