<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/how-to-add-pbapply-to-r-packages-2/"></div></div><div><a data-url="https://www.r-bloggers.com/how-to-add-pbapply-to-r-packages-2/" data-counturl="https://www.r-bloggers.com/how-to-add-pbapply-to-r-packages-2/" data-count="horizontal" data-text="How to add pbapply to R packages" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://peter.solymos.org/code/2016/09/16/how-to-add-pbapply-to-r-packages.html"> Peter Solymos - R related posts</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>As of today, there are 20 R packages that reverse depend/import/suggest (3/14/3)<br>
the <a target="_blank" href="http://cran.r-project.org/package=pbapply"><strong>pbapply</strong></a> package. Current and future package developers<br>
who decide to incorporate the progress bar using <strong>pbapply</strong><br>
might want to customize the type and style of the progress bar<br>
in their packages to better suit the needs of certain functions<br>
or to create a distinctive look.<br>
Here is a quick guide to help in setting up and customizing the progress bar.</p>
<p><img alt="" src="https://i1.wp.com/github.com/psolymos/pbapply/raw/master/images/pbapply-01.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Adding pbapply</h2>
<p>The <strong>pbapply</strong> package has no extra (non <code>r-base-core</code>) dependencies and is lightweight,<br>
so adding it as dependency does not represent a major overhead.<br>
There are two alternative ways of adding the <strong>pbapply</strong> package to another<br>
R package: <em>Suggests</em>, or <em>Depends/Imports</em>. Here are the recommended and<br>
tested ways of adding a progress bar to other R packages<br>
(see the <a target="_blank" href="https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Package-Dependencies"><em>Writing R Extensions</em></a> manual for an official guide).</p>
<h3>1. Suggests: pbapply</h3>
<p>The user decides whether to install <strong>pbapply</strong> and the function behavior changes accordingly. This might be preferred if there are only few functions that utilize a progress bar.</p>
<p><strong>pbapply</strong> needs to be added to the <code>Suggests</code> field in the <code>DESCRIPTION</code> file and<br>
use conditional statements in the code to fall back on a base functions<br>
in case of <strong>pbapply</strong> is not being installed:</p>
<div>
<pre><code>out &lt;- if (requireNamespace(&quot;pbapply&quot;))
   pbapply::pblapply(X, FUN, ...) else lapply(X, FUN, ...)
</code></pre>
</div>
<p>See a small R package <a target="_blank" href="https://github.com/psolymos/pbapplySuggests">here</a><br>
for an example (see <code>R CMD check</code> log on Travis CI: <a target="_blank" href="https://travis-ci.org/psolymos/pbapplySuggests"><img alt="Build Status" src="https://travis-ci.org/psolymos/pbapplySuggests.svg?branch=master"></a>).</p>
<h3>2. Depends/Imports: pbapply</h3>
<p>In this second case, <strong>pbapply</strong> needs to be installed and called explicitly<br>
via <code>::</code> or <code>NAMESPACE</code>. This might be preferred if many functions utilize<br>
the progress bar.</p>
<p><strong>pbapply</strong> needs to be added to the <code>Depends</code> or <code>Imports</code> field<br>
in the <code>DESCRIPTION</code> file.<br>
Use <strong>pbapply</strong> functions either as <code>pbapply::pblapply()</code> or specify them in the <code>NAMESPACE</code> (e.g. <code>importFrom(pbapply, pblapply)</code>) and<br>
use it as <code>pblapply()</code> (without the <code>::</code>).</p>
<p>See a small R package <a target="_blank" href="https://github.com/psolymos/pbapplyDepends">here</a><br>
for an example (see <code>R CMD check</code> log on Travis CI: <a target="_blank" href="https://travis-ci.org/psolymos/pbapplyDepends"><img alt="Build Status" src="https://travis-ci.org/psolymos/pbapplyDepends.svg?branch=master"></a>).</p>
<h2>Customizing the progress bar</h2>
<p>Other than aesthetical reasons, there are cases when customizing the<br>
progress bar is truly necessary.<br>
For example, when working with a GUI, the default text based progress<br>
bar might not be appropriate and developers want a Windows or Tcl/Tk<br>
based progress bar.</p>
<p>In such cases, one can specify the progress bar options in the<br>
<code>/R/zzz.R</code> file of the package. The following example<br>
shows the default settings, but any of those list elements<br>
can be modified (see <code>?pboptions</code> for acceptable values):</p>
<div>
<pre><code>.onAttach &lt;- function(libname, pkgname){
    options(&quot;pboptions&quot; = list(
        type = if (interactive()) &quot;timer&quot; else &quot;none&quot;,
        char = &quot;-&quot;,
        txt.width = 50,
        gui.width = 300,
        style = 3,
        initial = 0,
        title = &quot;R progress bar&quot;,
        label = &quot;&quot;,
        nout = 100L))
    invisible(NULL)
}
</code></pre>
</div>
<p>Specifying the progress bar options this way will set the options<br>
before <strong>pbapply</strong> is loaded. <strong>pbapply</strong> will not override these<br>
settings. It is possible to specify a partial list of options<br>
(from <strong>pbapply</strong> version 1.3-0 and above).</p>
<h2>Suppressing the progress bar</h2>
<p>Suppressing the progress bar is sometimes handy.<br>
By default, progress bar is suppressed when <code>!interactive()</code>.<br>
This is an important feature, so that <strong>Sweave</strong>, <strong>knitr</strong>,<br>
and R markdown documents are not polluted with a really long<br>
printout of the progress bar.<br>
(Although, it is possible to turn the progress bar back on within such documents.)</p>
<p>In an interactive session, put this inside a function to disable the<br>
progress bar (and reset it when exiting the function):</p>
<div>
<pre><code>pbo &lt;- pboptions(type = &quot;none&quot;)
on.exit(pboptions(pbo))
</code></pre>
</div>
<p>I hope that this little tutorial helps getting a progress bar where it belongs.<br>
Suggestions and feature requests are welcome.<br>
Leave a comment or visit the <a target="_blank" href="https://github.com/psolymos/pbapply/issues">GitHub repo</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/how-to-add-pbapply-to-r-packages-2/"></div></div><div><a data-url="https://www.r-bloggers.com/how-to-add-pbapply-to-r-packages-2/" data-counturl="https://www.r-bloggers.com/how-to-add-pbapply-to-r-packages-2/" data-count="vertical" data-text="How to add pbapply to R packages" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://peter.solymos.org/code/2016/09/16/how-to-add-pbapply-to-r-packages.html"> Peter Solymos - R related posts</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/0qpXzGUCH_E"></a></div>