Googleway map does not update when using a dynamic bucketlist in r shiny

47 views Asked by At

I am building an r shiny application that uses sortable bucketlists and googleway maps to show airports by state. When first run, the user can drag an airport from one bucketlist to the other and then the airport shows up on the google map. However when I change states, the bucketlist updates but the map does not update. When I try to move an item from one bucketlist to the other, the map does not update and still shows the values from the old map.

library(shiny)
library(googleway)
library(sortable)
library(dplyr)


states<-c("CA","CA","CA","NY","NY","NY")
airport<-c("SFO","OAK","LAX","JFK","Spadero","KIUA")
lat<-c(37.615223,37.7156,33.9438,40.6446,40.8227,42.9069)
lon<-c(-122.38997,-122.2143,-118.4091,-73.7797,-72.7489,-77.3207)

airport_list<-data.frame(states,airport,lat,lon)
print("airport_list")
print(airport_list)


# ******************** UI **************************
ui <- fluidPage(
  titlePanel("Airport"),
  fluidRow(
    
    column(6,
           selectInput(inputId="chosen_state", label="Choose a state", choices=c("CA","NY"),selected="CA"),
           google_mapOutput(outputId = "myMap"),    
    ),
    column(6,
           
           uiOutput("bucket"),
           
    )
  ))

# ******************** Server **************************

server <- function(input, output,session){
  
  observeEvent(input$chosen_state,{
    places_list <- airport_list[airport_list$states == input$chosen_state, "airport"]
    print("places_list") 
    print(places_list) 
    output$bucket <- renderUI({bucket_list(header = "List of Airports",group_name = "bucket_list_container",orientation = "vertical",
                                           add_rank_list(text = "Show Airport",labels = places_list[1], input_id = "show_list"),
                                           add_rank_list(text = "Open Airports",labels = places_list,input_id = "open_list")
    )
      
    })
  })
  
  
  observeEvent(input$show_list,{
    
    trip_df <- airport_list[airport_list$airport %in% unlist(input$show_list), ]
    
    print("input$show_list")
    print(unlist(input$show_list))
    
    #Sort based on the order in the UI  
    trip_df <-trip_df[match(input$show_list, trip_df$airport), ]
    
    print("trip df")
    print(trip_df)   
    
    #I added a default value when the bucketlist first loads and that gets rid of grey box.
    output$myMap <- renderGoogle_map({
      google_map(data=trip_df,key = mapKey) %>%
        add_markers(lat="lat", lon="lon",label="airport")%>%
        add_polylines(lat="lat",lon="lon")
    })
    print("The map was updated")
  })
  
  
}

shinyApp(ui, server)

I tried to reset the map by drawing an empty map in the chosen_state observeEvent but that did not work.

For some reason, after the bucketlist is updated the map rendering does not work. The output$map block is running because I can print out "The map was updated" but it does not update.

0

There are 0 answers