1

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())
    })

}

Any ideas what's going wrong? Thanks in advance for any insights!

  • 2
    We don't know what `dataset`, or `col.data` looks like. Can you `dput` so we can get an idea of the data structure to better help? – Carl Boneri Apr 26 '17 at 15:48
  • Sorry, this is my first time posting here, so I appreciate the guidance. `dataset` is a data frame of several different variable types - it's very large, the `dput` is huge. `col.data` (and all of the columns being filtered) is an integer column with values 1, 0, and 9. – thepinkfreudian Apr 26 '17 at 15:53
  • Could you perhaps use `dplyr::sample_frac` on the dataset without losing the integrity of the question? It's hard to help without debugging if it's related to the data itself or the function calls – Carl Boneri Apr 26 '17 at 15:55
  • then give us `dput(head(dataset))`. My guess is the filter argument is not correctly specified, but without the dataset thats only guessing,... – Tonio Liebrand Apr 26 '17 at 15:57
  • updated to include part of the `str(dataset)`, edited to remove addresses & names of businesses. (The `head` function won't be useful because the first columns in `dataset` is all of that info.) – thepinkfreudian Apr 26 '17 at 16:07
  • You example is not reproducible. But perhaps [this](http://stackoverflow.com/a/1686618/5784831) already helps. BTW: I would also prefer e.g. `dput(your_data[1:10, ]`... – Christoph Apr 26 '17 at 18:56
  • Updated to include section of `dput`. – thepinkfreudian Apr 26 '17 at 19:07
  • Realized my subset of `dput` doesn't include the `lng` and `lat` variables. The dataset as a whole plots fine, it's when I try to pass it through the reactive expression that something gets lost, hence what led me to believe the problem is in how I've used `filter` or how I've written the expression in general. – thepinkfreudian Apr 26 '17 at 19:29

1 Answers1

0

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.