<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/simmer-3-6-3/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.enchufa2.es/archives/simmer-3-6-3.html"> R – Enchufa2</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The third update of the 3.6.x release of <a rel="nofollow" href="http://r-simmer.org/" target="_blank">simmer</a>, the Discrete-Event Simulator for R, is on CRAN. First of all and once again, I must thank Duncan Garmonsway (@<a rel="nofollow" href="https://github.com/nacnudus" target="_blank">nacnudus</a>) for writing a new vignette: <a rel="nofollow" href="http://r-simmer.org/articles/simmer-04-bank-2.html" target="_blank">“The Bank Tutorial: Part II”</a>.</p>
<p>Among various fixes and performance improvements, this release provides a way of knowing the progress of a simulation. This feature is embed into the <code>run()</code> function as a parameter called <code>progress</code>, which accepts a function with a single argument and will be called for each one of the <code>steps</code> (10 by default) with the current progress ratio (a number between 0 and 1). A very naive example using a simple <code>print()</code>:</p>
<pre><code><span>library</span><span>(</span><span>simmer</span><span>)</span>

<span>wait_and_go</span> <span>&lt;-</span> <span>trajectory</span><span>(</span><span>)</span> <span>%>%</span>
  <span>timeout</span><span>(</span><span>1</span><span>)</span>

<span>simmer</span><span>(</span><span>)</span> <span>%>%</span>
  <span>add_generator</span><span>(</span><span>&quot;dummy&quot;</span>, <span>wait_and_go</span>, <span>function</span><span>(</span><span>)</span> <span>1</span><span>)</span> <span>%>%</span>
  <span>run</span><span>(</span><span>until</span><span>=</span><span>10</span>, <span>progress</span><span>=</span><span>print</span>, <span>steps</span><span>=</span><span>5</span><span>)</span></code></pre>
<pre><code>## [1] 0
## [1] 0.2
## [1] 0.4
## [1] 0.6
## [1] 0.8
## [1] 1</code></pre>
<pre><code>## simmer environment: anonymous | now: 10 | next: 10
## { Generator: dummy | monitored: 1 | n_generated: 10 }</code></pre>
<p>Or we can get a nice progress bar with the assistance of the <a rel="nofollow" href="https://cran.r-project.org/package=progress" target="_blank"><code>progress</code></a> package:</p>
<pre><code><span>simmer</span><span>(</span><span>)</span> <span>%>%</span>
  <span>add_generator</span><span>(</span><span>&quot;dummy&quot;</span>, <span>wait_and_go</span>, <span>function</span><span>(</span><span>)</span> <span>1</span><span>)</span> <span>%>%</span>
  <span>run</span><span>(</span><span>until</span><span>=</span><span>1e5</span>, <span>progress</span><span>=</span><span>progress</span><span>:</span><span>:</span><span>progress_bar</span><span>$</span><span>new</span><span>(</span><span>)</span><span>$</span><span>update</span><span>)</span>
<span>#> [==============---------------------------------------------------------] 20%</span></code></pre>
<p>But more importantly, this release implements a new way of retrieving attributes (thus deprecating the old approach, which will be still available throughout the 3.6.x series and will be removed in version 3.7). Since v3.1.x, arrival attributes were retrieved by providing a function with one argument. A very simple example:</p>
<pre><code><span>trajectory</span><span>(</span><span>)</span> <span>%>%</span>
  <span>set_attribute</span><span>(</span><span>&quot;delay&quot;</span>, <span>3</span><span>)</span> <span>%>%</span>
  <span>timeout</span><span>(</span><span>function</span><span>(</span><span>attr</span><span>)</span> <span>attr</span><span>[</span><span>&quot;delay&quot;</span><span>]</span><span>)</span></code></pre>
<pre><code>## Warning: Attribute retrieval through function arguments is deprecated.
## Use 'get_attribute' instead.</code></pre>
<pre><code>## trajectory: anonymous, 2 activities
## { Activity: SetAttribute | key: delay, value: 3, global: 0 }
## { Activity: Timeout      | delay: 0x5569098b9228 }</code></pre>
<p>Later on, v3.5.1 added support for global attributes, making it necessary to add a second argument to retrieve this new set of attributes:</p>
<pre><code><span>trajectory</span><span>(</span><span>)</span> <span>%>%</span>
  <span>set_attribute</span><span>(</span><span>&quot;delay&quot;</span>, <span>3</span>, <span>global</span><span>=</span><span>TRUE</span><span>)</span> <span>%>%</span>
  <span>timeout</span><span>(</span><span>function</span><span>(</span><span>attr</span>, <span>glb</span><span>)</span> <span>glb</span><span>[</span><span>&quot;delay&quot;</span><span>]</span><span>)</span></code></pre>
<pre><code>## Warning: Attribute retrieval through function arguments is deprecated.
## Use 'get_attribute' instead.</code></pre>
<pre><code>## trajectory: anonymous, 2 activities
## { Activity: SetAttribute | key: delay, value: 3, global: 1 }
## { Activity: Timeout      | delay: 0x556908730320 }</code></pre>
<p>This method is a kind of rarity in <code>simmer</code>. It’s clunky, as it is not easy to document (and therefore to discover and learn), and non-scalable, because new features would require more and more additional arguments. Thus, it is now deprecated, and the <code>get_attribute()</code> function becomes the new method for retrieving attributes. It works in the same way as <code>now()</code> for the simulation time:</p>
<pre><code><span>env</span> <span>&lt;-</span> <span>simmer</span><span>(</span><span>)</span>

<span>trajectory</span><span>(</span><span>)</span> <span>%>%</span>
  <span>set_attribute</span><span>(</span><span>&quot;delay_1&quot;</span>, <span>3</span><span>)</span> <span>%>%</span>
  <span># shortcut equivalent to set_attribute(..., global=TRUE)</span>
  <span>set_global</span><span>(</span><span>&quot;delay_2&quot;</span>, <span>2</span><span>)</span> <span>%>%</span> 
  <span>timeout</span><span>(</span><span>function</span><span>(</span><span>)</span> <span>get_attribute</span><span>(</span><span>env</span>, <span>&quot;delay_1&quot;</span><span>)</span><span>)</span> <span>%>%</span>
  <span># shortcut equivalent to get_attribute(..., global=TRUE)</span>
  <span>timeout</span><span>(</span><span>function</span><span>(</span><span>)</span> <span>get_global</span><span>(</span><span>env</span>, <span>&quot;delay_2&quot;</span><span>)</span><span>)</span></code></pre>
<pre><code>## trajectory: anonymous, 4 activities
## { Activity: SetAttribute | key: delay_1, value: 3, global: 0 }
## { Activity: SetAttribute | key: delay_2, value: 2, global: 1 }
## { Activity: Timeout      | delay: 0x55690829f550 }
## { Activity: Timeout      | delay: 0x55690830c310 }</code></pre>
<p>This is a little bit more verbose, but I believe it is more consistent and intuitive. Moreover, it allows us to easily implement new features for extracting arrival information. In fact, <code>get_attribute()</code> will come hand in hand with two more verbs: <code>get_name()</code> and <code>get_prioritization()</code>, to retrieve the arrival name and prioritization values respectively.</p>
<blockquote>
<h3>New features:</h3>
<ul>
<li>Show simulation progress via an optional <code>progress</code> callback in <code>run()</code> (#103).</li>
<li>New “The Bank Tutorial: Part II” vignette, by Duncan Garmonsway <span>@nacnudus</span> (#106).</li>
<li>New getters for running arrivals (#109), meant to be used inside trajectories:
<ul>
<li><code>get_name()</code> retrieves the arrival name.</li>
<li><code>get_attribute()</code> retrieves an attribute by name. The old method of retrieving them by providing a function with one argument is deprecated in favour of <code>get_attribute()</code>, and will be removed in version 3.7.x.</li>
<li><code>get_prioritization()</code> retrieves the three prioritization values (<code>priority</code>, <code>preemptible</code>, <code>restart</code>) of the active arrival.</li>
</ul>
</li>
<li>New shortcuts for global attributes (#110): <code>set_global()</code> and <code>get_global()</code>, equivalent to <code>set_attribute(global=TRUE)</code> and <code>get_attribute(global=TRUE)</code> respectively.</li>
</ul>
<h3>Minor changes and fixes:</h3>
<ul>
<li>Some code refactoring and performance improvements (2f4b484, ffafe1e, f16912a, fb7941b, 2783cd8).</li>
<li>Use <code>Rcpp::DataFrame</code> instead of <code>Rcpp::List</code> (#104).</li>
<li>Improve argument parsing and error messages (#107).</li>
<li>Improve internal function <code>make_resetable()</code> (c596f73).</li>
</ul>
</blockquote>
<p><em>Article originally published in Enchufa2.es: <a rel="nofollow" href="https://www.enchufa2.es/archives/simmer-3-6-3.html" target="_blank">simmer 3.6.3</a></em>.</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://www.enchufa2.es/archives/simmer-3-6-3.html"> R – Enchufa2</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/Slm5mZshqg0" height="1"></a></div>