进度条和mapply(将输入作为列表)

3
我想监控mapply函数的进度。这个数据由两个列表和一个带有两个参数的函数组成。
如果我使用一个只需要1个参数的函数,我可以使用ldply而不是lapply。(我想把输出rbind.fill到data.frame中)
如果我想用mdply做同样的事情,它不起作用,因为mdply中的函数需要从数据框或数组的列中取值。Mapply采用列表作为输入。
这些plyr应用程序函数非常方便,不仅因为我可以将输出作为data.frame获取,还因为我可以使用进度条。
我知道有pbapply包,但没有mapply版本,也有txtProgressBar函数,但我无法弄清如何将其与mapply一起使用。
我试图创建一个可重现的示例(运行时间约为30秒)
我猜这是个糟糕的例子。我的l1是一个被爬网站的列表(rvest::read_html),我不能将其发送为数据框到mdply。这些列表确实需要是列表。
mdply <- plyr::mdply

l1 <- as.list(rep("a", 2*10^6+1))
l2 <- as.list(rnorm(-10^6:10^6))

my_func <- function(x, y) {

ab <- paste(x, "b", sep = "_")
ab2 <- paste0(ab, exp(y), sep = "__")

return(ab2)

}

mapply(my_func, x = l1, y = l2)

mdply无法工作

mdply(l1, l2, my_func, .progress='text')

Error in do.call(flat, c(args, list(...))) : 'what' must be a function or character string
2个回答

2

回答我自己的问题。现在有一个名为pbmapply的函数在pbapply中,它可以将进度条添加到mapply。

最初的回答。现在,在pbapply中有一个名为pbmapply的函数,它可以给mapply添加进度条。


2

?mdply 这里看来,您不能指定两个数据输入。您的错误信息意味着 mdply 尝试将 l2 作为函数使用,但是列表无法强制转换为函数...

以下内容可以正常工作

mdply(
    data.frame(x=unlist(l1), y=unlist(l2)), # create a data.frame from l1 and l2
    my_func, # your function
    .progress=plyr::progress_text(style = 3) # create a textual progress bar
)[, 3] # keep the output only

我现在明白你的意图了:

我认为我已经理解了你的目的:

mdply(
    .data=data.frame(r=1:length(l1)), # "fake data" (I will use them as item index)
    .fun=function(r) return(my_func(l1[[r]], l2[[r]])), # a wrapper function of your function
    .progress=plyr::progress_text(style = 3) # create a textual progress bar
)[, 2] # keep the output only

请注意,我不得不用一个新函数包装您的函数,该函数只考虑一个参数,并使用该参数来访问l1l2


谢谢。问题是实际上l1是我使用rvest::read_html爬取的网页列表。我无法将此列表用作data.frame中的列。我猜这个例子不太好。 - Roccer
谢谢你的帮助。函数可以运行,但输出结果不是我想要的/不同于mapply给出的结果。我会稍后接受你的答案,因为你通过示例解决了我的问题。 - Roccer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接