r - 在 R Shiny 中动态更改传单 map 中心

标签 r maps parameter-passing shiny leaflet

我正在制作一个 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('&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) 处有一个初始 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/

相关文章:

r - 突出情节中的几个点

xml - 使用并行化通过 R 抓取网页

r - 强制 R 函数调用自给自足

python - Python 中的 Google App Engine 和 Google Maps 邻近搜索

ios - 如何摆脱 iOS 版 Google map 上的兴趣点

java - 合并排序中的参数传递

r - 为大型数据集加速 plot() 函数

c++ - 如何将参数和函数传递给 pthread_create

c - C 中将字符串传递给函数参数

security - HTTPS 安全 IE 诺基亚 map