R cli进度条在purrr::map内部

huangapple go评论70阅读模式
英文:

R cli progress bar within purrr::map

问题

I need to present a progress bar that shows the progress of a purrr::pmap.
I use the cli package for user-facing information generally, and there is a progress bar in there that seems proficient. However, I cannot get access to a created progress bar inside of another function.

library(cli)
library(purrr)

a <- 1:1000
p <- cli::cli_progress_bar(total = length(a))
te <- function(x){
  cli::cli_progress_update(id=p)
  return(1/x)
}

cli_progress_done(id=p)

a %>% purrr::map(te)

It seems that the id argument is not used? Is there a way to access the progress bar inside of the inner function so that I can update the progress?

Thanks!

Fredrik

英文:

I need to present a progress bar that show the progress of a purrr::pmap.
I use the cli package for user facing information generally, and there is a progress bar in there that seems proficient. However, I cannot get access to a created progress bar inside of another function.

library(cli)
library(purrr)

a &lt;- 1:1000
p &lt;- cli::cli_progress_bar(total = length(a))
te &lt;- function(x){
  cli::cli_progress_update(id=p)
  return(1/x)
}

cli_progress_done(id=p)

a |&gt; purrr::map(te)

It seems that the id argument is not used? Is there a way to access the progress bar inside of the inner function so that I can update the progress?

Thanks!

Fredrik

答案1

得分: 3

你可以在 purrr::map() 中使用 .progress 参数(它基于 cli):

a <- 1:1000
map(a, function(x){Sys.sleep(1/100); (1/x)}, .progress = list(
  type = "iterator", 
  format = "Calculating {cli::pb_bar} {cli::pb_percent}",
  clear = TRUE))

R cli进度条在purrr::map内部

英文:

You could use the .progress argument (its based on cli) in purrr::map():

a &lt;- 1:1000
map(a, function(x){Sys.sleep(1/100); (1/x)}, .progress = list(
  type = &quot;iterator&quot;, 
  format = &quot;Calculating {cli::pb_bar} {cli::pb_percent}&quot;,
  clear = TRUE))

R cli进度条在purrr::map内部

huangapple
  • 本文由 发表于 2023年2月13日 22:53:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定