【问题标题】:Asynchronous Programming in R with the Future Package使用 Future 包在 R 中进行异步编程
【发布时间】:2019-06-21 13:16:58
【问题描述】:

我是使用 Future 包在 R 中进行异步编程的新手,因此需要一些帮助。我正在尝试使用支持异步编程的 rshiny 构建一个简单的应用程序。所以我的代码作为一个直方图、一个滑块、一个简单的文本打印和 read.csv 函数来读取一个大的 CSV 文件。所以我的计划是在我的 read.csv 函数使用 R 中的 future 包在后台运行之前,我想控制我的其他应用程序。

但我的代码等待读取 CSV 文件。任何帮助将不胜感激。代码示例如下。

library(promises)
library(future)
library(shinydashboard)
library(shiny)
library(tidyverse)
plan(multiprocess)

#UI parts
ui <- dashboardBody(fluidRow(box(tableOutput("input1")),
                             box(textOutput("input2"))),

                    fluidRow(box(
                      sliderInput(
                        inputId = "bins",
                        label = "Number of bins:",
                        min = 1,
                        max = 5,
                        value = 2
                      )
                    ),
                    box(plotOutput(outputId = "distPlot"))),


                    fluidRow(box(
                      sliderInput(
                        "slider2",
                        label = h3("Slider Range"),
                        min = 0,
                        max = 100,
                        value = c(40, 60)
                      )
                    ),
                    box(verbatimTextOutput("range"))))

#server part
server <- function(input, output, session) {
  output$input1 <- renderTable({
    promise <- future((read.csv("data/sample_large.csv")))
    promise %...>% head() %...>% print()
  })

  output$input2 <- renderText({
    print("hello")
  })
  output$distPlot <- renderPlot({
    dist <- rnorm(input$bins)
    hist(dist)
  })
  output$range <- renderPrint({
    input$slider2
  })

}
shinyApp(ui = dashboardPage(dashboardHeader(),
                            dashboardSidebar(),
                            ui),
         server = server)

【问题讨论】:

  • 看看data.table::fread,它可以并行读取文本文件(IMO,这是加载csv文件的最佳方式)
  • fread 是我通常使用的,但如果仍然很慢,vroom 值得一试。很有希望github.com/r-lib/vroom
  • 感谢您的回复,但我计划使用 promise 和 future 进行异步编程

标签: r asynchronous promise future


【解决方案1】:

在评估 Promise 之前未加载其余 UI 的行为是预期行为。它在 promises 包中作为他们所谓的“闪亮刷新周期”的一部分进行了解释,并在herehere 进行了更详细的描述。

只有在所有输出都完成执行后,才会将它们发送回 Shiny 以更新 UI。您可能希望/更喜欢输出准备好后立即渲染,但不幸的是,这不是 Shiny 的操作方式。

如第二个链接中所述,您可以“欺骗”shiny 认为所有输出都已执行,然后在 promise 评估后使用反应值触发最终更新:

#server part
server <- function(input, output, session) {

  data <- reactiveVal()

# Return NULL from this operation so Shiny 'thinks' the output is evaluated
  observe({
    data(NULL)
    future({read.csv("data/sample_large.csv")}) %...>%
      data() #Assign to data
    NULL
  })

# When data() is updated as a side effect of our promise the table will be updated
  output$input1 <- renderTable({
    req(data()) %>%
      head(5) %>%
      print()
  })

# in the mean time all outputs will be judged to be complete so can be rendered
  output$input2 <- renderText({
    print("hello")
  })
  output$distPlot <- renderPlot({
    dist <- rnorm(input$bins)
    hist(dist)
  })
  output$range <- renderPrint({
    input$slider2
  })

}
shinyApp(ui = dashboardPage(dashboardHeader(),
                            dashboardSidebar(),
                            ui),
         server = server)

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 2011-01-19
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多