Search code examples
rshinyr-leaflet

addMarkers not receiving points data from reactive function


I am trying to create a Leaflet in Shiny which filters a data set based on a number of parameters and plots only those points which have been selected by the user. Everything works, except one crucial part: when I run the app, I get the "Error: Point data not found; please provide addMarkers with data and/or lng/lat arguments" message. EDIT: I am getting this error even when input$question1 == FALSE. This makes sense to me as I have not defined an else or an initial dataset for plotting, but it may be relevant.

Here is a section of the dput for dataset:

dput(dataset[1:10,14:24]) 
structure(list(target.pops = structure(c(6L, 
6L, 18L, 31L, 21L, 27L, 27L, 18L, 6L, 27L), .Label = c("DD Adult", 
"DD Adult, DD Child", "DD Adult, DD Child, MH Adult", "DD Adult, DD Child, 
MH Adult, MH Child", 
"DD Adult, DD Child, MH Adult, MH Child, SA Adult", "DD Adult, DD Child, MH 
Adult, MH Child, SA Adult, SA Child", 
"DD Adult, DD Child, MH Adult, MH Child, SA Child", "DD Adult, DD Child, MH 
Child", 
"DD Adult, MH Adult", "DD Adult, MH Adult, MH Child", "DD Adult, MH Adult, 
MH Child, SA Adult", 
"DD Adult, MH Adult, MH Child, SA Adult, SA Child", "DD Adult, MH Adult, SA 
Adult", 
"DD Adult, MH Child", "DD Child, MH Adult, MH Child", "DD Child, MH Adult, 
MH Child, SA Child", 
"DD Child, MH Child", "DD Child, MH Child, SA Child", "DD Child, SA Child", 
"MH Adult", "MH Adult, MH Child", "MH Adult, MH Child, SA Adult", 
"MH Adult, MH Child, SA Adult, SA Child", "MH Adult, MH Child, SA Child", 
"MH Adult, SA Adult", "MH Adult, SA Adult, SA Child", "MH Child", 
"MH Child, SA Adult", "MH Child, SA Child", "SA Adult", "Unknown"
), class = "factor"), 
col.data = c(1L, 1L, 1L, 9L, 1L, 1L, 1L, 
1L, 1L, 1L), 
adult = c(1L, 1L, 0L, 9L, 1L, 0L, 0L, 0L, 1L, 
0L), dd = c(1L, 1L, 1L, 9L, 0L, 0L, 0L, 1L, 1L, 0L), mh = c(1L, 
1L, 1L, 9L, 1L, 1L, 1L, 1L, 1L, 1L),
sa = c(1L, 1L, 1L, 9L, 
0L, 0L, 0L, 1L, 1L, 0L), 
outpt = c(0L, 0L, 0L, 0L, 1L, 0L, 
1L, 0L, 0L, 0L), iw = c(1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L), b3 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L)),
.Names = c("status", "service", "target.pops", "child", "adult", "dd", "mh", 
"sa", "outpt", "iw", "b3"), row.names = c(NA, 10L), class = "data.frame")

Here is the reactive function I'm using to generate the filtered values and the renderLeaflet code:

server <- function(input, output) {
    
    points <- reactive ({
            if (input$question1 == TRUE) {
            filtered2 <- filter(dataset, col.data == 1)
            select_(dataset, ~lng, ~lat)
            }
    })

    output$map <- renderLeaflet({
            leaflet(dataset) %>%
                    addTiles() %>%
                    setView(-77.33, 35.5881, 7)
            addMarkers(points())
    })
    
}

Do you have any ideas what's going wrong?


Solution

  • I was able to fix this. After a week of being stuck, I changed the addMarkers syntax to

    addMarkers(data = points())
    

    and it works as expected.