Shiny R leaflet AddMarkers NULL值eror

时间:2016-09-07 00:04:28

标签: r csv shiny leaflet rstudio

我正在尝试创建一个传单Shiny应用程序,但我不断收到Warning: Error in derivePoints: addMarkers requires non-NULL longitude/latitude values错误。我已附上此代码。此外,输入数据文件的屏幕截图和下载链接。

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

1 个答案:

答案 0 :(得分: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)

在原始代码中,该函数正在创建一个列表,因此数据没有像传单所期望的那样准备好。