如何将闪亮的进度条与 llply 进度条同步

how to synchronize a shiny progress bar with a llply progress bar

我想找到一种方法来在闪亮的 UI 中显示 llply 进度条。 请查看下面的代码。你有什么想法吗?

library(shiny)
library(plyr)
function_I_cant_edit <- function(){plyr::llply(LETTERS ,.fun=function(x){Sys.sleep(0.2)},.progress = "text")}

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


  observeEvent(input$go, {

    progress <- shiny::Progress$new(session, min=1, max=15)
    on.exit(progress$close())
    progress$set(message = 'Calculation in progress')
    function_I_cant_edit()

    for (i in 1:15) {
      progress$set(value = i)
      Sys.sleep(0.1)
    }

  })

  output$plot <- renderPlot({
    plot(cars)
  })
})

ui <- basicPage(
  actionButton("go","PUSH ME"),
  plotOutput("plot")

)
shinyApp(ui = ui, server = server)

一个想法是在 llply 中使用 progress="tk",但是有没有最性感的方法?

另一个想法是在闪亮的应用程序中显示控制台输出...但我没有做到这一点。

此致

编辑:

llpy函数使用progress_tk()或progress_text()或progress_time()

所以我创建了一个 progress_shiny() 函数

    progress_shiny <-function (title = "plyr progress", label = "Working...", ...) 
{
  n <- 0
  tk <- NULL
  list(init = function(x) {
    tk    <<- shiny::Progress$new(session,min=1, max=15)

    tk$set(message = 'Calculation in progress')
  }, step = function() {
    n <<- n + 1
    tk$set(value = n)
  }, term = function() print("fin"))
}

我试过了:

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

  # session <<- session
  observeEvent(input$go, {


    # function_I_cant_edit()
    llply(LETTERS ,.fun=function(x){Sys.sleep(0.2)},.progress = "shiny")


  })

  output$plot <- renderPlot({
    plot(cars)
  })
})

ui <- basicPage(
  actionButton("go","PUSH ME"),
  plotOutput("plot")

)
shinyApp(ui = ui, server = server)

但错误消息是“public_bind_env$initialize(...) 中的错误:objet 'session' introuvable”...

我想我正在寻找东西的路上;)

您可以创建一个自定义进度处理程序,它从 shiny 中获取一个 progress 对象,例如

progress_shiny <-function (progress, step = 1){
  list(
    init = function(n){},
    step = function() {
      progress$set( progress$getValue() + step )
    }, 
    term = function(){}
  )
}

并在您的服务器代码中像这样使用它。

observeEvent(input$go, {
  progress <- shiny::Progress$new(session, min=0, max=50)
  on.exit(progress$close())

  # use the main progress outside of llply 
  progress$set( value = 1)
  Sys.sleep( 1 )
  progress$set( value = 20 )

  # then pass it along so that llply steps 
  # contribute to the main progress
  llply(LETTERS ,.fun=function(x){
    Sys.sleep(0.2)
  }, .progress = progress_shiny(progress))

})

这样llply里面的进度条就可以贡献到主进度条

code from the plyr package之后,您也可以将进度对话的初始化和终止也放入函数中。这与一些合理的默认值一起构成了一个相当干净的调用。

progress_shiny <- function(session, min=0, value=min, step=1, message="Working...") {
  p<-NULL
  list(
    init = function(max) {
      p<<-shiny::Progress$new(session, min=min, max=max)
      p$set(value=value, message=message)
    },
    step = function() {
      p$inc(step)
    },
    term = function(){
      p$close()  
    }
  )
}

使用时间变得更短:

observeEvent(input$go, {
  # no additional setup needed
  llply(LETTERS ,.fun=function(x){
    Sys.sleep(0.2)
  }, .progress = progress_shiny(session))
})