<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-a-column-selecter/"></div></div><div><a data-url="https://www.r-bloggers.com/building-a-column-selecter/" data-counturl="https://www.r-bloggers.com/building-a-column-selecter/" data-count="horizontal" data-text="Building a column selecter" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://edwinth.github.io/blog/column-selecter/"> That’s so Random</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Maybe the following sounds familiar. You have a large data set with many, many columns of which the most are irrelevant to you. Typically, a dump from a database or the full set extracted from an API. Several times I found myself the better part of an afternoon going back and forth between a view of the data where I tried to figure out which columns to keep, and an R session where I wrote the code for creating the subset of columns. Wouldn’t it be nice to have an app in which you could just click the columns you would like to keep? This seemed a perfect opportunity to get my feet wet with Shiny gadgets, which I still wanted to do since I first heard about it on useR2016.</p>
<p>I did not really have a feel for how difficult it would be to build something like this. All I knew was that I probably needed <code>DT</code> to show an interactive data frame. Well it proved very easy to build a tool like this, I was just baffled by how easy it was. Due to the fine documentation of the Shiny functionalities, and excellent examples by <a target="_blank" rel="nofollow" href="https://yihui.shinyapps.io/DT-selection/">Yihui Xie</a> and <a target="_blank" rel="nofollow" href="http://shiny.rstudio.com/articles/gadgets.html">Joe Cheng</a>, I had it working before I knew. You have to give it up to these guys from RStudio. When you enable someone with barely any knowledge of JavaScript, CSS and html to build an app in an evening, you certainly are doing something right.</p>
<p>So here is how to do it. You simply run the function <code>col_select</code> on a data frame and the mini ui opens. You click the columns you want to keep and store these in a new object like:</p>
<div>
<pre><code><span>Batting_rel</span><span> </span><span>&lt;-</span><span> </span><span>col_select</span><span>(</span><span>Lahman</span><span>::</span><span>Batting</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="screenshot" src="https://i0.wp.com/edwinth.github.io/images/2016-11-26/col_sel_screenshot.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The downside of applying the function in this way, is its lack of reproducibility. Therefore, it is possible to return the <code>dplyr</code> code that makes the selection, instead of the selection itself. The code will be inserted in the R script. In the next example the second line is inserted by running the app and selecting the first four columns.</p>
<div>
<pre><code><span>col_select</span><span>(</span><span>Lahman</span><span>::</span><span>Batting</span><span>,</span><span> </span><span>ret</span><span> </span><span>=</span><span> </span><span>'dplyr_code'</span><span>)</span><span>
</span><span>Lahman</span><span>::</span><span>Batting</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>playerID</span><span>,</span><span> </span><span>yearID</span><span>,</span><span> </span><span>stint</span><span>,</span><span> </span><span>teamID</span><span>)</span><span>
</span></code></pre>
</div>
<p>Here is the full code for the function. You will notice a third parameter with which you select how many rows are shown in the app. The code can also be found in the <a target="_blank" rel="nofollow" href="https://github.com/edwinth/thatssorandom">R-package that accompanies this blog</a>. The functions from my last post about <a target="_blank" rel="nofollow" href="https://edwinth.github.io/blog/bathroom-with-r/">designing our bathroom with R</a> are now made available here as well.</p>
<div>
<pre><code><span>library</span><span>(</span><span>shiny</span><span>)</span><span>
</span><span>library</span><span>(</span><span>miniUI</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>DT</span><span>)</span><span>
</span><span>col_select</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>df</span><span>,</span><span>
</span><span>ret</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;df_select&quot;</span><span>,</span><span> </span><span>&quot;dplyr_code&quot;</span><span>),</span><span>
</span><span>top_n</span><span> </span><span>=</span><span> </span><span>100</span><span>)</span><span> </span><span>{</span><span>
</span><span>ret</span><span> </span><span>&lt;-</span><span> </span><span>match.arg</span><span>(</span><span>ret</span><span>)</span><span>
</span><span>stopifnot</span><span>(</span><span>is.data.frame</span><span>(</span><span>df</span><span>))</span><span>
</span><span>df_head</span><span> </span><span>&lt;-</span><span> </span><span>head</span><span>(</span><span>df</span><span>,</span><span> </span><span>top_n</span><span>)</span><span>
</span><span>ui</span><span> </span><span>&lt;-</span><span> </span><span>miniPage</span><span>(</span><span>
</span><span>gadgetTitleBar</span><span>(</span><span>&quot;Have your pick&quot;</span><span>),</span><span>
</span><span>miniContentPanel</span><span>(</span><span>
</span><span>dataTableOutput</span><span>(</span><span>&quot;selection_df&quot;</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>&quot;100%&quot;</span><span>)</span><span>
</span><span>)</span><span>
</span><span>)</span><span>
</span><span>server</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>input</span><span>,</span><span> </span><span>output</span><span>,</span><span> </span><span>session</span><span>){</span><span>
</span><span>options</span><span>(</span><span>DT.options</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>pageLength</span><span> </span><span>=</span><span> </span><span>10</span><span>))</span><span>
</span><span>output</span><span>$</span><span>selection_df</span><span> </span><span>&lt;-</span><span> </span><span>renderDataTable</span><span>(</span><span>
</span><span>df_head</span><span>,</span><span> </span><span>server</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>selection</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>target</span><span> </span><span>=</span><span> </span><span>&quot;column&quot;</span><span>)</span><span>
</span><span>)</span><span>
</span><span>observeEvent</span><span>(</span><span>input</span><span>$</span><span>done</span><span>,</span><span> </span><span>stopApp</span><span>(</span><span> </span><span>input</span><span>$</span><span>selection_df_columns_selected</span><span>))</span><span>
</span><span>}</span><span>
</span><span>cols_selected</span><span> </span><span>&lt;-</span><span> </span><span>runGadget</span><span>(</span><span>ui</span><span>,</span><span> </span><span>server</span><span>)</span><span>
</span><span>if</span><span> </span><span>(</span><span>ret</span><span> </span><span>==</span><span> </span><span>&quot;df_select&quot;</span><span>)</span><span> </span><span>{</span><span>
</span><span>return</span><span>(</span><span> </span><span>df</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>cols_selected</span><span>)</span><span> </span><span>)</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>df_name</span><span> </span><span>&lt;-</span><span> </span><span>deparse</span><span>(</span><span>substitute</span><span>(</span><span>df</span><span>))</span><span>
</span><span>colnames_selected</span><span> </span><span>&lt;-</span><span> </span><span>colnames</span><span>(</span><span>df</span><span>)[</span><span>cols_selected</span><span>]</span><span> </span><span>%>%</span><span>
</span><span>paste</span><span>(</span><span>collapse</span><span> </span><span>=</span><span> </span><span>&quot;, &quot;</span><span>)</span><span>
</span><span>rstudioapi</span><span>::</span><span>insertText</span><span>(</span><span>
</span><span>paste</span><span>(</span><span>df_name</span><span>,</span><span> </span><span>&quot; %>% select(&quot;</span><span>,</span><span> </span><span>colnames_selected</span><span>,</span><span> </span><span>&quot;)&quot;</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/building-a-column-selecter/"></div></div><div><a data-url="https://www.r-bloggers.com/building-a-column-selecter/" data-counturl="https://www.r-bloggers.com/building-a-column-selecter/" data-count="vertical" data-text="Building a column selecter" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://edwinth.github.io/blog/column-selecter/"> That’s so Random</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/YBwWBW7WvbY"></a></div>