Use local bootstrap theme with R Markdown using bslib

I want to load a local bootstrap theme with bslib. Note: This continues a discussion on github. Essentially what I want to achieve is for a graphical designer to make our own custom Bootstrap theme and then load it.

It was suggested on github that I first package the files together with sass_layer():

library(sass)
my_theme <- sass_layer(
   functions = sass_file("functions.scss"),
   defaults = sass_file("variables.scss"),
   mixins = sass_file("mixins.scss"),
   rules = sass_file("rules.scss"),
)

Then you can add this custom theme to a bslib theme like so:

library(bslib)
bs_bundle(bs_theme(), my_theme)

I have two follow-up questions:

  1. How do I set this custom theme in R Markdown? Do I call it explicitly in YAML options like:
    theme: !expr bslib::bs_bundle(bslib::bs_theme(), get_my_theme())
  1. I want to copy the files from a Bootswatch theme (I'm on BS4 at the moment) and then start from there. However for a Bootswatch theme I do not find all the files you mention above: functions.scss, variables.scss, mixins.scss, rules.scss. For example, for BS4 "Flatly" I only find the following files: _bootswatch.scss, _variables.scss, font.css. Given the files found, how do I instantiate my own bs_theme using them?

I figured it out myself after digging into bslib.

Answer to #1:

Yes, use theme: !expr getMyCustomTheme()

Answer to #2:

Copy the Bootswatch theme you want to fork. You can get the filepaths by calling bslib::bootswatch_themes(4, TRUE)). Then do:

getMyCustomTheme <- function() {
  bs_bundle(bs_theme(4),
            sass_layer(
              file_attachments = c(fonts.css = normalizePath("theme/flatly/font.css")),
              defaults = list(sass_file("theme/flatly/_variables.scss")),
              rules = list(sass_file("theme/flatly/_bootswatch.scss")))
  )
}

This topic was automatically closed 7 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.