我正在尝试编写一个函数来下载不同的向量并将它们组合起来以返回一个数据框。我正在使用 pmap()
在 purrr 包中循环遍历每个向量。我想从进度包中添加一个进度条,以向最终用户显示数据框的进度。这可以使用 <<-
按照包装中的建议 Read Me ...
代码的简化版本:
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.
但是当我将函数放入我的 R 包时,我在构建检查中遇到了错误...
no visible binding for '<<-' assignment to 'pb'
根据我目前阅读的内容,使用 <<-
似乎是 CRAN 的禁忌。有没有其他方法可以使用进度条而不必使用 <<-
或允许 <<-
的 hacky 方式在包裹里?
最佳答案
避免必须使用 <<-
通过将辅助函数 ( f_one_col()
) 直接放在 pmap()
中功能。
... 并使用 pmap_dfc()
而不是 pmap()
和 bind_cols()
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)
})
}
关于r - 将进度条与 pmap 一起用于可以在 CRAN 上打包的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64728854/