在 R Shiny 中动态更改传单地图中心

Dynamically change leaflet map center in R Shiny

我正在制作一个 R Shiny 应用程序,用户将在其中输入他们的城市、州和邮政编码,按下一个按钮,然后他们的位置(纬度、经度)将成为地图的新中心。通过 server.R:

中的这段代码收集输入
output$ntextCounter <- eventReactive(input$goButton, {
   citySelected <- as.character(input$city)
   stateSelected <- as.character(input$state)
   zipCodeSelected <- as.character(input$zipCode)
   location2 <- stri_detect(paste(as.character(input$city),
   as.character(input$state), as.character(input$zipCode), sep=", "), fixed = locationData$Location, opts_regex=stri_opts_regex(case_insensitive=TRUE))
   counter <<- counter + 1
   lat1 <- as.numeric(locationData[which(location2),]$latitude)
   lon1 <- as.numeric(locationData[which(location2),]$longitude)
   return(c(lat1, lon1))
})

我可以使用以下方法轻松查看 UI 中的新 latitude/longitude 值:

 verbatimTextOutput("ntextCounter")

但我需要能够将这些值 "return(c(lat1, lon1))" 传递到 ui.r:

传单地图中的中心 = c(Lat, Lon)
leafletMap("map", "100%", 365,
    initialTileLayer = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
    initialTileLayerAttribution = HTML('&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'),
    options=list(center = c(38.25, -93.85), zoom = 4, maxBounds = list(list(1, -180), list(78, 180))
    )),

我在 c(38.25, -93.85) 处有一个初始地图中心,但最终我希望能够将 ntextCounter 的更改值传递给它。我不确定这是范围界定问题还是什么,但我需要帮助才能将新的 lat/lon 值放入传单地图中心。

如有任何帮助,我们将不胜感激。提前致谢。

看来您是在 ui 方面制作传单。如果您希望它响应输入,您必须在服务器端使用 renderLeaflet 来实现。

您的坐标可以存储在 reactiveValues 中,您可以使用 observeEvent 更新它们:

location <- reactiveValues(lat = 38.25, lon = -93.85)
observeEvent(input$goButton, {
           city <- as.character(input$city)
           state <- as.character(input$state)
           zipCode <- as.character(input$zipCode)
           newloc <- stri_detect(paste(city, state, zipCode, sep=", "), 
                                    fixed = locationData$Location, 
                                    opts_regex=stri_opts_regex(case_insensitive=TRUE))
          location$lat <- as.numeric(locationData[which(newloc),]$latitude)
          location$lon <- as.numeric(locationData[which(newloc),]$longitude)
})