切换tabPanel后googleway生成的Shiny地图无法更新求助
Googleway地图切换Tab面板后无法随区域选择更新
问题描述
我用googleway和leaflet包在Shiny中生成了两张地图,初始状态下通过selectInput选择区域时,两张地图都能正常更新位置。但切换到「地点间距离」Tab面板后,再次选择其他区域,只有leaflet生成的地图会更新,googleway生成的地图完全没有变化,求解决办法。
复现代码
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)
状态截图
正常状态地图

切换Tab面板后再次选择区域的地图

内容的提问来源于stack exchange,提问作者Carlo Soares
相关产品推荐
相关产品推荐

