Home > database >  Correct way of referring to dataframe columns while filtering dataframe in reactive object
Correct way of referring to dataframe columns while filtering dataframe in reactive object

Time:11-17

I am new to Shiny. In my server, I am slicing my dataframe according to the value fo a dropdown menu and a time slider:


ui <- fluidPage(
  titlePanel("Title"),
  tags$head(tags$style('.selectize-dropdown {z-index: 10000}')),
  sliderInput("date", "Date", min=as.Date(min(master$date)), max=as.Date(max(master$date)), value=as.Date(min(master$date))),
  selectInput("select", "Determinand", choices = c("item1","item2","item3"),
  leafletOutput("mymap")
)

server <- function(input, output, session) {
  
  # Filter data according to determinand and time
  master <- reactive({
    master %>% {filter(.$determinand == input$select & .$date == input$date)}
  })

  # A leaflet map is rendered here

}

The master data look like this:

   notation label lat   long date                 determinand value unit
0  a        site1 12.10 3.5  2000-01-11T12:45:00  item1       17    km/h
1  b        site2 12.14 3.6  2000-01-11T12:45:00  item2       11    kg
2  c        site3 12.11 3.7  2000-01-11T12:45:00  item1       5     km/h
3  d        site4 12.15 3.8  2000-01-11T12:45:00  item2       86    kg

I have been playing with the data filtering function inside server. If I write it as:

  # Filter data according to determinand and time
  master <- reactive({
    master %>% {filter(.$determinand == input$select & .$date == input$date)}
  })

then I get: Warning: Error in $: object of type 'closure' is not subsettable

If I write it as:

  # Filter data according to determinand and time
  master <- reactive({
    master %>% {filter(determinand == input$select & date == input$date)}
  })

I get: Error in filter: object 'determinand' not found.

How should I refer to the dataframe columns in the reactive function?

CodePudding user response:

I just removed the {} in the filter function and it works

# Filter data according to determinand and time
  data <- reactive({
    
    master %>% 
      filter(determinand == input$select, date == input$date)
    
  })
  • Related