我有一个简单的闪亮的应用程序,我想防止保存和导出数据的应用程序的用户,如果他们在“项目名称”列中输入太多的字符时,修改rhandsontable。我发现了一种方法,使字母红色作为警告,你是输入太多的字符,但我正在努力寻找一种方法,实际上防止用户保存数据。这是我的证明,我不能弄清楚。一如既往,任何帮助都将受到赞赏。
library(shiny)
library(rhandsontable)
library(tibble)
project_data <- tibble(
"Project Name" = c("Project A", "Project B", "Project C"),
"Project Cost" = c(10000, 15000, 20000),
"Project Status" = c("In Progress", "Completed", "In Progress"),
"Project Start Date" = as.Date(c("2023-01-15", "2022-05-10", "2023-03-20"))
)
modified_data <- reactiveVal(project_data)
ui <- fluidPage(
titlePanel("Project Data"),
fluidRow(
column(12,
rHandsontableOutput("table")
)
),
fluidRow(
column(12,
actionButton("saveButton", "Save")
)
)
)
server <- function(input, output, session) {
modified_data <- reactiveVal(project_data)
output$table <- renderRHandsontable({
rhandsontable(modified_data()) %>%
hot_col("Project Name", allowInvalid = FALSE, renderer = "
function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
if (value.length > 20) {
td.style.color = 'red';
}
}
")
})
observeEvent(input$saveButton, {
invalid_values <- modified_data()$`Project Name`[nchar(modified_data()$`Project Name`) > 20]
if (length(invalid_values) == 0) {
saved_data <- hot_to_r(input$table)
write.csv(saved_data, file = "saved_project_data.csv", row.names = FALSE)
showModal(
modalDialog(
title = "Success",
"Data has been saved to 'saved_project_data.csv'.",
easyClose = TRUE
)
)
} else {
showModal(
modalDialog(
title = "Warning",
"DATA HAS NOT BEEN SAVED",
easyClose = TRUE
)
)
}
})
}
shinyApp(ui, server)
1条答案
按热度按时间mklgxw1f1#
你需要一个
validator
在这里。假设你想让用户只在project name
列中输入最大长度为10的字符串,那么下面的代码将阻止将更长的字符值保存到单元格中,并在用户尝试这样做的情况下打印错误消息。