使用带有pmap的进度条实现可进入CRAN软件包的功能

使用带有pmap的进度条实现可进入CRAN软件包的功能,r,purrr,r-package,cran,R,Purrr,R Package,Cran,我正在尝试编写一个函数,它将下载不同的向量,并将它们组合起来返回一个数据帧。我在purrr包中使用pmap()循环遍历每个向量。我想从进度包中添加一个进度条,向最终用户显示数据帧的进度。这可以通过使用进度包Readme.md文件中的来实现。如果Superassignment首先在父环境中查找具有其分配给的符号的对象,并且仅在未找到新对象时才在全局环境中创建新对象,则Superassignment不保证分配到全局环境中,这是一种回退。阅读帮助(“避免使用不确定我是否完全遵循…你是否建议我遵循进度中

我正在尝试编写一个函数,它将下载不同的向量,并将它们组合起来返回一个数据帧。我在purrr包中使用
pmap()
循环遍历每个向量。我想从进度包中添加一个进度条,向最终用户显示数据帧的进度。这可以通过使用进度包Readme.md文件中的
来实现。如果Superassignment首先在父环境中查找具有其分配给的符号的对象,并且仅在未找到新对象时才在全局环境中创建新对象,则Superassignment不保证分配到全局环境中,这是一种回退。阅读
帮助(“避免使用
不确定我是否完全遵循…你是否建议我遵循
进度中使用的内容\u progress
?…这就是我建议使用

library(tidyverse)
library(progress)

f_one_col <- function(x, m, s){
  d <- tibble(i = rnorm(n = 5, mean = m, sd = s))
  names(d) <- x
  # slow things down to check if the progress bar is appearing
  Sys.sleep(1)
  pb$tick()
  return(d)
} 

f <- function(d){
  pb <<- progress_bar$new(total = nrow(d))
  pb$tick(0)
  d$col <- pmap(.l = d, .f = f_one_col)
  pb$terminate() 
  rm(pb, envir = .GlobalEnv)
  return(bind_cols(d$col))
}

d0 <- tibble(
  x = c("a", "b", "c"),
  m = c(10, 20, 30),
  s = c(5, 200, 1000)
)
f(d = d0)
# # A tibble: 5 x 3
#        a     b      c
#    <dbl> <dbl>  <dbl>
# 1  6.50   70.8 -1071.
# 2  3.51  -52.0  -542.
# 3  3.76  369.   -351.
# 4 11.4   171.   1745.
# 5  0.421 111.   1886.
no visible binding for '<<-' assignment to 'pb'
f <- function(d){
  pb <- progress_bar$new(total = nrow(d))
  pb$tick(0)
  pmap_dfc(
    .l = d, 
    .f = function(x, m, s){
      d <- tibble(i = rnorm(n = 5, mean = m, sd = s))
      names(d) <- x
      pb$tick()
      Sys.sleep(0.5)
      return(d)
    })
}