Antonio
Antonio

Reputation: 1111

How to adjust daterange in Shiny

The code below works normally. A table with information from my database called Testis displayed after choosing the start and end date of the daterange. So far OK. Notice that the table is not shown automatically when I run the APP, because I have the following: req(input$daterange1) in data_subset . However, if I change my Test database instead of having 01/10 and 02/10 and it has 01/11 and 02/11, when I run the APP, the table is automatically generated, do you know how to adjust this? That is, adjust so that daterange is empty too.

Executable code below:

library(shiny)
library(shinythemes)

Test <- structure(list(date2 = as.Date(c("2021-07-30","2021-07-30","2021-10-01","2021-10-02")), 
                       Category = c("FDE", "ABC", "FDE", "ABC"), 
                       coef = c(4, 1, 6, 1)), class = "data.frame",row.names = c(NA, -4L))


ui <- fluidPage(
  
  shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                    br(),
                    tabPanel("",
                             sidebarLayout(
                               sidebarPanel(
                                 uiOutput('daterange'),
                                 br()
                                 
                               ),
                               mainPanel(
                                 dataTableOutput('table')

                               )
                             ))
  ))

server <- function(input, output,session) {
  
  data <- reactive(Test)
  
  output$daterange <- renderUI({
    dateRangeInput("daterange1", "Period you want to see:",
                   min   = min(data()$date2),
                   max   = max(data()$date2))
  })
  
  
  
  data_subset <- reactive({
    req(input$daterange1)
    req(input$daterange1[1] <= input$daterange1[2])
    days <- seq(input$daterange1[1], input$daterange1[2], by = 'day')
    subset(data(), date2 %in% days)
  })
  
 
  output$table <- renderDataTable({
    data_subset()
  })
  
}

shinyApp(ui = ui, server = server)

As it is, it works, I need to insert the start and end dates in daterange to generate the table.

enter image description here

If I use the database below:

Test <- structure(list(date2 = as.Date(c("2021-07-30","2021-07-30","2021-11-01","2021-11-02")), 
                       Category = c("FDE", "ABC", "FDE", "ABC"), 
                       coef = c(4, 1, 6, 1)), class = "data.frame",row.names = c(NA, -4L))

Notice that I only changed the days 01/10 and 02/10 to 01/11 and 02/11. When I run the code, the table is automatically generated as you can see below.

enter image description here

Upvotes: 0

Views: 86

Answers (1)

YBS
YBS

Reputation: 21297

That is because you are not using start and end in dateRangeInput(). In this case both start and end are NULL. If your dataframe has current date (when you change 10/2 to 11/2), then both start and end get current date, hence you are noticing 11/1/21 as both start and end, and a table is displayed with current date info. My suggestion is that you should use start and end with NA in updateDateRangeInput(). Try this

library(shiny)
library(shinythemes)

Test <- structure(list(date2 = as.Date(c("2021-07-30","2021-07-30","2021-11-01","2021-11-02")),
                       Category = c("FDE", "ABC", "FDE", "ABC"),
                       coef = c(4, 1, 6, 1)), class = "data.frame",row.names = c(NA, -4L))

ui <- fluidPage(

  shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                    br(),
                    tabPanel("",
                             sidebarLayout(
                               sidebarPanel(
                                 uiOutput('daterange'),
                                 br()

                               ),
                               mainPanel(
                                 dataTableOutput('table')

                               )
                             ))
  ))

server <- function(input, output,session) {

  data <- reactive(Test)

  output$daterange <- renderUI({
    req(data())
    dateRangeInput("daterange1", "Period you want to see:",
                   min   = min(data()$date2),
                   max   = max(data()$date2)
                   )
  })
  
  observe({
    updateDateRangeInput(session, inputId = "daterange1", start = NA, end = NA)
  })

  data_subset <- reactive({
    req(input$daterange1)
    req(input$daterange1[1] <= input$daterange1[2])
    days <- seq(input$daterange1[1], input$daterange1[2], by = 'day')
    subset(data(), date2 %in% days)
  })

  output$table <- renderDataTable({
    data_subset()
  })

}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions