R shiny:observeEvent 和 eventReactive 的不同行为

R shiny: different behaviours of observeEvent and eventReactive

下面闪亮的应用程序显示了一个用 rhandsontable().

构建的 editable table

问题:你能解释一下为什么每次编辑数据 table 时控制台都会打印 "ping""pong" 从不打印。

library(shiny)

ui <- fluidPage(
  rhandsontable::rHandsontableOutput(
    outputId = "data")
)

server <- function(input, output, session) {
  
  data <- data.frame(a = 1, b = 2, c = 3)
  
  output$data <- rhandsontable::renderRHandsontable({
    rhandsontable::rhandsontable(
      selectCallback = TRUE,
      data = data)
  })
  
  observeEvent(input$data$changes$changes, {
    print("ping")
  })
  
  edits <- eventReactive(input$data$changes$changes, {
    print("pong")
  })
  
}

shinyApp(ui = ui, server = server)

因为edits()之后没有被调用,所以shiny认为你不需要它,因此没有理由做任何工作,你需要添加它应该去的地方或者您想用它做什么:

library(shiny)

ui <- fluidPage(
    rhandsontable::rHandsontableOutput(
        outputId = "data")
)

server <- function(input, output, session) {
    
    data <- data.frame(a = 1, b = 2, c = 3)
    
    output$data <- rhandsontable::renderRHandsontable({
        rhandsontable::rhandsontable(
            selectCallback = TRUE,
            data = data)
    })
    
    observeEvent(input$data$changes$changes, {
        print("ping")
    })
    
    edits <- eventReactive(input$data$changes$changes, {
        print("pong")
    })
    
    observe({
        edits()
    })
    
}

shinyApp(ui = ui, server = server)