Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:进度条不是从0开始的_R_Progress Bar - Fatal编程技术网

R:进度条不是从0开始的

R:进度条不是从0开始的,r,progress-bar,R,Progress Bar,我试图用R的进度库编写进度条。在所有的努力中,酒吧的起价超过百分之十 我使用的是完全相同的示例代码,并且拥有包1.2.0的当前版本。我在RStudio的Anaconda发行版1.0.153版中运行此代码 library(progress) pb <- progress_bar$new(total = 100) for (i in 1:100) { pb$tick() Sys.sleep(1 / 100) } 进度条应该从0开始,但应该从20%开始。它在100%时正确完成。TL;D

我试图用R的进度库编写进度条。在所有的努力中,酒吧的起价超过百分之十

我使用的是完全相同的示例代码,并且拥有包1.2.0的当前版本。我在RStudio的Anaconda发行版1.0.153版中运行此代码

library(progress)
pb <- progress_bar$new(total = 100)
for (i in 1:100) {
  pb$tick()
  Sys.sleep(1 / 100)
}
进度条应该从0开始,但应该从20%开始。它在100%时正确完成。

TL;DR:添加pb$ticklen=0;系统0.5;pb$ticklen=0,在您创建进度条之后。你可能需要调整睡眠时间。这是令人不满意的骇客,但工作

这更好地说明了问题:

f <- function(wait = 1, total = 5) {
  pb <- progress::progress_bar$new(total = total)
  pb$tick(len = 0) # doesn't seem to work

  for (i in 1:total) {
    print(i)
    pb$tick(len = 1)
    Sys.sleep(wait)
  }
}
但如果我运行f1/10,我会得到:

f1/100的结果是:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
因此,进度条必须有某种启动时间。在我的电脑上大约140毫秒,试试f1/1000200。但是,即使我将等待设置为f5这样的长时间,我仍然没有得到第一次显示

如果我在pb$ticklen=0之后而不是之前添加了一个预等待时间,那么只要预等待时间~>200毫秒,我就会得到第一个勾号:

f <- function(wait = 1, total = 5, pre = 1) {
  pb <- progress::progress_bar$new(total = total)
  pb$tick(len = 0) # doesn't display
  Sys.sleep(pre)   # pre-waiting time
  pb$tick(len=0)   # now it displays the 0% bar
  
  for (i in 1:total) {
    print(i)
    pb$tick()
    Sys.sleep(wait)
  }
}

你有一个非常短的时间间隔,控制台只会更新这么多次每秒。如果你把睡眠时间增加到1/10左右,你还看到这个问题吗?是的。滴答声之间的短时间应该无关紧要,因为第一次滴答声从~12-20%开始,然后调用Sys.sleep。当pb$tick是循环中的第一个方法调用时,会发生什么情况导致打印12-20%的数据?在那之后,所有其他的东西都是均匀分布的。
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
f <- function(wait = 1, total = 5, pre = 1) {
  pb <- progress::progress_bar$new(total = total)
  pb$tick(len = 0) # doesn't display
  Sys.sleep(pre)   # pre-waiting time
  pb$tick(len=0)   # now it displays the 0% bar
  
  for (i in 1:total) {
    print(i)
    pb$tick()
    Sys.sleep(wait)
  }
}
> f(1/100, 5, 0.3)
[======>---------------------------]   0%[1] 1
[======>---------------------------]  20%[1] 2
[=============>--------------------]  40%[1] 3
[===================>--------------]  60%[1] 4
[==========================>-------]  80%[1] 5