Delete map after pressing reset button on shiny

I am not able to delete the generated map after I press the reset button on shiny, could you help me to insert the code to delete the map made after pressing the button? For both selectInput works normally, only the map that is not deleted from the screen.

  library(shiny)
  library(shinythemes)
  library(lubridate)
  library(googleway)
  
  set_key("API KEY")
  
  
  df1<- structure(
    list(
      Marketname = c("Market1","Market1", "Market2","Market2", "Market3", "Market3", "Market4", "Market4"),
      Latitude = c(-22.900200453490385, -22.900200453490385,-22.89279876292728,-22.89279876292728,-22.89107669207457,-22.89107669207457,-22.91668421655409,-22.91668421655409),
      Longitude = c(-48.448779371935494,-48.448779371935494, -48.45043377250408,-48.45043377250408,-48.44108027972275,-48.44108027972275,-48.43786997555729,-48.43786997555729)),
    row.names = c(NA, 8L), class = "data.frame")
  
  
  ui <- fluidPage(  
    shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                      br(),
                      tabPanel("Rota",
                               sidebarLayout(
                                 sidebarPanel(
                                   selectizeInput("market1", label = h5("Choose starting point:"), choices = NULL, 
                                                  multiple = TRUE,
                                                  options = list(maxItems = 1)),
                                   
                                   selectizeInput("market2", label = h5("Choose destination point:"), choices = NULL, 
                                                  multiple = TRUE,
                                                  options = list(maxItems = 1)),
                                   
                                   actionButton(inputId = "getRoute", label = "Get route"),
                                   actionButton(inputId = "reset", label = "Reset")),
  
                                 mainPanel(
                                   tabsetPanel(      
                                     tabPanel("Route",google_mapOutput(outputId = "mapWarsaw"),
                                     )
                                   ))
                               ))))
  
  server <- function(input, output,session) {
    
    
    observe({
      updateSelectizeInput(session, "market1",
                           choices = unique(df1$Marketname)
      )
    })
    
    
    observe({
      excludeOption <- NULL
      if (!is.null(input$market1)) {
        excludeOption <- input$market1
      }
      
      updateSelectizeInput(session, "market2",
                           choices = unique(df1$Marketname[df1$Marketname != excludeOption])
      )
    })
    
    observeEvent(input$getRoute, {
      origin <- df1[df1$Marketname == input$market1, c("Latitude", "Longitude")][1, ]
      
      destination <- df1[df1$Marketname == input$market2, c("Latitude", "Longitude")][1, ]
    
      route <- google_directions(origin = origin, 
                                 destination = destination,
                                 mode = "driving")
      
      df_routes <- data.frame(polyline = direction_polyline(route))
      
      df_way <- cbind(
        route$routes$legs[[1]]$end_location,
        data.frame(address = route$routes$legs[[1]]$end_address)
      )
      
      
      m3<-google_map() %>%
        add_polylines(data = df_routes, polyline = "polyline", stroke_weight = 4)
      
      output$mapWarsaw <- renderGoogle_map({
        m3
      })
    })
    
     observeEvent(input$reset, {
       updateSelectInput(session, "market1", selected = "")
       updateSelectInput(session, "market2", selected = "")
                
  })
    
  }
  
  shinyApp(ui = ui, server = server)

Here I pressed reset, the selectInput was cleared, but the map was not, so I would like to insert some code that cleared the screen. Of course, after inserting the selectInput options again, the map was generated normally on the screen.

enter image description here

Without an API key I could not fully test the approach below, but I it works for rendering and removing a table in place of a map. I pulled output$mapWarsaw out of the observeEvent() and made it a standalone section. I also created a reactiveValue() that stores the route map data. This was done because it's easy to overwrite this object to NULL when not needed. Thus, the observeEvent() for getting a route now produces the map object and as a last step updates the reactiveValue() to the map data. Then, output$mapWarsaw was updated with a req() function, which will cause the output not to render if the data is NULL. Finally, a line was added to the reset observeEvent() to set the route map reactiveValue to NULL.

library(shiny)
library(shinythemes)
library(lubridate)
library(googleway)

set_key("API KEY")


df1<- structure(
  list(
    Marketname = c("Market1","Market1", "Market2","Market2", "Market3", "Market3", "Market4", "Market4"),
    Latitude = c(-22.900200453490385, -22.900200453490385,-22.89279876292728,-22.89279876292728,-22.89107669207457,-22.89107669207457,-22.91668421655409,-22.91668421655409),
    Longitude = c(-48.448779371935494,-48.448779371935494, -48.45043377250408,-48.45043377250408,-48.44108027972275,-48.44108027972275,-48.43786997555729,-48.43786997555729)),
  row.names = c(NA, 8L), class = "data.frame")


ui <- fluidPage(  
  shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                    br(),
                    tabPanel("Rota",
                             sidebarLayout(
                               sidebarPanel(
                                 selectizeInput("market1", label = h5("Choose starting point:"), choices = NULL, 
                                                multiple = TRUE,
                                                options = list(maxItems = 1)),
                                 
                                 selectizeInput("market2", label = h5("Choose destination point:"), choices = NULL, 
                                                multiple = TRUE,
                                                options = list(maxItems = 1)),
                                 
                                 actionButton(inputId = "getRoute", label = "Get route"),
                                 actionButton(inputId = "reset", label = "Reset")),
                               
                               mainPanel(
                                 tabsetPanel(      
                                   tabPanel("Route",
                                            google_mapOutput(outputId = "mapWarsaw"),
                                            tableOutput('test')
                                   )
                                 ))
                             ))))

server <- function(input, output,session) {
  
  observe({
    updateSelectizeInput(session, "market1",
                         choices = unique(df1$Marketname)
    )
  })
  
  observe({
    excludeOption <- NULL
    if (!is.null(input$market1)) {
      excludeOption <- input$market1
    }
    
    updateSelectizeInput(session, "market2",
                         choices = unique(df1$Marketname[df1$Marketname != excludeOption])
    )
  })
  
  observeEvent(input$getRoute, {
    origin <- df1[df1$Marketname == input$market1, c("Latitude", "Longitude")][1, ]
    
    destination <- df1[df1$Marketname == input$market2, c("Latitude", "Longitude")][1, ]

    route <- google_directions(origin = origin,
                               destination = destination,
                               mode = "driving")

    df_routes <- data.frame(polyline = direction_polyline(route))

    df_way <- cbind(
      route$routes$legs[[1]]$end_location,
      data.frame(address = route$routes$legs[[1]]$end_address)
    )

    m3<-google_map() %>%
      add_polylines(data = df_routes, polyline = "polyline", stroke_weight = 4)

    # update the reactiveValue with the route map
    route_map$d <<- m3
    
  })
  
  route_map = reactiveValues(d = NULL)
  
  output$mapWarsaw <- renderGoogle_map({
    req(route_map$d)
    route_map$d
  })
  
  
  observeEvent(input$reset, {
    updateSelectInput(session, "market1", selected = character(0))
    updateSelectInput(session, "market2", selected = character(0))
    route_map$d <<- NULL
  })
  
}

shinyApp(ui = ui, server = server)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.