[英]Create interactive webmap with markers in R using Shiny, Leaflet and rCharts
I am trying to create an interactive webmap in R to display storms using Shiny, Leaflet and rCharts (the structure is loosely based on the http://ramnathv.github.io/bikeshare app). 我正在尝试在R中创建一个交互式webmap,使用Shiny,Leaflet和rCharts显示风暴(该结构基于http://ramnathv.github.io/bikeshare应用程序)。
The idea is that the user selects one storm name at a time (df$Name) and the markers for that storm (lat/long) are displayed in a Leaflet map (with zoom in/out function). 这个想法是用户一次选择一个风暴名称(df $ Name),并且该风暴的标记(lat / long)显示在Leaflet地图中(具有放大/缩小功能)。
I cannot get Leaflet to load a new map and markers for each separate storm name. 我无法让Leaflet为每个单独的风暴名称加载新的地图和标记。 Any help/advice would be greatly appreciated!
任何帮助/建议将不胜感激!
This is what the data (TCs.Rda) look like (sample data file uploaded here ): 这就是数据(TCs.Rda)的样子( 此处上传的示例数据文件):
Year Name Wind lat long
2010 BONNIE 15 30.100 -91.000
2010 FIVE 25 30.000 -88.900
2010 FIVE 25 30.400 -88.800
2010 FIVE 25 30.800 -88.600
library(shiny); library(rCharts); library(leaflet)
load("TCs.Rda")
name <- sort(unique(data$Name))
shinyServer(function(input, output){
dataset <- reactive({ df <- data[data$Name == input$name, ] })
output$add <- renderText({paste("Storm name:", input$name)})
output$Controls <- renderUI({
list(selectInput("name", "Select storm", name, selected=name[1])) })
output$myChart <- renderMap({
df <- dataset()
map <- Leaflet$new()
map$setView(c(35, -80),zoom=5)
map$tileLayer(provider='Stamen.TonerLite')
for (i in 1:nrow(df)) {
map$marker(c(df[i, "lat"], df[i, "long"]), bindPopup=df[i, "Wind"])}
map$set(dom='myChart')
map$enablePopover(TRUE)
map$fullScreen(TRUE)
map
})
})
library(shiny); library(rCharts); library(leaflet)
shinyUI(pageWithSidebar(
headerPanel("Storm tracks"),
sidebarPanel(h3("My subtitle"), uiOutput("Controls")),
mainPanel(
tabsetPanel(
h3(textOutput("add")),
tabPanel("map", tags$style(".leaflet {height: 400px;}"), # I can only get this to work with a tabset panel, but ideally both the textOutput and map would go directly in the mainPanel
showOutput("myChart", "leaflet")))
)
))
I figured it out myself eventually! 我终于弄明白了! Another post ( Shiny renders a responsive rCharts leaflet map once, but is blank if you change the input variable ) indicated that the line of code "map$set(dom='myChart')" could be preventing Leaflet from reloading a new map on each selection.
另一篇文章( Shiny渲染响应式rCharts传单一次,但如果更改输入变量则为空白 )表示代码行“map $ set(dom ='myChart')”可能阻止Leaflet重新加载新地图每个选择。
I didn't think that could be the issue here (my example is a little different and does not use geojson), but apparently it was. 我不认为这可能是问题(我的例子有点不同,并没有使用geojson),但显然它是。
This is my working code, in case it helps anyone else - 这是我的工作代码,万一它可以帮助其他人 -
library(shiny);library(rCharts);library(leaflet)
load("TCs.Rda")
name <- sort(unique(data$Name))
shinyServer(function(input, output){
dataset <- reactive({df<- data[data$Name == input$name, ]})
output$add <- renderText({paste("Storm name:", input$name)})
output$Controls <- renderUI({list(selectInput("name", "Select storm", name, selected=name[1]) ) })
output$myChart <- renderMap({
df <- dataset()
map <- Leaflet$new()
map$setView(c(35, -80),zoom=3)
map$tileLayer(provider='Stamen.TonerLite')
for (i in 1:nrow(df)) {map$marker(c(df[i, "lat"], df[i, "long"]))}
map$fullScreen(TRUE)
map
})
})
library(shiny); library(rCharts); library(leaflet)
shinyUI(pageWithSidebar(
headerPanel("Storm tracks"),
sidebarPanel(h3("My subtitle"), uiOutput("Controls")),
mainPanel(
tabsetPanel(
tabPanel("map", h3(textOutput("add")),
tags$style(".leaflet {height: 400px;}"),
showOutput("myChart", "leaflet")))
)
))
And the result looks like this: 结果如下:
I strongly recommend rstudio's leaflet . 我强烈推荐rstudio的传单 。
I've an introductory tutorial on it and why it's so awesome here: http://www.r-bloggers.com/the-leaflet-package-for-online-mapping-in-r/ 我有一个关于它的入门教程以及为什么它在这里非常棒: http : //www.r-bloggers.com/the-leaflet-package-for-online-mapping-in-r/
Also, I'm using it to build an interact planning tool for the DfT: https://github.com/npct/pct 此外,我正在使用它为DfT构建交互式规划工具: https : //github.com/npct/pct
Good luck with it! 祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.