The position of map (leaflet) not updating from dropdown user input R

I have a global variable called map_status, selectedDropdownData. I currently implemented the following and it works good.
Following is map.r

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>library(leaflet)
render_leaflet_map <- function(mapData, mapStatus, palette_colors = c("green", "yellow", "orange", "red"), min_zoom = 7, max_bounds_lat = c(31.3322, 37.0043), max_bounds_lng = c(-114.8183, -109.0452)) {
if (is.null(mapData)) return(NULL)
data <- mapData$data
dataset <- mapData$dataset
print(paste("From render lealet mapstatus", mapStatus))
# Compute range of the data to create breakpoints for the color palette
data_range <- range(data[[dataset]], na.rm = TRUE)
breaks <- quantile(data[[dataset]], probs = seq(0, 1, length.out = 5), na.rm = TRUE) # Create 4 intervals
# Create a color palette based on the breaks
pal <- colorBin(palette = palette_colors, domain = data[[dataset]], bins = breaks, na.color = "transparent")
leaflet(data, options = leafletOptions(minZoom = min_zoom, maxBounds = list(lat = max_bounds_lat, lng = max_bounds_lng))) %>%
addTiles() %>%
setView(lng = mapStatus[['map_lng']], lat = mapStatus[['map_lat']], zoom = mapStatus[['map_zoom']]) %>%
addPolylines(
color = ~pal(data[[dataset]]),
weight = 4,
opacity = 1,
labelOptions = labelOptions(
direction = 'auto',
noHide = FALSE,
textOnly = FALSE,
style = list('background' = 'rgba(255, 255, 255, 0.8)', 'padding' = '5px', 'border' = '1px solid #cccccc')
)
) %>%
addLegend(
position = "bottomright",
pal = pal,
values = ~data[[dataset]],
title = "Range,
opacity = 0.7,
labFormat = labelFormat(prefix = "", suffix = "")
)
}
set_map_status <- function(mapStatus) {
print("Inside set_map_status")
mapStatus$map_lng <<- mapStatus$map_lng
mapStatus$map_lat <<- mapStatus$map_lat
mapStatus$map_zoom <<- mapStatus$map_zoom
print(paste(":):):):):) mapStatus", mapStatus))
}
# Function to observe and maintain the zoom status of maps
observe_map_status <- function(input, mapStatus) {
observe({
req(input$map_center, input$map_zoom)
isolate({
if (!is.null(input$map_center) &&
(input$map_center$lat != mapStatus$map_lat ||
input$map_center$lng != mapStatus$map_lng ||
input$map_zoom != mapStatus$map_zoom)) {
mapStatus$map_lng <<- input$map_center$lng
mapStatus$map_lat <<- input$map_center$lat
mapStatus$map_zoom <<- input$map_zoom# Increment the render trigger
print(paste("In map if"))
}
set_map_status(mapStatus)
})
})
</code>
<code>library(leaflet) render_leaflet_map <- function(mapData, mapStatus, palette_colors = c("green", "yellow", "orange", "red"), min_zoom = 7, max_bounds_lat = c(31.3322, 37.0043), max_bounds_lng = c(-114.8183, -109.0452)) { if (is.null(mapData)) return(NULL) data <- mapData$data dataset <- mapData$dataset print(paste("From render lealet mapstatus", mapStatus)) # Compute range of the data to create breakpoints for the color palette data_range <- range(data[[dataset]], na.rm = TRUE) breaks <- quantile(data[[dataset]], probs = seq(0, 1, length.out = 5), na.rm = TRUE) # Create 4 intervals # Create a color palette based on the breaks pal <- colorBin(palette = palette_colors, domain = data[[dataset]], bins = breaks, na.color = "transparent") leaflet(data, options = leafletOptions(minZoom = min_zoom, maxBounds = list(lat = max_bounds_lat, lng = max_bounds_lng))) %>% addTiles() %>% setView(lng = mapStatus[['map_lng']], lat = mapStatus[['map_lat']], zoom = mapStatus[['map_zoom']]) %>% addPolylines( color = ~pal(data[[dataset]]), weight = 4, opacity = 1, labelOptions = labelOptions( direction = 'auto', noHide = FALSE, textOnly = FALSE, style = list('background' = 'rgba(255, 255, 255, 0.8)', 'padding' = '5px', 'border' = '1px solid #cccccc') ) ) %>% addLegend( position = "bottomright", pal = pal, values = ~data[[dataset]], title = "Range, opacity = 0.7, labFormat = labelFormat(prefix = "", suffix = "") ) } set_map_status <- function(mapStatus) { print("Inside set_map_status") mapStatus$map_lng <<- mapStatus$map_lng mapStatus$map_lat <<- mapStatus$map_lat mapStatus$map_zoom <<- mapStatus$map_zoom print(paste(":):):):):) mapStatus", mapStatus)) } # Function to observe and maintain the zoom status of maps observe_map_status <- function(input, mapStatus) { observe({ req(input$map_center, input$map_zoom) isolate({ if (!is.null(input$map_center) && (input$map_center$lat != mapStatus$map_lat || input$map_center$lng != mapStatus$map_lng || input$map_zoom != mapStatus$map_zoom)) { mapStatus$map_lng <<- input$map_center$lng mapStatus$map_lat <<- input$map_center$lat mapStatus$map_zoom <<- input$map_zoom# Increment the render trigger print(paste("In map if")) } set_map_status(mapStatus) }) }) </code>
library(leaflet)

render_leaflet_map <- function(mapData, mapStatus, palette_colors = c("green", "yellow", "orange", "red"), min_zoom = 7, max_bounds_lat = c(31.3322, 37.0043), max_bounds_lng = c(-114.8183, -109.0452)) {
  if (is.null(mapData)) return(NULL)
  
  data <- mapData$data
  dataset <- mapData$dataset
  print(paste("From render lealet mapstatus", mapStatus))
  
  
  # Compute range of the data to create breakpoints for the color palette
  data_range <- range(data[[dataset]], na.rm = TRUE)
  breaks <- quantile(data[[dataset]], probs = seq(0, 1, length.out = 5), na.rm = TRUE) # Create 4 intervals
  
  # Create a color palette based on the breaks
  pal <- colorBin(palette = palette_colors, domain = data[[dataset]], bins = breaks, na.color = "transparent")
  
  leaflet(data, options = leafletOptions(minZoom = min_zoom, maxBounds = list(lat = max_bounds_lat, lng = max_bounds_lng))) %>%
    addTiles() %>%
    setView(lng = mapStatus[['map_lng']], lat = mapStatus[['map_lat']], zoom = mapStatus[['map_zoom']]) %>%
    addPolylines(
      color = ~pal(data[[dataset]]),
      weight = 4,
      opacity = 1,
      labelOptions = labelOptions(
        direction = 'auto',
        noHide = FALSE,
        textOnly = FALSE,
        style = list('background' = 'rgba(255, 255, 255, 0.8)', 'padding' = '5px', 'border' = '1px solid #cccccc')
      )
    ) %>%
    addLegend(
      position = "bottomright",
      pal = pal,
      values = ~data[[dataset]],
      title = "Range,
      opacity = 0.7,
      labFormat = labelFormat(prefix = "", suffix = "")
    )
}
    set_map_status <- function(mapStatus) {
  print("Inside set_map_status")
  mapStatus$map_lng <<- mapStatus$map_lng
  mapStatus$map_lat <<- mapStatus$map_lat
  mapStatus$map_zoom <<- mapStatus$map_zoom
  print(paste(":):):):):) mapStatus", mapStatus))
}
# Function to observe and maintain the zoom status of maps
observe_map_status <- function(input, mapStatus) {
  observe({
    req(input$map_center, input$map_zoom)
    isolate({
      if (!is.null(input$map_center) &&
          (input$map_center$lat != mapStatus$map_lat ||
           input$map_center$lng != mapStatus$map_lng || 
           input$map_zoom != mapStatus$map_zoom)) {
        
        mapStatus$map_lng <<- input$map_center$lng
        mapStatus$map_lat <<- input$map_center$lat
        mapStatus$map_zoom <<- input$map_zoom# Increment the render trigger
        
        print(paste("In map if"))
      }
      set_map_status(mapStatus)
    })
  })

}

I call these functions in server.r

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> # Call the observe_map_status function
observe_map_status(input, mapStatus)
# Render the heat map
output$map <- renderLeaflet({
mapData <- currentMapData()
render_leaflet_map(mapData, mapStatus)
})
</code>
<code> # Call the observe_map_status function observe_map_status(input, mapStatus) # Render the heat map output$map <- renderLeaflet({ mapData <- currentMapData() render_leaflet_map(mapData, mapStatus) }) </code>
 # Call the observe_map_status function
  observe_map_status(input, mapStatus)
  # Render the heat map
  output$map <- renderLeaflet({
    mapData <- currentMapData()
    render_leaflet_map(mapData, mapStatus)
  })

The above code works and maintains the map status when the user zooms in/out, applies filters etc. But now I also want the map to zoom in to the location depending on the input user selected from the dropdown. I wrote a similar function in map.r

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>observe_selectedDropdownData <- function(mapStatus, selectedDropdownData) {
observe({
isolate({
if ((!is.null(selectedDropdownData))) {
print(paste("In observe_selectedDropdownData"))
print(paste("selectedDropdownData$latitude ", selectedDropdownData$latitude))
print(paste("selectedDropdownData$longitude ", selectedDropdownData$longitude))
centerLat <- mean(range(selectedDropdownData$latitude))
centerLng <- mean(range(selectedDropdownData$longitude))
print(paste("centerLat ", centerLat))
print(paste("centerLng ", centerLng))
mapStatus$map_lng <<- centerLat
mapStatus$map_lat <<- centerLng
mapStatus$map_zoom <<- 10# Increment the render trigger
}
})
set_map_status(mapStatus)
print(paste("Mapstatus ", mapStatus))
})
}
</code>
<code>observe_selectedDropdownData <- function(mapStatus, selectedDropdownData) { observe({ isolate({ if ((!is.null(selectedDropdownData))) { print(paste("In observe_selectedDropdownData")) print(paste("selectedDropdownData$latitude ", selectedDropdownData$latitude)) print(paste("selectedDropdownData$longitude ", selectedDropdownData$longitude)) centerLat <- mean(range(selectedDropdownData$latitude)) centerLng <- mean(range(selectedDropdownData$longitude)) print(paste("centerLat ", centerLat)) print(paste("centerLng ", centerLng)) mapStatus$map_lng <<- centerLat mapStatus$map_lat <<- centerLng mapStatus$map_zoom <<- 10# Increment the render trigger } }) set_map_status(mapStatus) print(paste("Mapstatus ", mapStatus)) }) } </code>
observe_selectedDropdownData <- function(mapStatus, selectedDropdownData) {
  observe({
    isolate({
      if ((!is.null(selectedDropdownData))) {
        print(paste("In observe_selectedDropdownData"))
        print(paste("selectedDropdownData$latitude ", selectedDropdownData$latitude))
        print(paste("selectedDropdownData$longitude ", selectedDropdownData$longitude))
        centerLat <- mean(range(selectedDropdownData$latitude))
        centerLng <- mean(range(selectedDropdownData$longitude))
        
        print(paste("centerLat ", centerLat))
        print(paste("centerLng ", centerLng))
        mapStatus$map_lng <<- centerLat
        mapStatus$map_lat <<- centerLng
        mapStatus$map_zoom <<- 10# Increment the render trigger
      }
    })
      set_map_status(mapStatus)
      print(paste("Mapstatus ", mapStatus))
  })
}

and tried calling it in server.r as follows

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> observe({
req(input$subCorridorSelect)
if (!is.null(input$subCorridorSelect)) {
selectedDropdownData <<- dropdownData()[dropdownData()$sub_corridor == input$subCorridorSelect, ]
} else {
selectedDropdownData <<- NULL # Keep selectedData as NULL if no selection is made
}
observe_selectedDropdownData(mapStatus, selectedDropdownData)
})
</code>
<code> observe({ req(input$subCorridorSelect) if (!is.null(input$subCorridorSelect)) { selectedDropdownData <<- dropdownData()[dropdownData()$sub_corridor == input$subCorridorSelect, ] } else { selectedDropdownData <<- NULL # Keep selectedData as NULL if no selection is made } observe_selectedDropdownData(mapStatus, selectedDropdownData) }) </code>
  observe({
    
    req(input$subCorridorSelect)
    
    if (!is.null(input$subCorridorSelect)) {
      selectedDropdownData <<- dropdownData()[dropdownData()$sub_corridor == input$subCorridorSelect, ]
    } else {
      selectedDropdownData <<- NULL  # Keep selectedData as NULL if no selection is made
    }
    
    observe_selectedDropdownData(mapStatus, selectedDropdownData)
  })

The value of map status gets updated. But that region is not zoomed in the map.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật