Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accordion - doesnt work on lists (+hotfix) #349

Closed
vladimirstroganov opened this issue Jun 4, 2023 · 3 comments · Fixed by #350
Closed

accordion - doesnt work on lists (+hotfix) #349

vladimirstroganov opened this issue Jun 4, 2023 · 3 comments · Fixed by #350
Labels
fixed-in-dev Fixed in development version

Comments

@vladimirstroganov
Copy link

vladimirstroganov commented Jun 4, 2023

Hi David!

It seems, accordion function doesn't work with list arguments.

Below is an illustrative example and my case-specific hotfix.
I'm sure you'll solve this in much more elegant way! = )

#######

The example is based on the first example in https://rinterface.github.io/bs4Dash/reference/accordion.html

#######


accordion_hotfixed = function (..., id, width = 12) 
{
  items <- list(...)
  
  if(class(items[[1]])!="shiny.tag") items=unlist(items,recursive = F) #hotfix
  
  lapply(seq_along(items), FUN = function(i) {
    items[[i]]$children[[2]]$attribs[["data-parent"]] <<- paste0("#",
                                                                 id)
    items[[i]]$children[[1]]$children[[1]]$children[[1]]$attribs$`data-target` <<- paste0("#collapse_",
                                                                                          id, "_", i)
    items[[i]]$children[[2]]$attribs[["id"]] <<- paste0("collapse_",
                                                        id, "_", i)
  })
  shiny::tags$div(class = if (!is.null(width))
    paste0("col-sm-", width), shiny::tags$div(class = "accordion",
                                              id = id, items))
}

if (interactive()) {
  library(shiny)
  library(bs4Dash)
  
  shinyApp(
    ui = dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody(
        
        # original version:
        
        accordion(
          id = "accordion1",
          accordionItem(
            title = "Accordion 1 Item 1",
            status = "danger",
            collapsed = TRUE,
            "This is some text!"
          ),
          accordionItem(
            title = "Accordion 1 Item 2",
            status = "indigo",
            collapsed = FALSE,
            "This is some text!"
          )
        ),
        
        #new code
        
        # original function, with list argument
        accordion(
          id = "accordion1_broken",
          lapply(1:2,
                 function(i)
                   accordionItem(
                     title = paste('[List arg] Accordion 1 Item', i),
                     status = "danger",
                     collapsed = ifelse(i==1,TRUE, FALSE),
                     "This is some text!"
                   )
          )
        ),
        
        # hotfixed function, with list argument
        accordion_hotfixed(
          id = "accordion1_hotfixed",
          lapply(1:2,
                 function(i)
                   accordionItem(
                     title = paste('[List arg, hotfix] Accordion 1 Item', i),
                     status = "danger",
                     collapsed = ifelse(i==1,TRUE, FALSE),
                     "This is some text!"
                   )
          )
        ),
        
        # end of new code
        
        accordion(
          id = "accordion2",
          accordionItem(
            title = "Accordion 2 Item 1",
            status = "info",
            collapsed = TRUE,
            "This is some text!"
          ),
          accordionItem(
            title = "Accordion 2 Item 2",
            status = "success",
            collapsed = FALSE,
            "This is some text!"
          )
        )
      ),
      title = "Accordion"
    ),
    server = function(input, output) { }
  )
}

@DivadNojnarg
Copy link
Member

Thanks for pointing this. Yes I forgot to align this code with the rest of the package. This below is what I do and this also would work, adding a .list param to pass items as list:

accordion_hotfixed <- function (..., id, width = 12, .list = NULL) 
{
    items <- c(list(...), .list)
   ...
}

DivadNojnarg added a commit that referenced this issue Jun 5, 2023
@DivadNojnarg DivadNojnarg added the fixed-in-dev Fixed in development version label Jun 5, 2023
@DivadNojnarg
Copy link
Member

Just pushed a fix in dev. Thanks again for reporting :)

@vladimirstroganov
Copy link
Author

Amazing! Thank you)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed-in-dev Fixed in development version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants