<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/regular-expression-searching-within-shiny-selectize-objects/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://yonicd.github.io/2017-09-26-regexSelect/"> Jonathan Sidi's R Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>regexSelect is a small package that uses <a rel="nofollow" href="https://shiny.rstudio.com/articles/modules.html" target="_blank">Shiny modules</a> to solve a problem in Shiny selectize objects – <a rel="nofollow" href="https://www.rstudio.com/wp-content/uploads/2016/09/RegExCheatsheet.pdf" target="_blank">regular expression</a> (regex) searching. You can quickly filter the values in the selectize object, while being able to add that new regex query to the selectize list.</p>
<p>This is great for long lists, since you can return multiple item simultaneously without needing to endlessly click items in a list!</p>
<h2>Install</h2>
<div>
<pre><code><span>install.packages</span><span>(</span><span>'regexSelect'</span><span>)</span><span>
</span><span>#devtools::install_github('yonicd/regexSelect')
</span></code></pre>
</div>
<p>Below are two examples of using regular expressions to quickly populate multiple items in a <code>ggplot</code> and a <code>datatable</code>.</p>
<h2>regexSelect with Plots</h2>
<p><img alt="" src="https://i0.wp.com/raw.githubusercontent.com/yonicd/regexSelect/master/Miscellaneous/gifs/regexSelectPlot.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The shiny module works with two main functions:</p>
<div>
<pre><code><span># server side: </span><span> </span><span>callModule</span><span>(</span><span>module</span><span>=</span><span>regexSelect</span><span>,</span><span> </span><span>id</span><span>=</span><span>'myId'</span><span>,</span><span> </span><span>reactive</span><span>(</span><span></span><span>selectizeInput</span><span> </span><span>Choices</span><span>></span><span>))</span><span>
</span><span># ui side: </span><span> </span><span>regexSelectUI</span><span>(</span><span>id</span><span> </span><span>=</span><span> </span><span>&quot;myId&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>'myLabel'</span><span>,</span><span> </span><span>choices</span><span> </span><span>=</span><span> </span><span></span><span>selectizeInput</span><span> </span><span>Choices</span><span>></span><span>)</span><span>
</span></code></pre>
</div>
<p>regexSelect comes with controls placed in a group checkbox below the selectize object. When calling regexSelect you can show or hide the checkbox controls (hidden by default), as to make it look like a normal selectize object, and save valuable UI real estate.</p>
<p>While the shiny app is running regexSelect properties can be manipulated through the checkbox controls giving greater flexibilty to:</p>
<ul>
<li>Toggle regexSelect to work as a standard selectize object.</li>
<li>Retain the regex search as a new value the selectize object.</li>
<li>Change arguments that are passed to <a rel="nofollow" href="https://www.rdocumentation.org/packages/base/versions/3.4.1/topics/grep?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">base::grep</a> : <code>ignore.case</code>, <code>perl</code>, <code>fixed</code>, <code>invert</code>.</li>
</ul>
<div>
<pre><code><span>library</span><span>(</span><span>shiny</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>ui</span><span> </span><span>&lt;-</span><span> </span><span>fluidPage</span><span>(</span><span>
</span><span>selectInput</span><span>(</span><span>'var'</span><span>,</span><span>
</span><span>'Choose Variable'</span><span>,</span><span>
</span><span>choices</span><span> </span><span>=</span><span> </span><span>names</span><span>(</span><span>diamonds</span><span>)[</span><span>sapply</span><span>(</span><span>diamonds</span><span>,</span><span>function</span><span>(</span><span>x</span><span>){</span><span>
</span><span>inherits</span><span>(</span><span>x</span><span>,</span><span>c</span><span>(</span><span>'character'</span><span>,</span><span>'factor'</span><span>)))]</span><span>
</span><span>},</span><span>
</span><span>selected</span><span> </span><span>=</span><span> </span><span>'clarity'</span><span>),</span><span>
</span><span>uiOutput</span><span>(</span><span>'regexchoose'</span><span>),</span><span>
</span><span>plotOutput</span><span>(</span><span>&quot;data&quot;</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>{</span><span>
</span><span>output</span><span>$</span><span>regexchoose</span><span>&lt;-</span><span>shiny</span><span>::</span><span>renderUI</span><span>({</span><span>
</span><span>regexSelectUI</span><span>(</span><span>id</span><span> </span><span>=</span><span> </span><span>&quot;a&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>input</span><span>$</span><span>var</span><span>,</span><span>
</span><span>choices</span><span> </span><span>=</span><span> </span><span>unique</span><span>(</span><span>diamonds</span><span>[[</span><span>input</span><span>$</span><span>var</span><span>]]),</span><span> </span><span>checkbox.show</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>})</span><span>
</span><span>observeEvent</span><span>(</span><span>input</span><span>$</span><span>var</span><span>,{</span><span>
</span><span>curr_cols</span><span> </span><span>&lt;-</span><span> </span><span>callModule</span><span>(</span><span>module</span><span> </span><span>=</span><span> </span><span>regexSelect</span><span>,</span><span> </span><span>id</span><span> </span><span>=</span><span> </span><span>&quot;a&quot;</span><span>,</span><span>
</span><span>shiny</span><span>::</span><span>reactive</span><span>(</span><span>unique</span><span>(</span><span>diamonds</span><span>[[</span><span>input</span><span>$</span><span>var</span><span>]]))</span><span>
</span><span>)</span><span>
</span><span>observeEvent</span><span>(</span><span>curr_cols</span><span>(),{</span><span>
</span><span>cols_now</span><span> </span><span>&lt;-</span><span> </span><span>curr_cols</span><span>()</span><span>
</span><span>output</span><span>$</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>shiny</span><span>::</span><span>renderPlot</span><span>({</span><span>
</span><span>ggplot</span><span>(</span><span>diamonds</span><span>[</span><span>diamonds</span><span>[[</span><span>input</span><span>$</span><span>var</span><span>]]</span><span>%in%</span><span>cols_now</span><span>,],</span><span>
</span><span>aes_string</span><span>(</span><span>x</span><span>=</span><span>'table'</span><span>,</span><span>y</span><span>=</span><span>'carat'</span><span>,</span><span>colour</span><span>=</span><span>input</span><span>$</span><span>var</span><span>))</span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span>
</span><span>})</span><span>
</span><span>})</span><span> </span><span>})</span><span>
</span><span>}</span><span>
</span><span>shinyApp</span><span>(</span><span>ui</span><span>,</span><span> </span><span>server</span><span>)</span><span>
</span></code></pre>
</div>
<h2>regexSelect with Tables</h2>
<p><img alt="" src="https://i2.wp.com/raw.githubusercontent.com/yonicd/regexSelect/master/Miscellaneous/gifs/regexSelectTable.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code><span>ui</span><span> </span><span>&lt;-</span><span> </span><span>shiny</span><span>::</span><span>fluidPage</span><span>(</span><span>
</span><span>regexSelectUI</span><span>(</span><span>id</span><span> </span><span>=</span><span> </span><span>&quot;a&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;Variable:&quot;</span><span>,</span><span>
</span><span>choices</span><span> </span><span>=</span><span> </span><span>names</span><span>(</span><span>iris</span><span>)</span><span>
</span><span>),</span><span>
</span><span>shiny</span><span>::</span><span>tableOutput</span><span>(</span><span>&quot;data&quot;</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>{</span><span>
</span><span>curr_cols</span><span> </span><span>&lt;-</span><span> </span><span>callModule</span><span>(</span><span>module</span><span> </span><span>=</span><span> </span><span>regexSelect</span><span>,</span><span> </span><span>id</span><span> </span><span>=</span><span> </span><span>&quot;a&quot;</span><span>,</span><span>
</span><span>shiny</span><span>::</span><span>reactive</span><span>(</span><span>names</span><span>(</span><span>iris</span><span>))</span><span>
</span><span>)</span><span>
</span><span>observeEvent</span><span>(</span><span>curr_cols</span><span>(),{</span><span>
</span><span>cols_now</span><span> </span><span>&lt;-</span><span> </span><span>curr_cols</span><span>()</span><span>
</span><span>if</span><span>(</span><span> </span><span>length</span><span>(</span><span>cols_now</span><span>)</span><span>==</span><span>0</span><span> </span><span>)</span><span> </span><span>cols_now</span><span> </span><span>&lt;-</span><span> </span><span>names</span><span>(</span><span>iris</span><span>)</span><span>
</span><span>output</span><span>$</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>shiny</span><span>::</span><span>renderTable</span><span>({</span><span>
</span><span>iris</span><span>[,</span><span>cols_now</span><span> </span><span>,</span><span> </span><span>drop</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>]</span><span>
</span><span>},</span><span> </span><span>rownames</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>})</span><span>
</span><span>}</span><span>
</span><span>shiny</span><span>::</span><span>shinyApp</span><span>(</span><span>ui</span><span>,</span><span> </span><span>server</span><span>)</span><span>
</span></code></pre>
</div>
	<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://yonicd.github.io/2017-09-26-regexSelect/"> Jonathan Sidi's R Blog</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/tlPIQOLQ8ZQ" height="1"></a></div>