簡體   English   中英

使用Shiny,Leaflet和rCharts在R中創建帶有標記的交互式webmap

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

我正在嘗試在R中創建一個交互式webmap,使用Shiny,Leaflet和rCharts顯示風暴(該結構基於http://ramnathv.github.io/bikeshare應用程序)。

這個想法是用戶一次選擇一個風暴名稱(df $ Name),並且該風暴的標記(lat / long)顯示在Leaflet地圖中(具有放大/縮小功能)。

我無法讓Leaflet為每個單獨的風暴名稱加載新的地圖和標記。 任何幫助/建議將不勝感激!

這就是數據(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

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

我終於弄明白了! 另一篇文章( Shiny渲染響應式rCharts傳單一次,但如果更改輸入變量則為空白 )表示代碼行“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的傳單

我有一個關於它的入門教程以及為什么它在這里非常棒: http//www.r-bloggers.com/the-leaflet-package-for-online-mapping-in-r/

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

祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM