Could help me with two small problems in the code below?
The first thing is that I'm not able to show the information in the table that I generate. The idea is to show the information corresponding to my Test
database, but it doesn't appear. The second thing is that when I run the APP, the table is already generated, however this is strange, since in data_subset
I insert req(input$daterange1)
.
Executable code below:
library(shiny)
library(shinythemes)
Test <- structure(list(date2 = c("2021-06-30","2021-06-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({
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)
CodePudding user response:
In date2 %in% days
, the class of days
is Date
while the class of date2
is character
.
I suggest to modify first you data.frame to define your dates as actual dates:
Test <- structure(list(date2 = as.Date(c("2021-06-30","2021-06-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))
and then to simply compare your dates.
data_subset <- reactive({
req(input$daterange1)
req(input$daterange1[1] <= input$daterange1[2])
subset(data(), date2 >= input$daterange1[1] & date2 <= input$daterange1[2])
)}
As for your second point, dateRangeInput
provides, as a default the current date in the client's time zone... Hence, it is not NULL. You can alternatively add a button.