Subset a dataframe using dateInput values as range in shiny


#1

Hello I have a simple shiny app below. Here is the df:

location = c("100 ail","16th and Whitmore","40AB01 - ANTWERPEN","100 ail","16th and Whitmore","40AB01 - ANTWERPEN") 
date = c("2015-09-01 00:00:00","2016-03-06 19:00:00","2016-11-22 15:00:00","2018-02-01 09:30:00", "2018-02-01 03:00:00", "2017-03-07 10:00:00") 
pm25=c("FALSE","FALSE","FALSE","FALSE","FALSE","FALSE")
pm10=c("TRUE","FALSE","FALSE","TRUE","FALSE","FALSE")
no2=c("TRUE","FALSE","FALSE")
latitude=c(47.932907,41.322470,36.809700,47.932907,41.322470,36.809700)
longitude=c(106.92139000,-95.93799000
            ,-107.65170000,106.92139000,-95.93799000
            ,-107.65170000)

df = data.frame(location, date,latitude,longitude,pm25,pm10,no2)

and the app:

ui = fluidPage(

  uiOutput("dt"),
  uiOutput("dt2"),
  submitButton(text = "Submit", icon = NULL, width = NULL),
  shiny::dataTableOutput("merged") 
)

#server.r

#df$location <- gsub( " " , "+" , df$location)
server = function(input, output, session) {







  output$dt<-renderUI({

    dateInput('date',
              label = 'First Available Date',
              value = df$date
    )           


  })
  output$dt2<-renderUI({

    dateInput('date2',
              label = 'Last available Date',
              value = df$date
    )            


  })
  output$merged <- shiny::renderDataTable({
    df %>%
      filter(date >= input$dt & date <= input$dt2)
  })
}

shinyApp(ui = ui, server = server)

The df I want to display as datatable should take as inputs tha dates provided by the 2 selectInput() as range and changes its appearance everytime they are updated. Can this be done? Or dateInput() just displays the data that it takes from the beginning and cannot be used for subsetting?


#2

You can use dateInput as any other input in Shiny. I would recommend dateRangeInput for your case though. My first question is why are you putting the dateInput into a renderUI function call instead of putting dateInput directly in the ui function itself?

For example, the below fixes your issue:

location = c("100 ail","16th and Whitmore","40AB01 - ANTWERPEN","100 ail","16th and Whitmore","40AB01 - ANTWERPEN") 
date = c("2015-09-01 00:00:00","2016-03-06 19:00:00","2016-11-22 15:00:00","2018-02-01 09:30:00", "2018-02-01 03:00:00", "2017-03-07 10:00:00") 
pm25=c("FALSE","FALSE","FALSE","FALSE","FALSE","FALSE")
pm10=c("TRUE","FALSE","FALSE","TRUE","FALSE","FALSE")
no2=c("TRUE","FALSE","FALSE")
latitude=c(47.932907,41.322470,36.809700,47.932907,41.322470,36.809700)
longitude=c(106.92139000,-95.93799000
            ,-107.65170000,106.92139000,-95.93799000
            ,-107.65170000)

df = data.frame(location, date = as.Date(date),
                latitude,longitude,pm25,pm10,no2)
library(dplyr)
library(shiny)

ui = fluidPage(
    
    dateRangeInput("dateRange", 
                   "Select date range:",
                   start = '01/01/2013'),
    submitButton(text = "Submit", icon = NULL, width = NULL),
    dataTableOutput("merged") 
)

#server.r
server = function(input, output, session) {
    
    output$merged <- renderDataTable({
        df %>%
            filter(date >= input$dateRange[1] & 
                       date <= input$dateRange[2])
    })
}

shinyApp(ui = ui, server = server)