有人可以告诉我我的代码有什么问题吗?
library(shiny)
library(rhandsontable)
library(data.table)
library(shinyalert)
DF <- data.table(Date = as.character(Sys.Date()), col1 = as.numeric(c(1, 2, 3)))
ui <- fluidPage(
useShinyalert(),
rHandsontableOutput("table")
)
server <- function(input, output, session) {
data <- reactiveValues()
observe({
data$df <- as.data.table(DF)
})
observeEvent(input$table, {
if (!is.null(input$table)) {
df <- hot_to_r(input$table)
# Check if the Date column is in a valid format
valid_dates <- !is.na(as.Date(df$Date, format = "%Y-%m-%d"))
if (any(!valid_dates)) {
shinyalert("Invalid Date", "Please enter a valid date format (YYYY-MM-DD).", type = "error")
}
}
})
output$table <- renderRHandsontable({
rhandsontable(data$df) |>
hot_col(1, dateFormat = "YYYY-MM-DD", type = "date")
})
}
shinyApp(ui, server)
observeEvent(input$table, {
if (!is.null(input$table)) {
df <- hot_to_r(input$table)
# print(any(is.na(as.character(df$Date))))
if (any(is.na(as.character(as.Date(df$Date))))) {
shinyalert("Invalid Date", "Please enter a valid date format.", type = "error")
}
}
})