@eddelbuettel’s idea is a good one. (it’s a quick read…jump there and come back). We’ll avoid confusion and call it R⁶ over here. Feel free to don the superclass.
I often wait for a complete example or new package announcement to blog something when a briefly explained snippet might have sufficient utility for many R users. Also, tweets are fleeting and twitter could end up on the island of misfit social media sites if it can’t generate revenue or find a giant buyer this year. Don’t get me wrong, twitter convos are fine/useful, but blogs are at least semi-permanent, especially if you let them be hoovered up by the Internet Archive (“Save Page Now” on their site or use this handy Chrome extension).
I’ll tag all R⁶ posts as “r6” if you want to auto-filter those out of your stream or just page through them.
I’ll also lead off the these micro-posts with a simple one: adding progress bars to your tidyverse purrr
operations.
The purrr::map*
functions enable expressive and type-safe vectorized operations. Mine are usually over a few million/billion IPv4 addresses or domain names/URLs and often involve moderately lengthy tasks so I usually add the ability to incorporate progress bars to functions I make (and, I’m trying hard to get out of the bad habit of long-ish anonymous functions in purrr
calls). The following is a toy example, but it’s a working example you can run in your interactive R session now:
library(tidyverse)
arduously_long_nchar <- function(input_var, .pb=NULL) {
if ((!is.null(.pb)) && inherits(.pb, "Progress") && (.pb$i < .pb$n)) .pb$tick()$print()
Sys.sleep(1)
nchar(input_var)
}
pb <- progress_estimated(length(letters))
map_int(letters, arduously_long_nchar, .pb=pb)
And, yes, I did make you wait ~26 seconds (unless you were intrepid enough to reduce the amount of sleep time :-)
If you happen to forget the progress bar object (or know you don’t need one):
map_int(letters, arduously_long_nchar)
the function still works (sans progress bars).
If you happen to also mess up what you pass in to the .pb
parameter or get your progress bar out of sync with your object it won’t error out on you (it can be made much safer and wrapped in another function, say — tick_off(.pb)
— but this is supposed to be a small post).
Comments/feedback/your-own-progress-methods are most welcome and encouraged.
4 Comments
Hi Bob, that’s a really nice and flexible way to create a progress bar but I’d like to pick your brain about progress bars. Do you see any obvious advantages of progress_estimated{dplyr} over pbapply ?
I have been using the pbapply library to create progress bars. In your example it would be.
pbsapply(letters, arduouslylongnchar), or
pblapply(letters, arduouslylongnchar) depending on the desired output format.
I used to use
pbapply
functions all the time.purrr
map
functions have type-safety, really nice anon shorthand (~{}
makes an anon function w/.
,.x
,.y
auto-vars), and I thinkmap_df(…)
with my progress bar is way better than abind_rows()
after apblapply
, especially with that type-safety thing I mentioned.It’d be possible to write a wrapper library that does the same thing for
purrr
functions, too. Ultimately, the idiom that works for you (i.e. enables you to Get Stuff Done™) is the most important thing.Thanks! I will look more closely into
purrr
map
!This is excellent, and i’m a big fan of short posts like this. Thanks!
One question: Any thoughts about how to roll this into a progress bar class to rule them all, i.e. one so a function could work interactively, and then also with Shiny like so: https://shiny.rstudio.com/articles/progress.html?
(Yes, I was also compelled to comment to make this multiple Rafaels)
2 Trackbacks/Pingbacks
[…] article was first published on R – rud.is, and kindly contributed to […]
[…] @eddelbuettel’s idea is a good one. (it’s a quick read…jump there and come back). I often wait for a complete example or new package announcement to blog something when a briefly explained snippet might have sufficient utility for many R users. Also, tweets are fleeting and twitter could end up on the island of misfit… Continue reading → […]