【问题标题】:ReactiveValues trigger different observeEvents not workingReactiveValues 触发不同的 observeEvents 不工作
【发布时间】:2020-12-04 11:47:03
【问题描述】:

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

方法我尝试使用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)
      
    })
  }
)

【问题讨论】:

    标签: r shiny observers shiny-reactivity


    【解决方案1】:

    我认为它只是你需要在server 中的session,如:

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

    【讨论】:

      【解决方案2】:

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

      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)
            
          })
        }
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多