ijlyttle / bsplus

Shiny and R Markdown addons to Bootstrap 3
http://ijlyttle.github.io/bsplus/
Other
146 stars 23 forks source link

How can I create bsplus accordions using apply functions? #48

Closed askhari139 closed 6 years ago

askhari139 commented 6 years ago

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:

library(shiny)
library(shinydashboard)
library(bsplus)
library(datasets)

facts <- colnames(CO2)[sapply(colnames(CO2), is.character)]

# Define UI for application 
ui <- fluidPage(
   #What I want to do
    # bs_accordion(id = "test"),
    # lapply(facts, function(x){
    #     bs_append(tag = "test", title = x, ccheckboxGroupInput("t", "", choices = unique(CO2[[x]])))
    # })
    # 
    ##What works
    bs_accordion(id = "test1")%>%
        bs_append(title = facts[1], checkboxGroupInput("t1", "", choices = unique(CO2[[facts[1]]])))%>%
        bs_append(title = facts[2], checkboxGroupInput("t2", "", choices = unique(CO2[[facts[2]]])))%>%
        bs_append(title = facts[3], checkboxGroupInput("t3", "", choices = unique(CO2[[facts[3]]])))
        #bs_append(title = facts[2], content = list(unique(CO2[[facts[2]]]))) - gives a warning: 
    #     in charToRaw(enc2utf8(text)) :
    #     argument should be a character vector of length 1
    # all but the first element will be ignored
)

# Define server logic 
server <- function(input, output) {

}

# Run the application 
shinyApp(ui = ui, server = server)
ijlyttle commented 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.

ijlyttle commented 6 years ago

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)