I'm having a little problem with the code below. If you run the code, you'll notice it has a daterange
. Regardless of the days you choose in daterange
, the same values will appear in the table, but I would like it to show the appropriate value for the selected date, that is, if you choose the date from 01/11 to 01/11, it would only have to show the value corresponding to 01/11, which in this case is Week equal to Monday, Category ABC and mean equal to 3.
I believe it is necessary to make some adjustments in data_subset
.
Example:
As it is, it is showing the result for all the dates I have in my database, but I would like it to show only for the dates chosen in the daterange
. In this case, I would have to show only the first line, since I chose 01/11 to 01/11.
Code below:
library(shiny)
library(shinythemes)
library(dplyr)
Test <- structure(list(date1 = as.Date(c("2021-10-30","2021-10-30","2021-10-30","2021-10-30")),
date2 = as.Date(c("2021-10-30","2021-10-31","2021-10-31","2021-11-01")),
Week = c("Saturday", "Sunday", "Sunday", "Monday"),
Category = c("FDE", "ABC", "FDE", "ABC"),
time = c(4, 6, 6, 3)), class = "data.frame",row.names = c(NA, -4L))
ui <- fluidPage(
shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
br(),
tabPanel("",
sidebarLayout(
sidebarPanel(
uiOutput('daterange')
),
mainPanel(
dataTableOutput('table')
)
))
))
server <- function(input, output,session) {
data <- reactive(Test)
output$daterange <- renderUI({
dateRangeInput("daterange1", "Period you want to see:",
min = min(data()$date1),
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')
Test <- subset(data())
x<-Test%>%
group_by(Week,Category)%>%
summarize(mean(time))
})
output$table <- renderDataTable({
data_subset()
})
}
shinyApp(ui = ui, server = server)