Search code examples
rprogress-barplyrmapply

Progress bar and mapply (input as list)


I would like to monitor the progress of my mapply function. The data consists of 2 lists and there is a function with 2 arguments.

If I do something similar with a function that takes 1 arguments I can use ldply instead of lapply. (I'd like to rbind.fill the output to a data.frame)

If I want to do the same with mdply it doesn't work as the function in mdply wants values taken from columns of a data frame or array. Mapply takes lists as input.

These plyr apply functions are handy, not just because I can get the output as a data.frame but also because I can use the progress bar.

I know there is the pbapply package but that there is no mapply version and there is the txtProgressBar function but I could not figure out how to use this with mapply.

I tried to create a reproducible example (takes around 30 s to run)

I guess bad example. My l1 is a list of scraped websites (rvest::read_html) which I cannot send as a data frame to mdply. The lists really need to be lists.

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 does't work

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

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

Solution

  • Answering my own question. There is now a function called pbmapply in pbapply that adds progress bars to mapply.