I am trying to make a dynamic UI in which some tabPanel
and checkboxGroup
are created dynamically depending on the data.
Below an example data frame:
df <- data.frame(
"Group" = c("Group A", "Group B", "Group A", "Group A", "Group B"),
"Name" = c("Bob", "Paul", "Peter", "Emma", "John"),
"Value" = seq(1,10, length.out=5),
stringsAsFactors = F
)
df
Group Name Value
1 Group A Bob 1.00
2 Group B Paul 3.25
3 Group A Peter 5.50
4 Group A Emma 7.75
5 Group B Jhon 10.00
I managed to create two tabPanel
called "Group A" and "Group B" according to the unique values in column "Group" of my data frame. I can also create a checkboxGroupInput
based on the unique values of column "Name" for each group.
However, I don't understand where to place the usual server block to output a table subsetted per Group and the values checked in the box. None of the similar discussions I saw can help with this particular situation.
See my attempt below:
library(shiny)
library(DT)
# UI
ui <- fluidPage(
mainPanel(
uiOutput('mytabs')
)
)
# SERVER
server <- function(input, output, session) {
output$mytabs <- renderUI({
Tabs_titles = unique(df$Group)
do.call(tabsetPanel,
lapply(Tabs_titles,
function(x){
tabPanel(title = x,
checkboxGroupInput(inputId = "checkboxID",
label = "My Checkbox",
choices = df %>% subset(Group == x) %>% pull(Name),
selected = df %>% subset(Group == x) %>% pull(Name)
),
DT::dataTableOutput("my_Table")
)
}
)
)
})
### Where to place this 'usual' server code below? ###
# Observe box values when changed
box_values = reactive({input$checkboxID})
# Output table
output$my_Table <- DT::renderDataTable({
subset(df, Group = <cannot catch the variable 'x' from above>, Name = box_values)
})
}
shinyApp(ui, server)
Any explanation would be greatly appreciated.