Hello all,
I am getting stuck for couple days now trying to create a dynamic structure in shiny. What I would like to do is a checkboxGroupInput (with by default all the choices selected) which when one or several choices are deselected, call another checkboxGroupInput, offering the deselected choices from the checkboxGroupInput t-1 to the new one. If you imagine n choices, then there is the possibility that n checkboxGroupInput must be created.
I coded it for 2 iterations using several uiOutput (even if the solution is not suitable) to show you better what I want to do.
library(shiny)
choices0 <- LETTERS[1:10]
ui <- fluidPage(
tagList(
checkboxGroupInput("cbGI_0", label = 0, inline = TRUE,
choices = choices0,
selected = choices0),
uiOutput("cbGI_ui_1")
)
)
server <- function(input, output) {
output$cbGI_ui_1 <- renderUI({
choices <- choices0[!choices0 %in% input$cbGI_0]
if (length(choices) > 0){
tagList(
checkboxGroupInput("cbGI_1", label = 1, inline = TRUE,
choices = choices,
selected = choices),
uiOutput("cbGI_ui_2")
)
}
})
output$cbGI_ui_2 <- renderUI({
choices <- choices0[!choices0 %in% c(input$cbGI_0, input$cbGI_1)]
if (length(choices) > 0){
tagList(
checkboxGroupInput("cbGI_2", label = 2, inline = TRUE,
choices = choices,
selected = choices),
uiOutput("cbGI_ui_3")
)
}
})
#and so on
}
shinyApp(ui = ui, server = server)
The structure of my problem make me think to a kind of recursive function, but the problem is that the reactive dependency between 2 ui coming from the same renderUI seems tricky in shiny. See what I tried for now (without succes)...
library(shiny)
choices0 <- LETTERS[1:10]
ui <- fluidPage(
tagList(
checkboxGroupInput("cbGI_0", label = 0, inline = TRUE,
choices = choices0,
selected = choices0),
uiOutput("cbGI_ui_1")
)
)
server <- function(input, output) {
n <- reactive({
length(sapply(names(input), grepl, pattern = "cbGI_[0-9]*$"))
})
observe({
for (i in seq_len(n())){
output[[paste0("cbGI_ui_", i)]] <- renderUI({
aready_selected <- NULL
for (k in seq_len(i)){
aready_selected <- c(aready_selected, input[[paste0("cbGI_", k-1)]])
}
choices <- choices0[!choices0 %in% aready_selected]
if (length(choices) > 0){
tagList(
checkboxGroupInput(paste0("cbGI_", i), label = i, inline = TRUE,
choices = choices,
selected = choices),
uiOutput(paste0("cbGI_ui_", i+1))
)
}
})
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
Can you guys tell me if what I am trying to do is impossible in shiny or how to do it please ?
Thanks by advance !!