rshinyr-leafletgoogleway

The map generated by the googleway package is not updated after pressing the other tabPanel


The code below generates two maps, one being generated by the googleway package and the other by the leaflet package. See in the image that a region is selected in selectInput and automatically two maps are generated. If I change my selectInput with another option, the map automatically updates the new locations. This is working fine. The problem is when I go to the other tabPanel, that is "Distance between locations", and I try to choose another region to showe the map again, only the map generated in the leafletupdates, the map generated by googleway does not, this is strange. Any help on this problem?

library(shiny)
library(shinythemes)
library(googleway)
library(shinyjs)
library(dplyr)
library(leaflet)

set_key("API KEY")

df1<-structure(list(ETEs = c("Location1", "Location2", 
                             "Location3", "Location4", "Location5", "Location6", 
                             "Location7", "Location8"), Latitude = c(-22.8851078223344, 
                                                                     -22.8315940282463, -22.9269302273894, -22.7168354545552, -22.4049856273179, 
                                                                     -23.6335639883851, -23.8650940097111, -22.2258061474773), 
                    Longitude = c(-48.4939312250395,-48.4298167144681, -48.4594335076124, -48.5783308965308, 
                                  -48.1690878117765,-49.3218749756598, -48.0149391697704, -48.7246763738941), 
                    Region = c("Centro-Oeste Paulista", "Centro-Oeste Paulista", "Centro-Oeste Paulista", "Centro-Oeste Paulista", "Nordeste Paulista",
                               "Nordeste Paulista", "Nordeste Paulista", "Nordeste Paulista")), row.names = c(NA, -8L), class = "data.frame")


function.test<-function(df1,selected_regions){

  df_filtered <- df1 %>% 
    filter(Region %in% selected_regions)
  
  plot1<-google_map() %>%
    add_markers(data = df_filtered, lat = "Latitude", lon = "Longitude", info_window = df_filtered$ETEs)%>%
    clear_traffic() %>%
    clear_polylines() %>%
    clear_markers() %>%
    add_traffic() 
  
  plot2<- leaflet() %>%
    addMarkers(data = df_filtered, lat = ~Latitude, lng = ~Longitude) 
  
  
  return(list(
    "Plot1" = plot1,
    "Plot2" = plot2
  ))
}


ui <- fluidPage(
  
  useShinyjs(), 
  
  shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                    br(),
                    tabPanel("",
                             sidebarLayout(
                               sidebarPanel(
                                 
                                 selectizeInput("hours",
                                                label = h5("Choose the region:"), choices = NULL,
                                                multiple = TRUE,
                                                options = list(maxItems = 1))),
                               
                               
                               mainPanel(
                                 tabsetPanel(      
                                   tabPanel("Map",google_mapOutput(outputId = "mapWarsaw1",width = "80%", height = "400"),leafletOutput(outputId = "mapWarsaw2", width = "80%", height = "400")),
                                   tabPanel("Distance between locations"))
                               )
                             )
                    )
  ))


server <- function(input, output,session) {
  
  df1_reactive <- reactive(function.test(df1, input$hours))
  
  observe({
    updateSelectizeInput(session, "hours",
                         choices = unique(df1$Region)
    )
  })
  
  output$mapWarsaw1 <- renderGoogle_map({
    req(input$hours)
    df1_reactive()[[1]]
  })
  
  output$mapWarsaw2 <- renderLeaflet({
    req(input$hours)
    df1_reactive()[[2]]
  })

}

shinyApp(ui = ui, server = server)

Map OK enter image description here

Map after press tabPanel Distance between locations and try to generate the maps again enter image description here


Solution

  •  df1_reactive <- reactive(function.test(df1, input$hours))
    

    I'd start by swapping this to an eventReactive.

    df1_reactive <- eventReactive(input$hours, {
                                  function.test(df1, input$hours)
                    })
    

    But I think your problem is that by switching tabs you are suspending the execution. See here: https://shiny.rstudio.com/reference/shiny/latest/outputoptions

    Unfortunately, I can't reproduce without an API key but try this:

    # Disable suspend for output$myplot
    outputOptions(output, "mapWarsaw1", suspendWhenHidden = FALSE)