<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/building-views-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.quantide.com/building-views-with-r/"> R blog | Quantide - R training &amp; consulting</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h2><img sizes="(max-width: 960px) 100vw, 960px" alt="views-R" class="wp-image-3553" src="https://i0.wp.com/www.quantide.com/wp-content/uploads/2017/03/views.jpg?w=450" data-recalc-dims="1"></h2>
<p> </p>
<p>[Here you can see the <a target="_blank" rel="nofollow" href="http://www.quantide.com/wp-content/uploads/2017/03/views.pdf">Building views with R cheat sheet</a> at a full resolution]</p>
<h2></h2>
<h2>Queries</h2>
<p>In database theory a query is a request for data or information from a database table or combination of tables.</p>
<p>Since <code>dplyr</code> we have something that quite closely conceptually resembles a query in <code>R</code>:</p>
<pre>require(dplyr)</pre>

<pre>## Warning: package 'dplyr' was built under R version 3.2.5</pre>

<pre>require(pryr)</pre>

<pre>mtcars %>% 
  tbl_df() %>% 
  group_by(cyl) %>% 
  summarise(mean_mpg = mean(mpg), sd_mpg = sd(mpg))</pre>

<pre>## # A tibble: 3 × 3
##     cyl mean_mpg   sd_mpg
##   &lt;dbl>    &lt;dbl>    &lt;dbl>
## 1     4 26.66364 4.509828
## 2     6 19.74286 1.453567
## 3     8 15.10000 2.560048</pre>
<p>
I particularly appreciate of <code>dplyr</code> the possibility of building my <em>query</em> as a step by step set of <code>R</code> statement that I can progressively test at each step.</p>
<p> </p>
</div>
<div>
<h2>Views</h2>
<p>Again in database theory, a <em>view</em> is the result set of a stored query on the data, which the database users can query just as they would in a table.</p>
<p>I would like to have something similar to a view in <code>R</code></p>
<p>As far as I know, I can achieve this goal in three ways:</p>
<ul>
<li>Function <code>makeActiveBinding</code></li>
<li>Operator <code>%>a%</code> from package <code>pryr</code></li>
<li>My proposed `%>>% operator</li>
</ul>
<p> </p>
</div>
<div>
<h2>Function <code>makeActiveBinding()</code></h2>
<p>Function <code>makeActiveBinding(sym, fun, env)</code> installs a function in an environment <code>env</code> so that getting the value of <code>sym</code> calls <code>fun</code> with no arguments.</p>
<p>As a basic example I can actively bind a function that simulates a dice to an object named <code>dice</code> :</p>
<pre>makeActiveBinding(&quot;dice&quot;, function() sample(1:6, 1), env = globalenv())</pre>
<p>
so that:</p>
<pre>replicate(5 , dice)</pre>

<pre>## [1] 5 1 6 2 3</pre>
<p>
Similarly, I can wrap a<code>dplyr</code> expression into a function:</p>
<pre>f &lt;- function() {mtcars %>% 
  group_by(cyl) %>% 
  summarise(mean_mpg = mean(mpg), sd_mpg = sd(mpg))}</pre>
<p>
and then actively bind it to a symbol:</p>
<pre>makeActiveBinding('view', f , env = globalenv())</pre>
<p>
so that, any time we call <code>view</code> the result of function <code>f()</code>is computed again:</p>
<pre>view</pre>

<pre>## # A tibble: 3 × 3
##     cyl mean_mpg   sd_mpg
##   &lt;dbl>    &lt;dbl>    &lt;dbl>
## 1     4 26.66364 4.509828
## 2     6 19.74286 1.453567
## 3     8 15.10000 2.560048</pre>
<p>
As a result, if I change any value of <code>mpg</code> within <code>mtcars</code>, <code>view</code> is automatically updated:</p>
<pre>mtcars$mpg[c(1,3,5)] &lt;- 0
view</pre>

<pre>## # A tibble: 3 × 3
##     cyl mean_mpg   sd_mpg
##   &lt;dbl>    &lt;dbl>    &lt;dbl>
## 1     4 24.59091 9.231192
## 2     6 16.74286 7.504189
## 3     8 13.76429 4.601606</pre>
<p>
Clearly, I have to admit that all of this looks quite unfriendly, at least to me.</p>
<p> </p>
</div>
<div>
<h2>Operator <code>%&lt;a-%</code></h2>
<p>A valid alternative, that wraps away the complexity of function <code>makeActiveBinding()</code> is provided by operator <code>%&lt;a-%</code> from package <code>pryr</code>:</p>
<pre>view %&lt;a-%  {mtcars %>% 
  group_by(cyl) %>% 
  summarise(mean_mpg = mean(mpg), sd_mpg = sd(mpg))}</pre>
<p>
Again, if I change any value of <code>mpg</code> within <code>mtcars</code>, the value of <code>view</code> get automatically updated:</p>
<pre>mtcars$mpg[c(1,3,5)] &lt;- 50
view</pre>

<pre>## # A tibble: 3 × 3
##     cyl mean_mpg    sd_mpg
##   &lt;dbl>    &lt;dbl>     &lt;dbl>
## 1     4 29.13636  8.159568
## 2     6 23.88571 11.593451
## 3     8 17.33571  9.688503</pre>
<p>
Note that in this case I have to enclose the whole expression within curly brackets.</p>
<p>Moreover, the final assignment: <code>%&lt;a-%</code> goes on the left hand side of my chain of <code>dplyr</code> statements.</p>
<p> </p>
</div>
<div>
<h2>Operator <code>%>>%</code></h2>
<p>Finally I would like to propose a third alternative, still based on <code>makeActiveBinding()</code>, that I named <code>%>>%</code></p>
<pre>`%>>%` &lt;- function( expr, x) {
  x &lt;- substitute(x)
  call &lt;-   match.call()[-1]
  fun &lt;- function() {NULL}
  body(fun) &lt;- call$expr
  makeActiveBinding(sym = deparse(x), fun = fun, env = parent.frame())
  invisible(NULL)
}</pre>
<p>
that can be used as:</p>
<pre>mtcars %>% 
  group_by(cyl) %>% 
  summarise(mean_mpg = mean(mpg), sd_mpg = sd(mpg)) %>>% 
  view</pre>
<p>
And again, if I change the values of <code>mpg</code>:</p>
<pre>mtcars$mpg[c(1,3,5)] &lt;- 100</pre>
<p>
The content of <code>view</code> changes accordingly</p>
<pre>view</pre>

<pre>## # A tibble: 3 × 3
##     cyl mean_mpg   sd_mpg
##   &lt;dbl>    &lt;dbl>    &lt;dbl>
## 1     4 33.68182 22.41624
## 2     6 31.02857 30.44321
## 3     8 20.90714 22.88454</pre>
<p>
I believe this operator offers two advantages:</p>
<ul>
<li>Avoids the usage of curly brackets around my <code>dplyr</code> expression</li>
<li>Allows me to actively assign the result of my chain of <code>dplyr</code> statements, in a more <em>natural way</em> at the end of the chain</li>
</ul>
</div>
<p>The post <a target="_blank" rel="nofollow" href="http://www.quantide.com/building-views-with-r/">Building views with R</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.quantide.com/">Quantide – R training &amp; consulting</a>.</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://www.quantide.com/building-views-with-r/"> R blog | Quantide - R training &amp; consulting</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/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="https://www.r-users.com/">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/Efnt83w7bWk"></a></div>