<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/a-note-on-on-exit/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://yihui.name/en/2017/05/a-note-on-on-exit/"> English Blog on Yihui Xie | 谢益辉</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I have used <code>on.exit()</code> for several years, but it was not until the other day that I realized a very weird thing about it: you’d better follow the default positions of its arguments <code>expr</code> and <code>add</code>, i.e., the first argument has to be <code>expr</code> and the second has to be <code>add</code>.</p>
<pre><code>on.exit(expr = NULL, add = FALSE)
</code></pre>
<p>If you do <code>on.exit(add = TRUE, {...})</code>, weird things can happen. I discovered this <a rel="nofollow" href="https://github.com/rstudio/bookdown/commit/1f77a3e4ab" target="_blank">by accident</a>. I have never switched the positions of <code>expr</code> and <code>add</code> before, and I was surprised that <code>R CMD check</code> failed on <a rel="nofollow" href="https://travis-ci.org/rstudio/bookdown/builds/233076842" target="_blank">Travis</a> with an error message that confused me in the beginning:</p>
<pre><code>Error in on.exit(add = TRUE, if (file.exists(main)) { : 
  invalid 'add' argument
</code></pre>
<p>I was thinking why <code>add = TRUE</code> was considered invalid. Then I guessed perhaps the expression <code>if (file.exists(main)) {}</code> was treated as the actual value of <code>add</code>. So I switched to the normal order of arguments, and the error was gone.</p>
<p>I tested it a bit more and was totally confused, e.g., why was <code>1</code> printed twice below? I guess <code>TRUE</code> was not printed because <code>add</code> was treated as <code>expr</code>.</p>
<pre><code>f = function() {
  on.exit(add = print(TRUE), print(1))
}
f()
# [1] 1
# [1] 1
</code></pre>
<p>I don’t have the capability to understand the <a rel="nofollow" href="https://github.com/wch/r-source/blob/a7356bf91b/src/main/builtin.c#L131-L186" target="_blank">source code in C</a>, and I’ll leave it experts to explain the weird things I observed. For me, I’ll just never move <code>add</code> before <code>expr</code> again.</p>
<p>BTW, I don’t what the rationale is for the default <code>add = FALSE</code> in <code>on.exit()</code>, but I have not used <code>add = FALSE</code> for a single time, so I feel <code>add = TRUE</code> might be a better default. When I want to do something on exit, I almost surely mean do it <em>in addition to</em> the things that I assigned to <code>on.exit()</code> before, instead of cleaning up all previous tasks and only doing this one (<code>add = FALSE</code>).</p>
<p> </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://yihui.name/en/2017/05/a-note-on-on-exit/"> English Blog on Yihui Xie | 谢益辉</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/4u3QFdzA9Pw" height="1"></a></div>