ssm ssm - 2 months ago 26
R Question

Shiny R leaflet AddMarkers NULL value eror

I am trying to create a leaflet Shiny app however I keep getting the

Warning: Error in derivePoints: addMarkers requires non-NULL longitude/latitude values
Error. I have attached the code herewith. Also, a screenshot of the input data files and links to download.

DataBooks.csv

enter image description here

GPSBook.csv

enter image description here

Code:

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)


Thanks a lot for the help.
Links to files.

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

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

Answer

I hope I have understood you intention. If yes this can be simplified a lot. This is how I would do it. (just change back to the correct directories where your csv files are). The code:

    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)

In your original code the function was creating a list so the data was not prepared as leaflet would expect them to be.

Comments