向功能添加进度条

编程入门 行业动态 更新时间:2024-10-09 17:28:02
本文介绍了向功能添加进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我看了几个问题,但是找不到不使用循环的解决方案.我在此处和此处.现在,我如何添加与此功能完全相同的进度条:

I've looked at a couple of questions but couldn't find a solution that doesn't use loops. I've looked here and here. Now, how could I add a progress bar that works exactly at the same time as this function:

prog<-function(){ print("This is a test") Sys.sleep(05) setTxtProgressBar() }

上面是一个虚拟函数,我想到了使用 system.time 来捕获执行打印命令所花费的时间,并将其用于进度条.我如何在不使用for循环的情况下完成这项工作?编辑我试过了,但是还是很慢:

The above is a dummy function and I thought of using system.time to capture the time it takes for the print command to execute and use this for the progress bar. How could I make this work, without using a for loop? EDIT I tried this but it is still slow:

prog<-function(y=sort(runif(200)),...){ pb<-txtProgressBar(...) values<-c(0,y,1) lapply(values, function(x){ Sys.sleep(0.5) setTxtProgressBar(pb,x)}) Sys.sleep(1) close(pb) }

谢谢.

推荐答案

有一个软件包 pbapply ,它为使用以下功能的应用功能提供了进度条:

There is a package pbapply, which provides a progress bar for apply-functions using:

pblapply(X, FUN, ..., cl = NULL)

它的工作原理类似于普通的 apply 功能.

It works just like a normal apply-function.

此函数: pblapply(1:10,function(x){Sys.sleep(02); print(x)})给出了这样的输出:

This function: pblapply(1:10, function(x) {Sys.sleep(02); print(x)}) gave this output:

| | 0 % ~calculating [1] 1 |+++++ | 10% ~18s [1] 2 |++++++++++ | 20% ~16s [1] 3 |+++++++++++++++ | 30% ~14s [1] 4 |++++++++++++++++++++ | 40% ~12s [1] 5 |+++++++++++++++++++++++++ | 50% ~10s [1] 6 |++++++++++++++++++++++++++++++ | 60% ~08s [1] 7 |+++++++++++++++++++++++++++++++++++ | 70% ~06s [1] 8 |++++++++++++++++++++++++++++++++++++++++ | 80% ~04s [1] 9 |+++++++++++++++++++++++++++++++++++++++++++++ | 90% ~02s [1] 10 |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed = 20s

非常整洁.我不知道是否有帮助,但是值得一看.

Pretty neat. I don't know if it helps, but worth taking a look.

更多推荐

向功能添加进度条

本文发布于:2023-11-26 09:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633550.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度条   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!