在Shiny中,等待用户停止用rhandsontable填充表格

时间:2018-07-25 13:17:47

标签: r shiny handsontable rhandsontable

考虑到用户用手填写rhandsontable,我想实现一个与时间相关的条件,以便进行表格分析和绘图。 例如,如果在最近2秒钟内未向表中添加任何内容,请继续操作,否则请等待2秒钟。

我尝试使用validate()或简单条件(如下所示)。它不起作用,因为修改表后立即访问了observe(),当时的时间相关条件是false。当条件应为true时,将不再访问observe()函数,因此将不对条件进行测试...

我试图提供MRE,但是在一个简单的示例中,我很难满足对此类功能的需求。需求与分析和绘图的计算时间有关。

library(shiny)
library(rhandsontable)
library(ggplot2)


DF <- data.frame(x=integer(0), y=integer(0))

ui <- shinyUI(fluidPage(
  mainPanel( 
    rHandsontableOutput("hot"),
    plotOutput("plot1")
  )
))


server <- shinyServer(function(input, output) {

  values <- reactiveValues()
  values$table <- DF
  values$accessDF <- 0

  observe({
    if (!is.null(input$hot)) {
      DF <- hot_to_r(input$hot)
      values$accessDF <- Sys.time() # reset awaiting time when table is incremented
    } else {
      if (is.null(values[["DF"]]))
        DF <- DF
      else
        DF <- values[["DF"]]
    }
    values[["DF"]] <- DF
  })

  output$hot <- renderRHandsontable({
    rhandsontable(values[["DF"]], stretchH = "all", minRows=5)
  })

  observe({

    if (Sys.time() - values$accessDF > 2){ # unfornate try...
      # some modification of the table occuring here
      values$table <- values$DF
    }

  })

  output$plot1 <- renderPlot({

    ggplot(data=values$table) + geom_line(aes(x=x, y=y))

  })

})

shinyApp(ui=ui, server=server)

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案。使用reactiveTimer()强制observe()激活,即使它观察到的变量没有更新。

server:

autoInvalidate <- reactiveTimer(200) # to activate observer every 200 ms

,然后在observe()

autoInvalidate()

遵循条件

if (Sys.time() - values$accessDF > 2){ # unfornate try...
  # some modification of the table occuring here
  values$table <- values$DF
}

请参阅https://shiny.rstudio.com/reference/shiny/1.0.0/reactiveTimer.html