I want to build a shiny app that allows the user to select some columns to filter a data.table.
My real data has ~110 columns and the columns are numeric, character, factor, integer
I want to have a pre-selected filter in the sidebar panel but also to have a + button to allow the user to create custom filters based on the columns. I don't know if this can be done in shiny or not, I have read about insertUI and removeUI but I don't know if this could be applied to this case. Also the user-created filters should be applied consecutively, i.e, if user creates three filters, then filter1 should be applied, then filter2, and then filter3.
I have this little example app where there is an initial filter based on Person using textAreaInput (my final user would like to paste some names on the box to filter out the table) but I would like to add some another filters, for example a sliderInput for votes or a dropdownMenu for letters.
library(shinydashboard)
library(dplyr)
library(shiny)
library(DT)
header <- dashboardHeader(title="Analysis and database")
sidebar <- dashboardSidebar(
sidebarMenu(
# Setting id makes input$tabs give the tabName of currently-selected tab
id = "sidebarmenu",
menuItem("Database", tabName="db"),
menuItem("Search by Name", tabName = "Filt_table"),
textAreaInput("name_", "Name")
)
)
body <- dashboardBody(
tabItems(
tabItem("db","table content",
fluidRow(DT::dataTableOutput('tabla'))),
tabItem("Filt_table","Filtered table content",
fluidRow(DT::dataTableOutput('tablafilt')))
)
)
ui <- dashboardPage(header, sidebar, body)
### SERVER SIDE
server = function(input, output, session) {
my_data <- data.frame(Person=c("Anne", "Pete", "Rose", "Julian", "Tristan", "Hugh"),
Votes=c(10,25,56,89.36,78,1500),
Stuff=c("test|3457678", "exterm|4567sdf", "1001(hom);4.3.4|3456", "xdfrtg", "1234|trsef|456(het)", "hyggas|tertasga"),
letters=replicate(6, paste(sample(LETTERS,6, replace=T), collapse="")))
output$tabla <- DT::renderDataTable({
DT::datatable(my_data)
})
filtered <- reactive({
if(is.null(input$name_))
return()
glist <- isolate(input$name_)
filter(my_data, Person %in% glist)
})
output$tablafilt <- DT::renderDataTable({
if(is.null(input$name_))
return()
DT::datatable(filtered (),
filter = 'top',
extensions = 'Buttons',
options = list(
dom = 'Blftip',
buttons =
list('colvis', list(
extend = 'collection',
buttons = list(list(extend='csv',
filename = 'results'),
list(extend='excel',
filename = 'results'),
list(extend='pdf',
filename= 'results')),
text = 'Download'
)),
scrollX = TRUE,
pageLength = 5,
lengthMenu = list(c(5, 15, -1), list('5', '15', 'All'))
), rownames = FALSE
)
})
}
shinyApp(ui, server)