Shiny eventReactive 需要两次失效

Shiny eventReactive requiring two invalidations

有许多带有 eventReactive 的 Shiny 应用程序示例,其中多个输入会在 任一 输入无效时响应。但是,我正在尝试创建一个带有 eventReactive 的应用程序,该应用程序仅在应用程序启动时运行,并且仅在更新 Val1 textInput 并按下 actionButton 时运行。下面是我的应用程序的精简版。

我在事件部分尝试了花括号、c() 方法和 !is.null(input$Val) & !is.null(input$Run),但都没有成功(对于后者 - 因为我假设 inputText 在所有情况下都返回一个非 NULL 值,但我无法弄清楚它是什么 returns 无效时...)。

非常感谢!

library(shiny)
library(shinydashboard)

server <- function(input, output, session){
    dat <- eventReactive(input$Val1, { 
            data.frame(x = input$Val1, y = 1)
            }, ignoreNULL=FALSE) 

    output$table <- renderTable({ 
        dat()
        })} 

sidebar <- dashboardSidebar(
                textInput(inputId = "Val1", label = "Val1", value = "5"),
                textInput(inputId = "Val2", label = "Val2", value = "51"),
                actionButton("Run", "Run", class = "btn-success")) 

ui <- dashboardPage(header = dashboardHeader(), 
    sidebar = sidebar,
    body = dashboardBody(mainPanel(fluidRow(tableOutput("table")))))

shinyApp(ui, server) 

这里有一个解决方案,将Val1的当前值与eventReactive的最后一次触发时的Val1的值进行比较。我没有直接在 eventReactive 中进行比较(并为此使用 req),因为在这种情况下它被触发但 Val1 没有改变,dat不会显示任何内容(并且还会丢失最后显示的值)。

library(shiny)
library(shinydashboard)

server <- function(input, output, session){
  comparison_values <- reactiveValues(trigger_event = 0,
                                      previous_input = "value_on_startup")
  
  observeEvent(input$Run, {
    if (input$Val1 != comparison_values$previous_input) {
      comparison_values$trigger_event <- comparison_values$trigger_event + 1
      comparison_values$previous_input <- input$Val1
    }
  })
  
  dat <- eventReactive(comparison_values$trigger_event, {
    data.frame(x = input$Val1, y = 1)
  }, ignoreNULL=FALSE) 
  
  output$table <- renderTable({ 
    dat()
  })} 

sidebar <- dashboardSidebar(
  textInput(inputId = "Val1", label = "Val1", value = "5"),
  textInput(inputId = "Val2", label = "Val2", value = "51"),
  actionButton("Run", "Run", class = "btn-success")) 

ui <- dashboardPage(header = dashboardHeader(), 
                    sidebar = sidebar,
                    body = dashboardBody(mainPanel(fluidRow(tableOutput("table")))))

shinyApp(ui, server)