Closed askhari139 closed 6 years ago
I think that purrr's reduce()
function might be useful here.
I will try to make up an example a little later today.
I got this to work - it turns out to be a neat exercise in purrr
library(shiny)
library(shinydashboard)
library(bsplus)
library(datasets)
library(purrr)
# create list:
# each element contains levels for each column
# of the data-frame that is a factor
CO2_factor_levels <- map_if(CO2_factors, is.factor, levels)
# create function:
# given a tag (initiated with bs_accordion()), a name and a set of levels,
# append to the tag, an accordion-panel with:
# - a title
# - a checkboxGroupInput with choices, identified by the name
fn_append <- function(tag, title, choices) {
bsplus::bs_append(
tag,
title = title,
content = checkboxGroupInput(
inputId = paste0("t-", title),
label = NULL,
choices = choices
)
)
}
# Define UI for application
ui <- fluidPage(
# starting with an initial accordon-tag, append panels according to
# list of factor levels
reduce2(
names(CO2_factor_levels),
CO2_factor_levels,
fn_append,
.init = bs_accordion(id = "test2")
)
)
# Define server logic
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)
I have to create an accordion list made up of the column names in a dataframe and the content of each accordion as the factor levels of the column. I can list individual append statements for each column and work with it but that is too tedious and is not practical if I don't have any knowledge about the dataframe. Is there any way to do this using some kind of a loop? The code I wrote for this is below: