All-in on R⁴ : Progress [bars] on first post
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
@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 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 “r4” 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.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.