The code below works normally. A table with information from my database called Test
is 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 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, 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)