并行使用标准R发光进度条进行每次计算 [英] Using standard R shiny progress bar in parallel foreach calculations

查看:54
本文介绍了并行使用标准R发光进度条进行每次计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用doParallel后端在并行的foreach循环中使用标准R闪亮进度条.但是,这会导致以下错误消息:

I am trying to use the standard R shiny progress bar in a parallel foreach loop using the doParallel back-end. However, this results in the following error message:

警告:{:任务1中的错误失败-会话"不是ShinySession对象."

Warning: Error in {: task 1 failed - "'session' is not a ShinySession object."

代码(最小工作示例)

library(shiny)
library(doParallel)

ui <- fluidPage(
  actionButton(inputId = "go", label = "Launch calculation")
)

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

  workers=makeCluster(2)
  registerDoParallel(workers)

  observeEvent(input$go, {
    Runs=c(1:4)
    Test_out=foreach(i=Runs, .combine=cbind, .inorder=TRUE, .packages=c("shiny"),.export=c("session")) %dopar% { 
      pbShiny = shiny::Progress$new()
      pbShiny <- Progress$new(session,min = 0, max = 10)
      on.exit(pbShiny$close())
      test_vec=rep(0,100)

      for(i in 1:10){
        test_vec=test_vec+rnorm(100)
        pbShiny$set(message="Simulating",detail=paste(i),
                  value=i)
        Sys.sleep(0.2)
      }

    }
  })
}

shinyApp(ui = ui, server = server)

如果我依次运行foreach循环(使用registerDoSEQ()),则代码将运行.有人知道如何解决此问题吗?

The code runs if I run the foreach loop sequentially (using registerDoSEQ()). Does anyone know how to resolve this issue?

  • 使用doParallel在并行的foreach循环中向用户显示进度闪亮的后端
  • 用户应注意工人的数量和每个工人的进度和/或整体进度

以下链接下有一个类似的问题,但由于未提供有效的示例,因此未得到解决:

There is a similar question under the following link, but it didn't get resolved as no working example was provided:

将并行foreach用于进度条R闪亮

推荐答案

doParallel软件包是并行软件包的扩展,如此处的文档所示.

The doParallel package is an extension of the parallel package as shown in the documentation here.

https://cran.r-project.org/web/packages/doParallel/doParallel.pdf

阅读并行程序包的文档,我们看到它实现了3种不同的方法来实现并行性.请记住,R是一种单线程语言.

Reading the parallel package's documentation we see that it implements 3 different methods to achieve parallelism. Keep in mind R is a single threaded language.

  1. 父进程与工作进程或子进程进行通信的新R会话.
  2. 通过分叉
  3. 使用操作系统级别的工具

您可以在此处找到此信息,

You can find this information here,

https://stat.ethz.ch/R-manual/R-devel/library/parallel/doc/parallel.pdf

其结果是子进程直到完成计算并返回值才可以与父进程通信.据我所知.

A consequence of this is that the child process cannot communicate with the parent process until it completes its computation and returns a value. This is to the best of my knowledge.

因此,将无法在worker进程中勾选进度条.

Hence, ticking the progress bar within the worker process will not be possible.

完全公开,我没有使用doParallel软件包,有关光泽的文档也很有限.

Full disclosure, I have not worked with the doParallel package and the documentation with respect to shiny was limited.

也有一个类似的软件包,其中包含有关光泽的大量文档.这些是 futures promises ipc 软件包. futures promises 启用异步编程,而 ipc 启用进程间通信.为了进一步帮助我们,它还提供了一个 AsyncProgress()函数.

There is a similar package however with extensive documentation with respect to shiny. These are the futures and promises and ipc packages. futures and promises enable asynchronous programming while ipc enables interprocess communication. To help us even more it also has an AsyncProgress() function.

这里是一个示例,其中我们同步标记两个计数器.

Here is an example where we tick two counters synchronously.

library(shiny)
library(future)
library(promises)
library(ipc)

plan(multisession)


ui <- fluidPage(
  actionButton(inputId = "go", label = "Launch calculation")
)

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

  observeEvent(input$go, {

    progress = AsyncProgress$new(message="Complex analysis")

    future({
      for (i in 1:15) {
        progress$inc(1/15)
        Sys.sleep(0.5)
      }

      progress$close()
      return(i)
    })%...>%
      cat(.,"\n")

    Sys.sleep(1)

    progress2 = AsyncProgress$new(message="Complex analysis")

    future({
      for (i in 1:5) {
        progress2$inc(1/5)
        Sys.sleep(0.5)
      }

      progress2$close()

      return(i)
    })%...>%
      cat(.,"\n")

    NULL
  })
}

shinyApp(ui = ui, server = server)

您的代码已修改

这是您编写的代码,经过略微修改以衍生出许多异步过程.可以在工作程序中执行任何工作,例如您创建的向量以及添加 rnorm 的向量.(此处未显示)

Your code adapted

Here is the code you have written, slightly modified to spin off many asynchronous processes. Any work can be performed in the worker, such as the vector you create and add an rnorm too. (Not shown here)

library(shiny)
library(future)
library(promises)
library(ipc)

plan(multisession)

ui <- fluidPage(
  actionButton(inputId = "go", label = "Launch calculation")
)

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

  observeEvent(input$go, {
    Runs=c(1:4) #define the number of runs
    progress = list() #A list to maintain progress for each run

    for(j in Runs){
      progress[[j]] = AsyncProgress$new(message="Complex analysis")
      future({
        for (i in 1:10) {
          progress[[j]]$inc(1/10)
          Sys.sleep(0.2)
        }
        progress[[j]]$close()
        return(i)
    })%...>%
        cat(.,'\n')
    }

    NULL
  })
}

shinyApp(ui = ui, server = server)

上面的代码是在ipc文档中找到的代码的修改版本:

The code above is a modified version of the code found in the ipc documentation here:

http://htmlpreview.github.io/?https://github.com/fellstat/ipc/blob/master/inst/doc/shinymp.html

其他资源:

https://rstudio.github.io/promises/articles/overview.html

这篇关于并行使用标准R发光进度条进行每次计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆