<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/get-out-of-my-way-dunk-thru-rstats-errors-like-the-big-shaq-istician/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/RstatsMikeJackTzen/~3/xb-iuOqQiF0/"> rstats – MikeJackTzen</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Ahh, leaves falling, parents crying, collegicians biking uphill with a bag of in-n-out in between their teeth. Must be the new academic school year!</p>
<p><a rel="nofollow" href="https://mikejacktzen.files.wordpress.com/2017/09/shaq.jpg" target="_blank"><img data-orig-file="https://mikejacktzen.files.wordpress.com/2017/09/shaq.jpg?w=456" data-orig-size="350,350" data-medium-file="https://mikejacktzen.files.wordpress.com/2017/09/shaq.jpg?w=456?w=300" data-attachment-id="828" data-image-meta="{" data-image-description="" src="https://mikejacktzen.files.wordpress.com/2017/09/shaq.jpg?w=456" data-permalink="https://mikejacktzen.wordpress.com/2017/10/01/shaq-dunk-thru-datzen/shaq/" data-large-file="https://mikejacktzen.files.wordpress.com/2017/09/shaq.jpg?w=456?w=350" alt="" data-image-title="shaqistotle" sizes="(max-width: 350px) 100vw, 350px" data-comments-opened="1" class="wp-image-828" data-recalc-dims="1"></a></p>
<p>I figured it’s a good time to introduce my work-in-progress <a rel="nofollow" href="https://github.com/mikejacktzen/datzen" target="_blank">datzen</a> package of miscellaneous #rstats functions.  You can bee-line straight to the github <a rel="nofollow" href="https://github.com/mikejacktzen/datzen/blob/master/README.md" target="_blank">readme</a> with more examples.</p>
<p>Or stick around and I’ll highlight the Shaq example showcasing <code>datzen::itersave()</code></p>
<p>In #rstats if you want to <a rel="nofollow" href="http://r4ds.had.co.nz/iteration.html" target="_blank">iterate</a>, you can go about it in many different ways. Works pretty well for “homogeneous” iterations.</p>
<p>As good as they are, the standard approaches hit snags for “non-homogeneous” iterations, eg data from the web.</p>
<p>Go ahead, try them. I dare you.</p>
<p><em>You in 5 hours</em></p>
<p>“Aw shit, my brute force for loop crapped the bed during iteration 69. Now I have to manually restart it. I hope it doesn’t do it again. I’m running out of patience, and linen.”</p>
<p>Let’s take a look. The Big Aristotle, Dr. Shaq, was a notorious brute on the hardwood. Here he is, contemplating how he should score in the paint:</p>
<pre>
shaq = function(meatbag){
if(meatbag %in% 'scrub'){return('dunk on em')}
if(meatbag %in% 'sabonis'){return('elbow his face')}
if(!(meatbag %in% c('scrub','sabonis'))){
stop('shaq is confused')}
}

meatbags = c('scrub','sabonis','scrub','kobe')
names(meatbags) = paste0('arg_',seq_along(meatbags))

testthat::expect_failure(lapply(meatbags,FUN=shaq))
#> Error in FUN(X[[i]], ...): shaq is confused
</pre>
<p>Uh, some error confused Shaq.</p>
<p><em>enter, stage trap door</em><br>
“Meet <code>itersave()</code> ”</p>
<p><em>front row faints</em><br>
“It’s… hideously beautiful”</p>
<p>In a nutshell, <code>itersave</code> works like <code>lapply</code> but when it meets an ugly, unskilled, unqualified, and ungraceful error it will keep trucking along like Shaquille The Diesel O’Neal hitchhiking a ride on Chris Dudley’s <a rel="nofollow" href="https://www.youtube.com/watch?v=0ICBi-ku-G0" target="_blank">back</a></p>
<pre>
mainDir=paste0(getwd(),'/tests/proto/')
subDir='/temp/'

itersave(func_user=shaq,
         vec_arg_func=meatbags,
         mainDir,subDir)
#> [1] &quot;1 of 4&quot;
#> [1] &quot;2017-10-01 12:35:14 PDT&quot;
#> [1] &quot;arg_1&quot;
#> [1] &quot;2 of 4&quot;
#> [1] &quot;2017-10-01 12:35:14 PDT&quot;
#> [1] &quot;arg_2&quot;
#> [1] &quot;3 of 4&quot;
#> [1] &quot;2017-10-01 12:35:14 PDT&quot;
#> [1] &quot;arg_3&quot;
#> [1] &quot;4 of 4&quot;
#> [1] &quot;2017-10-01 12:35:14 PDT&quot;
#> [1] &quot;arg_4&quot;
</pre>
<p>The meatbags that Shaq succesfully put into bodybags.</p>
<pre>
print('the successes')
#> [1] &quot;the successes&quot;
list.files(paste0(mainDir,subDir))
#> [1] &quot;arg_1.rds&quot; &quot;arg_2.rds&quot; &quot;arg_3.rds&quot; &quot;failed&quot;
</pre>
<p>It’ll also book keep any errors along the way via <a rel="nofollow" href="http://purrr.tidyverse.org/" target="_blank">purrr</a>::<code>safely()</code> and <a rel="nofollow" href="https://cran.r-project.org/web/packages/R.utils/index.html" target="_blank">R.utils</a>::<code>withTimeout()</code>.</p>
<pre>
print('the failures')
#> [1] &quot;the failures&quot;
list.files(paste0(mainDir,subDir,'/failed/'))
#> [1] &quot;arg_4.rds&quot;
</pre>
<p>Along with the <strong>out</strong>, itersave has an <strong>in</strong> companion</p>
<p><em>enter, zipline from balcony</em><br>
“meet <code>iterload()</code> ”</p>
<p><em>audience faints</em></p>
<pre>
iterload(paste0(mainDir,subDir,'/failed'))
#> $arg_4
#> $arg_4$ind_fail
#> [1] 4
#> 
#> $arg_4$input_bad
#> [1] &quot;kobe&quot;
#> 
#> $arg_4$result_bad
#> </pre>