for循环中的状态栏

Status Bar in for loop

我试图在我的 for 循环中创建一个状态栏,但状态仍然为 0%。我不确定是什么问题。

  n <- length(unique(mu_ut$AnimalID))
  pb <- txtProgressBar(min = 0, max = n, style = 3)
  for(i in unique(mu_ut$AnimalID)){ 
     AnID <- paste("AnID", i, sep = ".")
     assign(AnID, mu_ut[mu_ut$AnimalID == i,])
     dbWriteTable(conn = db, name = AnID, value = mu_ut[mu_ut$AnimalID == i,], 
                  field.types = c(DateAndTime = "DATETIME", 
                                  AnimalID = "CHAR",
                                  Species = "CHAR",
                                  Sex = "CHAR",
                                  CurrentCohort = "CHAR",
                                  BirthYear = "DATE",
                                  CaptureUnit = "CHAR",
                                  CaptureSubunit = "CHAR",
                                  CaptureArea = "CHAR"), overwrite = TRUE)
  Sys.sleep(1)
  setTxtProgressBar(pb, i)
  }

是不是setTxtProgressBar函数中的“i”参数有问题?

提前致谢。

问题是 setTxtProgressBar 的值需要按照 txtProgressBar 中设置的顺序 min=0max=n。由于i确实是mu_ut$AnimalID范围内的唯一值之一,即使是整数也不清楚也是范围内的0:n.

试试这个。

  ids <- unique(mu_ut$AnimalID)
  pb <- txtProgressBar(min = 0, max = length(ids), style = 3)
  for (ind in seq_along(ids)) { 
     id <- ids[ind]
     AnID <- paste("AnID", id, sep = ".")
     assign(AnID, mu_ut[mu_ut$AnimalID == id,])
     dbWriteTable(conn = db, name = AnID, value = mu_ut[mu_ut$AnimalID == id,], 
                  field.types = c(DateAndTime = "DATETIME", 
                                  AnimalID = "CHAR",
                                  Species = "CHAR",
                                  Sex = "CHAR",
                                  CurrentCohort = "CHAR",
                                  BirthYear = "DATE",
                                  CaptureUnit = "CHAR",
                                  CaptureSubunit = "CHAR",
                                  CaptureArea = "CHAR"), overwrite = TRUE)
  Sys.sleep(1)
  setTxtProgressBar(pb, ind)
  }

(这与@CareyCaginalp 的评论非常相似。)