Show invalid dates not working under Validate?

I have a server file where in if I select input$Date[1] > input$Date[2] the notifications should pop up telling invalid date selected. I tried with the below code

output$table <- DT:renderDT({
datatable(data()),

validate(
need(input$Date[1] > input$Date[2], "Invalid date selected")
)
})

I am not the getting the output. May I know what wrong I am doing

r

input$Date is a character vector, not a Date vector, so to make that kind of comparisons you have to convert it first.

As usual, if you need specific help, please provide a reprex, not just a code snippet.

Hi Andre,

Re-editing mu code after thorough research (It is almost working).

But actually some minor issue is going on. If you closely observe the application. Like open the application > Select start date as Feb 27, 2020 > Select AAA/BBB > Submit > Table Disappears . And the table will not appear even if select proper dates in future. Make sense?

I have edited my code for you . Can you please help me in this?

ui.R


library(shiny)
library(shinydashboard)
library(dplyr)
library(DT)
library(lubridate)
source("df.R")

ui <- dashboardPage(
  dashboardHeader(disable = TRUE),
  dashboardSidebar(disable = TRUE),
  dashboardBody(
    dateRangeInput("Date", "", start = Sys.Date(), end = Sys.Date(), format = "yyyy-mm-dd", separator = "-"),
    selectInput("Tic","",choices = c("ALL",as.character(df$ID)),selected = "ALL"),
    actionButton("Submit","Submit"),
    dataTableOutput("table")
  )
)

server.R

source("df.R")
library(dplyr)

server <- function(input, output,session) { 
  df1 <- df %>% filter(Date1 == Sys.Date())
  data_1 <- reactiveVal(df1)



observeEvent(input$Submit,{
  if (input$Tic == "ALL"){
    table_display <- df %>% filter(Date1 %in% input$Date[1] : input$Date[2])
  }
  
  else if (input$Tic == "ALL" && input$Date[1] == Sys.Date() && input$Date[2] == Sys.Date())
   { 
    table_display <- df %>% filter(Date1 == Sys.Date())
   }
  
  else if (input$Tic != "ALL" && input$Date[1] == Sys.Date() && input$Date[2] == Sys.Date())
  {
    table_display <- df %>% filter(ID %in% input$Tic & Date1 == Sys.Date())
  }
  
 else 
  {
    table_display <- df %>% filter(ID %in% input$Tic & Date1 %in% input$Date[1] : input$Date[2])
  }
  
  data_1(table_display)
})

observeEvent(input$Submit,{
  if (input$Date[[1]] > input$Date[[2]]) {
    showNotification("Invalid dates")
    }
})


# output$Inv <- renderText({
#   if (input$Date[[1]] > input$Date[[2]]) {
#     print("Invalid dates")
#   }
# })

output$table <- DT::renderDataTable({
  #req(input$Date[[1]] <= input$Date[[2]])
  req(isolate(input$Date[[1]]) <= isolate(input$Date[[2]]))
  
  datatable(data_1())
})

}

df.R

df <- structure(list(Date = structure(c(1541662915.921, 1541562949.500, 
                                        1541662949.845, 1541662949.845, 1541662993.957, 1541662915.921, 
                                        1541662949.842, 1541662949.845, 1541662949.845, 1541662993.957
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Date1 = structure(c(17843, 
                                                                       17843, 17844, 17843, 18843, 17843, 18300, 17843, 17843, 18300
), class = "Date"), ID = structure(c(1L, 1L, 1L, 1L, 1L, 
                                     1L, 1L, 1L, 2L, 2L), .Label = c("AAA", "BBB"), class = "factor"), 
Value = c(68, 70175, 71107, 702175, 72638, 7238, 739, 738469, 
          75901, 7106), Status = structure(c(1L, 1L, 2L, 2L, 1L, 2L, 
                                             1L, 1L, 2L, 1L), .Label = c("Approved", "Pending"), class = "factor")), row.names = c(NA, 
                                                                                                                                   -10L), class = "data.frame")
df$Date <- NULL
df$Status <- NULL

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.