Search code examples
htmlrr-markdownaccordionbootstrap-accordion

Integrating R outputs in bsplus functions


I consider bsplus package as relevant when developing dynamic webs. I use R Markdown in Rstudio.

However, I find particularly tricky the way to integrate bsplus functions with R outputs.

Let's see an example with the bs_accordion function, using mtcars dataset

head <- head(mtcars)
tail <- tail(mtcars)

bs_accordion(id ="Data: mtcars") %>%
  bs_append(title = "Head of mtcars", content = head) %>%
  bs_append(title = "Tail of mtcars", content = tail)

I would like to display R outputs in the accordion function, displaying the data frames head and tail.

Now, it only displays the first numerical row in the head.

Is there any possibility to include R code within the content attribute in the bsplus functions?

In this way we could be able to display R results in a dynamic way.


Solution

  • This should work for your example. You have to create a datatable somehow, just including it wont render it as a table.

    Note: I changed the id of the accordion to Data-mtcars. Using a whitespace, ":" or ";" will disable the collapsing.

    library(shiny)
    library(bsplus)
    library(DT)
    
    ui <- fluidPage(
      bs_accordion(id ="Data-mtcars") %>%
        bs_set_opts(panel_type = "primary", use_heading_link = T) %>%
        bs_append(title = "Head of mtcars", content = DT::dataTableOutput("table1")) %>%
    
        bs_set_opts(panel_type = "primary", use_heading_link = T) %>%
        bs_append(title = "Tail of mtcars", content = DT::dataTableOutput("table2"))
    )
    
    server <- function(input, output) {
    
      output$table1 <- DT::renderDataTable({
        head
      })  
      output$table2 <- DT::renderDataTable({
        tail
      })
    }
    
    shinyApp(ui, server)