【问题标题】:how to synchronize a shiny progress bar with a llply progress bar如何将闪亮的进度条与 llply 进度条同步
【发布时间】:2016-09-09 22:29:52
【问题描述】:

我想找到一种在闪亮的 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”...

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

【问题讨论】:

  • plyr 的进度条只是打印到控制台的文本,可以用capture.output 捕获,但我认为您无法从@ 获取进度条987654326@ 到闪亮的用户界面。
  • Thks,我知道,但是 capture.output 不会是实时的..

标签: r shiny plyr


【解决方案1】:

您可以创建一个自定义进度处理程序,该处理程序从闪亮中获取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 中的进度条会影响主进度条

【讨论】:

    【解决方案2】:

    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))
    })
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      相关资源
      最近更新 更多