使用 Shiny、Leaflet 和 rCharts 在 R 中创建带有标记的交互式网络地图

Create interactive webmap with markers in R using Shiny, Leaflet and rCharts

我正在尝试使用 Shiny、Leaflet 和 rCharts(该结构大致基于 http://ramnathv.github.io/bikeshare 应用程序)在 R 中创建交互式网络地图以显示风暴。

这个想法是用户一次选择一个风暴名称 (df$Name) 并且该风暴的标记 (lat/long) 显示在 Leaflet 地图中(缩放 in/out功能)。

我无法让 Leaflet 为每个单独的风暴名称加载新地图和标记。任何 help/advice 将不胜感激!

这是数据 (TCs.Rda) 的样子(上传的示例数据文件 here):

 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

server.R

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      
       })  
    })

ui.R

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")))
      )
  ))

我终于自己弄明白了!另一个 post () 表示代码行“map$set(dom='myChart')”可能会阻止 Leaflet 在每次选择时重新加载新地图。

我不认为这可能是这里的问题(我的示例有点不同并且不使用 geojson),但显然是。

这是我的工作代码,以防对其他人有所帮助 -

server.R

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      
  })  
})

ui.R

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")))
  )
  ))

结果如下所示:

强烈推荐rstudio's leaflet

我有一个关于它的介绍教程以及为什么它在这里如此棒:http://www.r-bloggers.com/the-leaflet-package-for-online-mapping-in-r/

此外,我正在使用它为 DfT 构建交互式规划工具:https://github.com/npct/pct

祝你好运!