2016-09-07 36 views
0

我想創建一個傳單Shiny應用但是我不斷收到Warning: Error in derivePoints: addMarkers requires non-NULL longitude/latitude values錯誤。我附上了代碼。此外,還有一個輸入數據文件和下載鏈接的截圖。閃亮的R小冊子AddMarkers NULL值eror

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

嗨,非常感謝您的幫助。代碼很好。一個問題,如何重置視圖以僅顯示可見標記。所以,如果我選擇省級,我只想讓地圖放大。再次感謝。 – ssm

+0

嘗試在小冊子代理部分添加以下內容:代理%>%setView(lng = mean(location()$ Longitude),lat = mean(location()$ Latitude),zoom = 4) –

+0

非常感謝。我也使用這個代碼。 'fitBounds(lng1 = max(location()$ Longitude),lat1 = max(location()$ Latitude), lng2 = min(location()$ Longitude),lat2 = min(location()$ Latitude))' – ssm