ReactiveValues 触发不同的 observeEvents 不起作用

ReactiveValues trigger different observeEvents not working

问题: 我有以下应用程序。本质上,我想按下按钮来加载数据。第一次通过按下按钮加载数据后,我想询问是否要保存更改。如果是,确认更改已成功保存,否则显示一些其他数据(其他数据不包括在内)。

方法 我试图用 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, ignoreInit = TRUE,{
      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, ignoreInit = TRUE, {
      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, ignoreInit = TRUE, {
      mtcars
    })
    
    handson_df <- eventReactive(rv$table_change, ignoreInit = TRUE, {
      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)
      
    })
  }
)

我认为 server 中只需要 session,如:

server = function(input, output, session) {...

其实,我发现了问题所在。从 data_to_modify 到 handson_df 的 link 丢失了。在下面的解决方案中,我将它们放在一起,但原则上从 data_to_modify 添加另一个 reactiveValue 触发 handson_df 也将起作用

library(shiny)
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 <- 1
        rv$table_change <- isolate(!rv$table_change)
        cat("pressed_first time")
      } else {
        rv$pressed_first_time <- 1
        rv$confirm_module <- isolate(!rv$confirm_module)
      }
    })
    
    observeEvent(rv$confirm_module, ignoreInit = TRUE,{
      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
      )
      
    })
    
    observeEvent(input$show_confirmation, ignoreInit = TRUE, {
      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, ignoreInit = TRUE, {

      rhandsontable(mtcars)
    })
    
    # handson_df <- eventReactive(rv$table_change, ignoreInit = TRUE, {
    #   cat("create handsons")
    #   req(data_to_modify())
    #   rhandsontable(data_to_modify())
    # })
    
    
    output$data_table <- renderRHandsontable({
      cat("plot module")
      req(data_to_modify())
      data_to_modify()
      # htmlwidgets::onRender(handson_df(),change_hook)
      
    })
  }
)