Antex
Antex

Reputation: 1454

shiny leaflet data not found

For a shiny app that will render a leaflet map I am using the "Crime" dataset found in the 'ggmap' package. There are two Inputs am trying to implement. One is the 'offense', the other is the 'month' variables. These variables are available as drop down from shiny. The objective is to filter and display by the combination of the offense type and month. Ex. robbery in April for example...

When excuted, I get the Point data not found; error.

Here is a code construct to extract data.

fData <- reactive({
  data <- hustonCrime

           if (input$offenseFilter != "All"){
                  data <- subset(hustonCrime, offense %in% input$offenseFilter) 
                  }
           if (input$monthFilter != "All"){
                   data <- subset(hustonCrime, month %in% input$monthFilter) 
                 }
   })

Upvotes: 1

Views: 162

Answers (1)

tblznbits
tblznbits

Reputation: 6778

To fix the bad filtering situation you have to switch your code to the following:

fData <- reactive({
  data <- hustonCrime
  if (input$offenseFilter != "All") {
    data <- subset(data, offense %in% input$offenseFilter)
  }
  if (input$monthFilter != "All") {
    data <- subset(data, month %in% input$monthFilter)
  }
  return(data)
})

The problem in your code is that you kept resetting data to be a subset of hustonCrime, when in reality, you wanted to keep subsetting data.

Edit

As indicated in the comments, this code cumulatively filters the data as opposed to the code in the original question. To see this, let's walk through what happens with each code as inputs are changed in the Shiny app.

First, let's not change anything at all. Both inputs are set to "All".

Original Code:
Shiny loads the app and fData is created since it's a reactive element. Shiny steps into the reactive function and sets data equal to hustonCrime. It then checks input$offenseFilter and input$monthFilter and sees that they both equal "All" and it makes no changes to data. It then returns data.

New Code:
The exact same thing happens as in the Original Code.

Next, let's see what happens when the use changes input$offenseFilter.

Original Code:
Shiny recognizes that input$offenseFilter has changed and finds all reactive elements that depend on that variable. It finds fData and steps into the code. It sets data equal to hustonCrime and then checks if input$offenseFilter is equal to "All". It finds that it is not and it subsets hustonCrime such that offense is a value in the vector of elements in input$offenseFilter. It next checks if input$monthFilter is equal to "All" and finds that it is, so it makes no changes. The function returns data, which is set to the filtered hustonCrime.

New Code:
Shiny recognizes that input$offenseFilter has changed and finds all reactive elements that depend on that variable. It finds fData and steps into the code. It sets data equal to hustonCrime and then checks if input$offenseFilter is equal to "All". It finds that it is not and it subsets data, NOT hustonCrime, which will be important in the next step, and moves on. It acknowledges that input$monthFilter is equal to "All" and returns data, which is a subset of the original data.

Now, let's look at the last step where the user also choose a month filter.

Original Code:
Shiny steps into the fData reactive function and sets data equal to hustonCrime. It then overwrites data with the subset of hustonCrime where offense is in input$offenseFilter. Next, it sees that input$monthFilter does not equal "All" and it again overwrites data with a new subset of hustonCrime where month is in the vector of elements in input$monthFilter. Since data has just been overwritten with a subset of hustonCrime that only depended on month, it no longer includes the filter by offense.

New Code:
Shiny steps into the function, sets data equal to hustonCrime and then overwrites data with a subset of itself, not the hustonCrime data set. Next, it finds that input$monthFilter is not equal to "All" and again overwrites data with a subset of itself. This is important because prior to this step, data was filtered by offense while hustonCrime was not. Since data is already filtered by offense, filtering it by month now gives us the interaction we're looking for in the filtering.

In the end, the original code keeps overwriting data with subsets of the whole data set, when in reality we want to keep subsetting our working dataset to include all filters that have been selected.

Upvotes: 5

Related Questions