Skip to content

Instantly share code, notes, and snippets.

@yonicd
Last active June 24, 2017 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yonicd/621831b16bb9be374e0e48045366dce0 to your computer and use it in GitHub Desktop.
Save yonicd/621831b16bb9be374e0e48045366dce0 to your computer and use it in GitHub Desktop.
script to run selectize with grep functionality and clean up the created searches from the option list
library(shiny)
shinyApp(
ui = fluidPage(
selectizeInput(inputId = "variable",
label = "Variable:",
choices = split(names(iris),gsub('[_.]',' ',names(iris))),
options=list(multiple=TRUE,create = TRUE)),
checkboxGroupInput(inputId = 'grep',label = 'regex options',
choices = c('Enable'='enbl',
'Retain Searches'='ret',
'ignore.case'='ignore',
'perl'='perl','fixed'='fixed','invert'='invert'),
selected = c('enbl','ignore'),
inline = TRUE),
tableOutput("data")
),
server = function(input, output,session) {
observe({
if(!'ret'%in%input$grep){
updateSelectizeInput(session=session,inputId = 'variable',choices = names(iris),selected = input$variable,options = list(multiple=TRUE,create=TRUE))
}
})
observeEvent(input$variable,{
if('enbl'%in%input$grep){
curr_cols=switch((nchar(input$variable)==0)+1,
grep(input$variable,names(iris),
value=TRUE,
ignore.case = 'ignore'%in%input$grep,
perl = 'perl'%in%input$grep,
fixed='fixed'%in%input$grep,
invert='invert'%in%input$grep),
NULL)
}else{
curr_cols=switch((input$variable%in%names(iris))+1,names(iris),input$variable)
}
if(length(curr_cols)>0){
if(all(curr_cols%in%names(iris))){
output$data <- renderTable({
iris[,curr_cols , drop = FALSE]
}, rownames = TRUE)
}}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment