Shiny R 传单 AddMarkers NULL 值错误

问题描述 投票:0回答:1

我正在尝试创建一个传单闪亮应用程序,但我不断收到

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

r shiny r-leaflet
1个回答
3
投票

我希望我已经理解了你的意图。如果是的话,这可以简化很多。 我就是这样做的。 (只需改回 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)

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

© www.soinside.com 2019 - 2024. All rights reserved.