如何解决ReactiveValues触发不同的watchEvents不起作用
问题:我有以下应用。本质上,我想按下按钮来加载数据。第一次我通过按钮加载数据后,我想询问我是否要保存更改。如果是,则确认更改已成功保存,否则显示其他数据(不包括其他数据)。
方法我试图通过observeEvent
触发的reactiveValues
表达式来解决它。但是,正如您在运行以下脚本时所观察到的那样,此操作无法按预期进行。
问题:对什么地方有误吗?
library(shiny)
library(shinyWidgets)
library(rhandsontable)
shinyApp(
ui = fluidPage(
actionButton("show","Show data",width = "100%"),rHandsontableOutput("data_table")
),server = function(input,output) {
rv <- reactiveValues(
# Triggers
pressed_first_time = 0,confirm_module = TRUE,save_module = TRUE,table_change = TRUE
)
observeEvent(input$show,ignoreInit = TRUE,{
if (rv$pressed_first_time == 0){
rv$pressed_first_time <- isolate(rv$pressed_first_time + 1)
rv$table_change <- isolate(!rv$table_change)
cat("pressed_first time")
} else {
rv$pressed_first_time <- isolate(rv$pressed_first_time + 1)
rv$confirm_module <- isolate(!rv$confirm_module)
}
})
observeEvent(rv$confirm_module,{
confirmSweetAlert(
session = session,inputId = session$ns("show_confirmation"),title = "Be careful,your changes might be lost",text = "Do you want to save your changes?",type = "question",btn_labels = c("Cancel","Save"),btn_colors = NULL,closeOnClickOutside = FALSE,showCloseButton = FALSE,html = FALSE
)
cat("confirmation module")
rv$save_module <- isolate(!rv$save_module)
})
observeEvent(rv$save_module,{
if (isTRUE(input$show_confirmation)) {
sendSweetAlert(
session = session,title = "Saved",text = "Updated data has been successfully saved",type = "success"
)
rv$table_change <- isolate(!rv$table_change)
cat("saving module")
} else {
return()
}
})
data_to_modify <- eventReactive(rv$table_change,{
mtcars
})
handson_df <- eventReactive(rv$table_change,{
cat("create handsons")
req(data_to_modify())
rhandsontable(data_to_modify())
})
output$data_table <- renderRHandsontable({
cat("plot module")
req(handson_df())
htmlwidgets::onRender(handson_df(),change_hook)
})
}
)
解决方法
我认为您只是需要session
中的server
,如下所示:
server = function(input,output,session) {...
实际上,我发现了问题所在。从data_to_modify到handson_df的链接丢失。在下面的解决方案中,我将它们放在一起,但是原则上从data_to_modify添加另一个reactValue触发handson_df也会起作用
KeyType
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。