<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/all-in-on-r%e2%81%b4-progress-bars-on-first-post/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/03/27/all-in-on-r%E2%81%B4-progress-bars-on-first-post/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://twitter.com/eddelbuettel">@eddelbuettel</a>’s <a target="_blank" rel="nofollow" href="http://dirk.eddelbuettel.com/blog/2017/03/27/#000_introducing_r4">idea</a> is a good one. (it’s a quick read…jump there and come back). </p>
<p>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, <a target="_blank" rel="nofollow" href="https://www.socialmediadelivered.com/blog/2015/10/29/the-lifespan-of-a-social-media-post">tweets are fleeting</a> 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 <a target="_blank" rel="nofollow" href="https://blog.archive.org/2017/01/13/wayback-machine-chrome-extension-now-available/">handy Chrome extension</a>).</p>
<p>I’ll tag all R⁴ posts as “r4” if you want to auto-filter those out of your stream or just page through them.</p>
<p>I’ll also lead off the these micro-posts with a simple one: adding progress bars to your tidyverse <code>purrr</code> operations.</p>
<p>The <code>purrr::map*</code> 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 <code>purrr</code> calls). The following is a toy example, but it’s a working example you can run in your interactive R session now:</p>
<pre><code>library(tidyverse)

arduously_long_nchar &lt;- function(input_var, .pb=NULL) {
  
  if ((!is.null(.pb)) &amp;&amp; inherits(.pb, &quot;Progress&quot;) &amp;&amp; (.pb$i &lt; .pb$n)) .pb$tick()$print()
  
  Sys.sleep(1)
  
  nchar(input_var)
  
}

pb &lt;- progress_estimated(length(letters))

map_int(letters, arduously_long_nchar, .pb=pb)</code></pre>
<p>And, yes, I did make you wait ~26 seconds (unless you were intrepid enough to reduce the amount of sleep time <img alt="🙂" class="wp-smiley" src="https://s.w.org/images/core/emoji/2/72x72/1f642.png"></p>
<p>If you happen to forget the progress bar object (or know you don’t need one):</p>
<pre><code>map_int(letters, arduously_long_nchar)</code></pre>
<p>the function still works (sans progress bars).</p>
<p>If you happen to also mess up what you pass in to the <code>.pb</code> parameter or get your progress bar out of sync with your object it won’t error out on you (it can be made <em>much</em> safer and wrapped in another function, say — <code>tick_off(.pb)</code> — but this is supposed to be a small post).</p>
<p>Comments/feedback/your-own-progress-methods are most welcome and encouraged.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rud.is/b/2017/03/27/all-in-on-r%E2%81%B4-progress-bars-on-first-post/"> R – rud.is</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/W0u71o3LKYE"></a></div>