bstheme global tagging (or shinythemes)

Below is sample code using bslib to use a bootstrap theme. I am passing the class tag to one of the action buttons so it's formatted nicely. But, is there a way to do this more globally as a default for all action buttons so that I don't need to add the class to each one individually?

I am also experimenting with shinythemes instead of bslib and it would be the same issue using that package as well. However, the app loads must faster (locally) with shinythemes compared to bslib.

So, two questions:

  1. How can I establish a global way to pass a class to to bootstrap components (like to an actionButton)
  2. Which package seems to be a better choice, shinythemes or bslib?

library(shiny)
library(bslib)

ui <- fluidPage(

  navbarPage(
    theme = bs_theme(bootswatch = "flatly"),
    title = 'Methods',
    tabPanel('One'),
    tabPanel('Two'),
    tabPanel('Three'),
    tabPanel('Four')
  ),
  mainPanel(
	h1('Hello World'),
	fileInput("file", "Choose CSV File", accept = ".csv"),
	actionButton('aButton', 'Do Stuff'),
	submitButton("Submit"),
	actionButton("primary", "Primary", class = "btn-primary m-2"),
	checkboxInput('aCheckBox', 'Check Me', TRUE)
  )
)

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)

Which package seems to be a better choice, shinythemes or bslib?

bslib can do everything shinythemes can do, and a lot more.

the app loads must faster (locally) with shinythemes compared to bslib.

The performance should be pretty similar with the development version: remotes::install_github("rstudio/bslib") and remotes::install_github("rstudio/shiny"). Let me know if it isn't,

How can I establish a global way to pass a class to to bootstrap components (like to an actionButton)

It'd be helpful to know more about what you're trying to achieve here, but at least one way you could approach is to use a function which hard codes the class

action_button <- function(...) {
  actionButton(..., class = "btn-primary m-2")
}

And for other more complex components where you might want to modify the class of a sub-tag, you could leverage htmltools::tagAppendAttribute(x, .cssSelector = "button") Append tag attributes — tagAppendAttributes • htmltools

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.