Quantcast
Channel: R-bloggers
Viewing all articles
Browse latest Browse all 12094

Regular Expression Searching within Shiny Selectize Objects

$
0
0

(This article was first published on Jonathan Sidi's R Blog, and kindly contributed to R-bloggers)

regexSelect is a small package that uses Shiny modules to solve a problem in Shiny selectize objects – regular expression (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.

This is great for long lists, since you can return multiple item simultaneously without needing to endlessly click items in a list!

Install

install.packages('regexSelect')#devtools::install_github('yonicd/regexSelect')

Below are two examples of using regular expressions to quickly populate multiple items in a ggplot and a datatable.

regexSelect with Plots

The shiny module works with two main functions:

# server side: callModule(module=regexSelect,id='myId',reactive(<selectizeInputChoices>))# ui side: regexSelectUI(id="myId",label='myLabel',choices=<selectizeInputChoices>)

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.

While the shiny app is running regexSelect properties can be manipulated through the checkbox controls giving greater flexibilty to:

  • Toggle regexSelect to work as a standard selectize object.
  • Retain the regex search as a new value the selectize object.
  • Change arguments that are passed to base::grep : ignore.case, perl, fixed, invert.
library(shiny)library(ggplot2)ui<-fluidPage(selectInput('var','Choose Variable',choices=names(diamonds)[sapply(diamonds,function(x){inherits(x,c('character','factor')))]},selected='clarity'),uiOutput('regexchoose'),plotOutput("data"))server<-function(input,output,session){output$regexchoose<-shiny::renderUI({regexSelectUI(id="a",label=input$var,choices=unique(diamonds[[input$var]]),checkbox.show=TRUE)})observeEvent(input$var,{curr_cols<-callModule(module=regexSelect,id="a",shiny::reactive(unique(diamonds[[input$var]])))observeEvent(curr_cols(),{cols_now<-curr_cols()output$data<-shiny::renderPlot({ggplot(diamonds[diamonds[[input$var]]%in%cols_now,],aes_string(x='table',y='carat',colour=input$var))+geom_point()})})})}shinyApp(ui,server)

regexSelect with Tables

ui<-shiny::fluidPage(regexSelectUI(id="a",label="Variable:",choices=names(iris)),shiny::tableOutput("data"))server<-function(input,output,session){curr_cols<-callModule(module=regexSelect,id="a",shiny::reactive(names(iris)))observeEvent(curr_cols(),{cols_now<-curr_cols()if(length(cols_now)==0)cols_now<-names(iris)output$data<-shiny::renderTable({iris[,cols_now,drop=FALSE]},rownames=TRUE)})}shiny::shinyApp(ui,server)
var vglnk = { key: '949efb41171ac6ec1bf7f206d57e90b8' }; (function(d, t) {var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;s.src = '//cdn.viglink.com/api/vglnk.js';var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r); }(document, 'script'));

To leave a comment for the author, please follow the link and comment on their blog: Jonathan Sidi's R Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...


Viewing all articles
Browse latest Browse all 12094

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>