是否可以在生成时更新并输出闪亮的表?

Joh*_*nyB 5 r shiny

我目前正在开发一个闪亮的应用程序,当有人按下“计算”时,它会动态地对数据集运行一系列计算。数据集非常大,并且通过 a 进行大量计算lapply,这允许用户使用进度条跟踪进度。

这意味着即使有潜在结果已经坐在那里等待显示,输出数据帧的生成也可能非常慢。我遇到的问题是,当发现某些内容时,数据可能对时间非常敏感,因此,如果计算需要运行 15 分钟,则第一个计算中可能会显示一些超出 15 分钟的内容。日期。

有没有一种方法可以在每次迭代之后lapply(或者随意建议另一种方法),应用程序可以查看那里是否有数据并立即显示它,本质上是在每次迭代后刷新输出表?本质上是在观察期间而不是之后更新反应值。

我在下面放置了一个简短的示例应用程序,可以帮助可视化我遇到的问题:

library(shiny)

testDF <- data.frame(number = c(1:10),
                     letter = letters[1:10])

ui <- fluidPage(
    # UI Input
    numericInput(inputId = "attemptDivide", label = "Number to divide by",
                 value = 1, min = 1, max = 10),

    actionButton(inputId = "calculate", label = "Calculate"),

    # UI Output
    dataTableOutput("dividedTable")

)

# Define server logic
server <- function(input, output) {

    # Create a bucket for results
    results <- reactiveVal()

    # Observe when "Calculate" is pushed and we should work out whether the
    # number is perfectly divisible by the input given
    observeEvent(input$calculate, {

        divisibleDF <- lapply(testDF$number, function(x) {

            # Set to sleep for 1 second to simulate for the the lapply being
            # bigger and taking more time than this example
            Sys.sleep(1)

            # Find the row we're interested in
            interest <- subset(testDF, number == x)

            # Find whether the number is perfectly divisible by the one given
            divisible <- (x / input$attemptDivide) %% 1 == 0

            # If divisible is TRUE we keep, else we return an empty data frame
            if (divisible) {
                return(interest)
            } else {
                return(data.frame(number = integer(), letter = character()))
            }

        }) %>%
            do.call(rbind, .)

        # Save the results to bucket
        results(divisibleDF)

    })

    # Create the table
    output$dividedTable <- renderDataTable({
        results()
    })

}

# Run the app
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

预先感谢您的任何帮助。