使用 sweetalertR 保存前确认消息
Confirm message before saving using sweetalertR
我使用 sweetalertR 包在 UI 中创建一条确认消息(使用这个包的原因:它看起来非常好,消息是高度可定制的)。
但是,我需要在服务器函数中实现一些代码,以便只有在确认消息已被批准时才将文件保存到磁盘。目前它已经在点击保存按钮时保存了文件。
问题:sweetalert 函数似乎没有 inputID 参数!
library(shiny) #1.5.0
library(shinydashboard) #0.7.1
library(rhandsontable) #0.3.7
# remotes::install_github("timelyportfolio/sweetalertR")
library(sweetalertR) #0.2.0
library("xlsx") #0.6.5
shinyApp(
ui = fluidPage(
box(width = 12,
# Save button
actionButton(inputId = "saveBtn", "Save"),
br(),
# Editable table
rHandsontableOutput("submit_data_edit_table"),
# Confirmation button, see: http://www.buildingwidgets.com/blog/2015/6/29/week-25-sweetalertr
sweetalert(selector = "#saveBtn", text = 'Changes will be saved in a new excel file',
title = 'Confirm changes',
type = "warning",
allowOutsideClick = TRUE,
showCancelButton = TRUE,
cancelButtonText = 'Cancel',
confirmButtonText = 'Confirm',
confirmButtonColor = "darkred",
closeOnConfirm = FALSE,
evalFunction = 'function(){swal("Saved", "Restart the application", "success")}'
)
)
),
server = function(input, output, session) {
# Create a table that can be modified
output$submit_data_edit_table = renderRHandsontable({
if (!is.null(input$submit_data_edit_table)) {
DF = hot_to_r(input$submit_data_edit_table)
} else {
DF = iris
}
rhandsontable(DF,
height = 750
) %>%
hot_table(highlightCol = TRUE, highlightRow = TRUE)
})
# Save file based on button press
observe({
# Save button
input$saveBtn
submit_data_edit_table = isolate(input$submit_data_edit_table)
if (!is.null(submit_data_edit_table)) {
# Save table as Excel file
write.xlsx(hot_to_r(input$submit_data_edit_table), file = "newData.xlsx",
sheetName = "Tot",
col.names = TRUE, row.names = FALSE,
append = FALSE)
}
})
}
)
我找到了一个解决方案:改用 shinyalert。它提供相同的功能和设计,但有更好的文档记录。
我在服务器函数中包含了以下代码:
# Create global variable for confirmation message response
global <- reactiveValues(response = FALSE)
# Update file after table change
observeEvent(input$saveBtn, {
# Trigger confirmation dialog
shinyalert(title = "Confirm changes",
text = "Changes will be saved in a new excel file",
type = "warning",
closeOnClickOutside = TRUE,
showCancelButton = TRUE,
cancelButtonText = 'Cancel',
showConfirmButton = TRUE,
confirmButtonText = 'Confirm',
confirmButtonCol = "darkred",
timer = 15000, # 15 seconds
callbackR = function(x) {
global$response <- x
shinyalert(title = "Saved",
text = "Restart the application",
type = "success")
}
)
print(global$response)
observe({
req(input$shinyalert)
if (!global$response == "FALSE") {
submit_data_edit_table = isolate(input$submit_data_edit_table)
if (!is.null(submit_data_edit_table)) {
# Save as Excel file
write.xlsx(hot_to_r(input$submit_data_edit_table), file = "newData.xlsx",
sheetName = "Tot",
col.names = TRUE, row.names = FALSE,
append = FALSE)
}
# Reset value
global$response <- "FALSE"
} # End of confirmation button if
}) # End of observe
}) # End of observeEvent
而在 UI 中,您只需设置 shinyalert:
# Set up shinyalert to create confirmation messages
useShinyalert(),
别忘了先加载包!
library(shinyalert)
我使用 sweetalertR 包在 UI 中创建一条确认消息(使用这个包的原因:它看起来非常好,消息是高度可定制的)。 但是,我需要在服务器函数中实现一些代码,以便只有在确认消息已被批准时才将文件保存到磁盘。目前它已经在点击保存按钮时保存了文件。
问题:sweetalert 函数似乎没有 inputID 参数!
library(shiny) #1.5.0
library(shinydashboard) #0.7.1
library(rhandsontable) #0.3.7
# remotes::install_github("timelyportfolio/sweetalertR")
library(sweetalertR) #0.2.0
library("xlsx") #0.6.5
shinyApp(
ui = fluidPage(
box(width = 12,
# Save button
actionButton(inputId = "saveBtn", "Save"),
br(),
# Editable table
rHandsontableOutput("submit_data_edit_table"),
# Confirmation button, see: http://www.buildingwidgets.com/blog/2015/6/29/week-25-sweetalertr
sweetalert(selector = "#saveBtn", text = 'Changes will be saved in a new excel file',
title = 'Confirm changes',
type = "warning",
allowOutsideClick = TRUE,
showCancelButton = TRUE,
cancelButtonText = 'Cancel',
confirmButtonText = 'Confirm',
confirmButtonColor = "darkred",
closeOnConfirm = FALSE,
evalFunction = 'function(){swal("Saved", "Restart the application", "success")}'
)
)
),
server = function(input, output, session) {
# Create a table that can be modified
output$submit_data_edit_table = renderRHandsontable({
if (!is.null(input$submit_data_edit_table)) {
DF = hot_to_r(input$submit_data_edit_table)
} else {
DF = iris
}
rhandsontable(DF,
height = 750
) %>%
hot_table(highlightCol = TRUE, highlightRow = TRUE)
})
# Save file based on button press
observe({
# Save button
input$saveBtn
submit_data_edit_table = isolate(input$submit_data_edit_table)
if (!is.null(submit_data_edit_table)) {
# Save table as Excel file
write.xlsx(hot_to_r(input$submit_data_edit_table), file = "newData.xlsx",
sheetName = "Tot",
col.names = TRUE, row.names = FALSE,
append = FALSE)
}
})
}
)
我找到了一个解决方案:改用 shinyalert。它提供相同的功能和设计,但有更好的文档记录。
我在服务器函数中包含了以下代码:
# Create global variable for confirmation message response
global <- reactiveValues(response = FALSE)
# Update file after table change
observeEvent(input$saveBtn, {
# Trigger confirmation dialog
shinyalert(title = "Confirm changes",
text = "Changes will be saved in a new excel file",
type = "warning",
closeOnClickOutside = TRUE,
showCancelButton = TRUE,
cancelButtonText = 'Cancel',
showConfirmButton = TRUE,
confirmButtonText = 'Confirm',
confirmButtonCol = "darkred",
timer = 15000, # 15 seconds
callbackR = function(x) {
global$response <- x
shinyalert(title = "Saved",
text = "Restart the application",
type = "success")
}
)
print(global$response)
observe({
req(input$shinyalert)
if (!global$response == "FALSE") {
submit_data_edit_table = isolate(input$submit_data_edit_table)
if (!is.null(submit_data_edit_table)) {
# Save as Excel file
write.xlsx(hot_to_r(input$submit_data_edit_table), file = "newData.xlsx",
sheetName = "Tot",
col.names = TRUE, row.names = FALSE,
append = FALSE)
}
# Reset value
global$response <- "FALSE"
} # End of confirmation button if
}) # End of observe
}) # End of observeEvent
而在 UI 中,您只需设置 shinyalert:
# Set up shinyalert to create confirmation messages
useShinyalert(),
别忘了先加载包!
library(shinyalert)