I am developing a data selection component for my shiny App.
The input is a data.frame.
Then there is a dynamic data selection UI (implemented by renderUI() ) for users to choose data by different columns.
By default, I would expect the data are completely selected thus I set the selected parameters of checkboxGroupInput() as all values.
However, since the reactive expression is lazy evaluated , the data table will be really completed only after every UI component are rendered by renderUI(). That means even though I know by default all rows are selected, I still need to click through the selectInput() choices for initializing the values which will be provided by renderUI.
I am wondering that is what is the best practice to implement such data selection component UI in shiny ?
The run example code is here:
library(dplyr)
library(shiny)
set.seed(319)
df <- data.frame(A = sample(c("aa", "ab", "ac"), 100, T),
B = sample(c("ba", "bb", "bc"), 100, T),
C = sample(c("ca", "cb", "cc"), 100, T))
ui <- fluidPage(
titlePanel("Dynamically generated user interface components"),
fluidRow(column(12,
selectInput("cellsVars",
label = "Cell Attributes",
choices = c("A", "B", "C")),
uiOutput("cellsCheckBox")
),
fluidRow(column(12,
dataTableOutput("table"))
)
)
)
server <- function(input, output) {
output$cellsCheckBox <- renderUI({
if(is.null(input$cellsVars) ) return()
switch(input$cellsVars,
"A" = wellPanel(
checkboxGroupInput("A", label = "Donors",
choices = c("aa", "ab", "ac"),
selected = c("aa", "ab", "ac") )
),
"B" = wellPanel(
checkboxGroupInput("B", label = "Tissue",
choices = c("ba", "bb", "bc"),
selected = c("ba", "bb", "bc"))
),
"C" = wellPanel(
checkboxGroupInput("C", label = "Annotated Cell Type",
choices = c("ca", "cb", "cc"),
selected = c("ca", "cb", "cc"))
)
)
})
output$table <- renderDataTable({
filtered <- df %>% filter( (A %in% input$donors) & (B %in% input$tissueType) & (C %in% input$cellType))
filtered
})
}
shinyApp(ui, server)
Any comments/advice are welcome. Thank you