【问题标题】:In Shiny, await for the user to stop filling a table with rhandsontable在 Shiny 中,等待用户停止使用 rhandsontable 填充表格
【发布时间】:2019-01-02 07:57:12
【问题描述】:

考虑到用户手动填写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)

【问题讨论】:

    标签: r shiny handsontable rhandsontable


    【解决方案1】:

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

    server:

    autoInvalidate &lt;- 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

    【讨论】:

      猜你喜欢
      • 2018-05-18
      • 1970-01-01
      • 2018-02-08
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      相关资源
      最近更新 更多