我正在制作一个 R Shiny 应用程序,用户将在其中输入他们的城市、州和邮政编码,按下按钮,然后他们的位置(经纬度)将成为 map 的新中心。输入是通过 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 中的新纬度/经度值:
verbatimTextOutput("ntextCounter")
但我需要能够将这些值“return(c(lat1, lon1))”传递到 ui.r 的传单 map 中的 center = c(Lat, Lon) :
leafletMap("map", "100%", 365,
initialTileLayer = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
initialTileLayerAttribution = HTML('© <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) 处有一个初始 map 中心,但最终我希望能够从 ntextCounter 传递更改值。我不确定这是一个范围问题还是什么,但我需要帮助将新的纬度/经度值放入传单 map 中心。
任何帮助将不胜感激。提前致谢。
最佳答案
您似乎正在 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)
})
关于r - 在 R Shiny 中动态更改传单 map 中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32358203/