2016-09-07 12 views
0

リーフレットを作成しようとしています。シャイニーなアプリですが、私はWarning: Error in derivePoints: addMarkers requires non-NULL longitude/latitude valuesエラーを受け取り続けます。私はこのコードを添付しています。また、入力データファイルのスクリーンショットとダウンロードするリンク。光沢のあるリーフレットAddMarkers NULL値のエラー

DataBooks.csv

enter image description here

GPSBook.csv

enter image description here

コード:

library(shiny) 
    library(leaflet) 

    Location_levels=list(Institutional=0, Provincial=1, National=2, International=3) 
    DataBook <- read.csv("~/R_Projects/TNL_Network/DataBook.csv", comment.char="#") 
    GPSBook <- read.csv("~/R_Projects/TNL_Network/GPSBook.csv", comment.char="#") 

    ## Create content for the popups in the markers 
    popUpContent <- function(ins_id){ 
     subs<-subset(DataBook, Institute_id==ins_id) 
     name <- subs$Institute[[1]] 
     return(name[[1]]) 
    } 

    ## Get unique markers based on the location type selected. This function calls the popup content function above and returns a dataframe 
    markerData <- function(location){ 
     subs1<-subset(DataBook, Location_level<=Location_levels[location]) 
     unique_ins_ids<-levels(factor(subs1$Institute_id)) 
     mdata.list <- vector("list", length(unique_ins_ids)) 
     for(i in 1:length(unique_ins_ids)){ 
     mdata.list[[i]] <- list(subset(GPSBook, Institute_id==unique_ins_ids[i])["Longitude"][[1]], subset(GPSBook, Institute_id==unique_ins_ids[i])["Latitude"][[1]], 
         as.character(popUpContent(unique_ins_ids[i]))) 
     } 
     solution <- do.call('rbind', mdata.list) 
     dataf<-data.frame(solution) 
     colnames(dataf)<-c("lat", "long", "Msg") ## I ihave mixed up the origincal longitude and latitude. I invert it here. 
     return(dataf) 
    } 

    ## Function to create initial data. 
    initData <- function(){ 
     return(markerData("International")) 
    } 
    init_dataset <- initData() 

    ui <- fluidPage(
     leafletOutput("mymap"), 
     p(), 
     radioButtons(inputId = "radio", label = "", choices = as.list(levels(DataBook$Location)), selected = "International") 
    ) 

    server <- function(input, output, session) { 
     observe({ 
     proxy <- leafletProxy("mymap", data = markerData(input$radio)) 
     proxy %>% clearMarkers() 
     proxy %>% addMarkers() 
     }) 

     output$mymap <- renderLeaflet({ 
     leaflet(data = markerData(input$radio)) %>% addTiles() %>% 
      addMarkers() 
     }) 
    } 

    shinyApp(ui, server) 

おかげで助けをたくさん。 ファイルへのリンク。私は私があなたの意図を理解している願っています

https://drive.google.com/open?id=0B-TWCTRv7UM1bnVpWEIxTnB2d28

https://drive.google.com/open?id=0B-TWCTRv7UM1cjBxNnlhR2ZXc0U

+0

csvファイルを提供できますか?コードをテストするためにファイルからいくつかのサンプル? –

+0

質問を編集してデータファイルを追加しました。助けてくれてありがとう。 – ssm

答えて

2

。はいの場合、これは多く簡略化することができます。 これが私のやり方です。 (ちょうどcsvファイルがある正しいディレクトリに戻って変更してください)。コード:データはそれらがあることを期待するリーフレットのように調製していなかったので

library(shiny) 
    library(leaflet) 


    DataBook <- read.csv("./data/DataBook.csv", comment.char="#") 
    GPSBook <- read.csv("./data/GPSBook.csv", comment.char="#") 
    names(GPSBook) <- names(GPSBook)[c(1,2,4,3)] 

    ui <- fluidPage(
      leafletOutput("mymap"), 
      p(), 
      radioButtons(inputId = "radio", label = "", choices = as.list(levels(DataBook$Location)), selected = "International") 
    ) 

    server <- function(input, output, session) { 

      location <- reactive({ 
        tmp <- subset(DataBook, Location_level <= Location_levels[input$radio]) 
        uniqueIds <- unique(tmp$Institute_id) 
        tmpGps <- subset(GPSBook, Institute_id %in% uniqueIds) 


      }) 

      observe({ 
        proxy <- leafletProxy("mymap", data = location()) 
        proxy %>% clearMarkers() 
        proxy %>% addMarkers(popup = ~as.character(Name)) 
      }) 

      output$mymap <- renderLeaflet({ 
        leaflet(data = GPSBook) %>% addTiles() %>% 
          addMarkers(popup = ~as.character(Name)) 
      }) 
    } 

    shinyApp(ui, server) 

あなたの元のコードでは、関数は、リストを作成しました。

+0

こんにちは、ありがとうございました。コードは素晴らしい作品です。 1つの質問は、どのようにマーカーを表示するだけにするためにビューをリセットすることができます。したがって、都道府県を選択すると、マップを拡大するだけです。再度、感謝します。 – ssm

+0

これをリーフレットのプロキシ部分に追加してみてください:proxy%>%setView(lng =平均(location $)、lat = mean(location $)、zoom = 4) –

+0

ありがとうございます。私もこのコードを使ってやった。 lat2 = min(location()$ Latitude)) ' – ssm

関連する問題