<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/let-there-be-progress-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fjohnmackintosh.com%2F2017-06-01-UPDATED-let-there-be-progress%2F"> HighlandR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>	The ‘wrapr’package for use with dplyr programming – </p>
<h2>UPDATED POST</h2>
<p>I’m the first to admit I’m not an R expert, (even duffers can blog about it though), but when I began thinking about writing some dplyr functions to help me create and analyse <a target="_blank" rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fjohnmackintosh.com%2F2017-04-06-the-run-chart-ing-man%2F">run charts</a>, I had no idea that I was going to struggle quite so much getting to grips with Non Standard Evaluation and Lazy Evaluation (see my last post for links and more background).</p>
<p>To clarify, I wanted to create flexible dplyr functions, without hardcoded grouping parameters, so I could query different SQL Server tables and apply the same functions reliably to transform and plot the data.</p>
<p>Eventually I sussed things out and created the functions I needed, but it was yet another example of how R makes me feel really stupid on a regular basis.<br>
Please tell me I’m not alone with that…</p>
<p>I’m aware that dplyr 0.6 is <strong>imminent</strong>, and that my code will need revising as the dplyr verbs with underscores (for programmatic use) will be deprecated (at least eventually).</p>
<p>While I could probably install the dev version , I thought I should finally take a look at the let function in the <a target="_blank" rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fcran.r-project.org%2Fpackage%3Dwrapr">wrapr</a> package, as I’d seen numerous tweets giving it <a target="_blank" rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Ftwitter.com%2Fjsonbecker%2Fstatus%2F852339824774262784">positive feedback</a>.</p>
<p>I had a bash at recreating one of my functions – got some help directly from <a target="_blank" rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fwww.win-vector.com%2Fsite%2F">win-vector</a> and was then able to rewrite all my remaining functions quickly.</p>
<p>I’ve put the code gist <a target="_blank" rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fgist.github.com%2Fjohnmackintosh%2F30ec559bac2d09be4d0d01122b5dd35f">here</a> – should hopefully run as intended, assuming you have the current dplyr 0.5 and wrapr packages installed.</p>
<h2>Points to note</h2>
<ul>
<li>
<p>I commented out a line in the wrapr let example code for this demo so that all 3 return the same output.</p>
</li>
<li>
<p>There is some nice code to handle the lack of a grouping column, courtesy of Win-Vector. For my real world use, there will always be a grouping variable, but its nice to build this in for future requirements.</p>
</li>
<li>
<p>I used slice instead of filter in the NSE code – I could not get it to work at all otherwise. I’m pretty convinced I’ve done something wrong here, because the function only runs if I don’t wrap the variables in quotes when I call it. For the life of me I couldn’t figure out why a grouping call would work in one NSE function and fail in another when the same syntax was being used. Bearing in mind that the work I actually WANTED to do was going to be complex enough, I was just happy that it worked.</p>
</li>
</ul>
<h2>Code examples</h2>
<p>Day to day, interactive dplyr code :<br>
<img alt="2017-05-31 regular interactive dplyr.PNG" src="https://i1.wp.com/johnmackintosh.com/img/2017-05-31%20regular%20interactive%20dplyr.PNG?w=456" data-recalc-dims="1"></p>
<p>My attempt at the whole NSE/lazy eval thing :<br>
<img alt="2017-05-31 dplyr original NSE.PNG" src="https://i0.wp.com/johnmackintosh.com/img/2017-05-31%20dplyr%20original%20NSE.PNG?w=456" data-recalc-dims="1"></p>
<p>Wrapr code :<br>
<img alt="2017-05-31 wrapr comparison.PNG" src="https://i2.wp.com/johnmackintosh.com/img/2017-05-31%20wrapr%20comparison.PNG?w=456" data-recalc-dims="1"></p>
<p>With the let function you create the mapping block first, then use the variables there in your function – and can use the regular dplyr verbs. Once I’d seen how to set up the code, it was easy to recreate the others.</p>
<p>Outputs (in same order as above code examples):</p>
<p><img alt="2017-05-31 interactivePNG.PNG" src="https://i2.wp.com/johnmackintosh.com/img/2017-05-31%20interactivePNG.PNG?w=456" data-recalc-dims="1"></p>
<p><img alt="2017-05-31 NSE output.PNG" src="https://i1.wp.com/johnmackintosh.com/img/2017-05-31%20NSE%20output.PNG?w=456" data-recalc-dims="1"></p>
<p><img alt="2017-05-31 wrapr output.PNG" src="https://i2.wp.com/johnmackintosh.com/img/2017-05-31%20wrapr%20output.PNG?w=456" data-recalc-dims="1"></p>
<p>Here’s another example of using the underscore versions compared to wrapr.</p>
<p>This time the NSE code works with quoted variables:</p>
<p><img alt="2017-05-31 NSE StartDate.PNG" src="https://i1.wp.com/johnmackintosh.com/img/2017-05-31%20NSE%20StartDate.PNG?w=456" data-recalc-dims="1"></p>
<p>And here is the wrapr equivalent using let:</p>
<p><img alt="2017-05-31 wrapr start date.PNG" src="https://i1.wp.com/johnmackintosh.com/img/2017-05-31%20wrapr%20start%20date.PNG?w=456" data-recalc-dims="1"></p>
<p>From a real world deadlines / getting things done perspective – if you need to create flexible dplyr functions, and you need to do it soon – take a look at wrapr to see if it fits your needs.</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="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=http%3A%2F%2Fjohnmackintosh.com%2F2017-06-01-UPDATED-let-there-be-progress%2F"> HighlandR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Ffeedburner.google.com%2Ffb%2Fa%2Fmailverify%3Furi%3DRBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fwww.r-project.org%2F">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="http://apicdn.viglink.com/api/click?format=go&key=949efb41171ac6ec1bf7f206d57e90b8&loc=https%3A%2F%2Fwww.r-bloggers.com%2Ffeed%2F&out=https%3A%2F%2Fwww.r-users.com%2F">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/5lGpLqvBSJM"></a></div>