Package 'brand.yml'

Title: Unified Branding with a Simple YAML File
Description: Read and process 'brand.yml' 'YAML' files. 'brand.yml' is a simple, portable 'YAML' file that codifies your company's brand guidelines into a format that can be used by 'Quarto', 'Shiny' and 'R' tooling to create branded outputs. Maintain unified, branded theming for web applications to printed reports to dashboards and presentations with a consistent look and feel.
Authors: Garrick Aden-Buie [aut, cre] (ORCID: <https://orcid.org/0000-0002-7111-0077>), Posit Software, PBC [cph, fnd] (ROR: <https://ror.org/03wc8by49>)
Maintainer: Garrick Aden-Buie <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0.9000
Built: 2026-05-21 19:02:15 UTC
Source: https://github.com/posit-dev/brand-yml

Help Index


Create a Brand instance from a list or character vector.

Description

Create a Brand instance from a list or character vector.

Usage

as_brand_yml(brand)

Arguments

brand

A list or string of YAML representing the brand, or a path to a brand.yml file.

Value

A normalized brand_yml list.

Examples

as_brand_yml("
meta:
  name: Example Brand

color:
  primary: '#FF5733'
  secondary: '#33FF57'
")

as_brand_yml(list(
  meta = list(name = "Example Brand"),
  color = list(primary = "#FF5733", secondary = "#33FF57")
))

Extract a color value from a brand object

Description

Safely extracts a color value from a brand object based on the provided key. This function handles color references and resolves them, including references to palette colors and other theme colors. It detects and prevents cyclic references.

Usage

brand_color_pluck(brand, key)

Arguments

brand

A brand object created by read_brand_yml() or as_brand_yml().

key

A character string representing the color key to extract.

Details

The function checks for the color key in both the main color theme and the color palette. It can resolve references between colors (e.g., if "primary" references "palette.blue"). If a cyclic reference is detected (e.g., A references B which references A), the function will throw an error.

Value

The resolved color value (typically a hex color code) if the key exists, otherwise returns the key itself.

See Also

Other brand.yml helpers: brand_has(), brand_pluck(), with_brand_yml_path()

Examples

brand <- as_brand_yml(list(
  color = list(
    primary = "blue",
    secondary = "info",
    info = "light-blue",
    palette = list(
      blue = "#004488",
      light_blue = "#c3ddff"
    )
  )
))

# Extract the primary color
brand_color_pluck(brand, "primary") # "#004488"

# Extract a color that references another color
brand_color_pluck(brand, "info") # "#c3ddff"

# Extract a color that references another color
# which in turn references the palette
brand_color_pluck(brand, "secondary") # "#c3ddff"

# Extract a color that isn't defined
brand_color_pluck(brand, "green") # "green"

# Use brand_pluck() if you need direct (resolved) values
brand_pluck(brand, "color", "primary") # "#004488"
brand_pluck(brand, "color", "info") # "#c3ddff"
brand_pluck(brand, "color", "green") # NULL

Check if a brand has a specific nested element

Description

Checks if a given brand object has a specific nested element accessible via the additional arguments provided as key paths.

Usage

brand_has(brand, ...)

Arguments

brand

A brand object created by read_brand_yml() or as_brand_yml().

...

One or more character strings or symbols representing the path to the nested element.

Value

TRUE if the nested element exists in the brand object, FALSE otherwise.

See Also

Other brand.yml helpers: brand_color_pluck(), brand_pluck(), with_brand_yml_path()

Examples

brand <- as_brand_yml(list(
  meta = list(name = "Example Brand"),
  color = list(primary = "#FF5733")
))

# Check if brand has a primary color
brand_has(brand, "color", "primary") # TRUE

# Check if brand has a secondary color
brand_has(brand, "color", "secondary") # FALSE

Extract a nested element from a brand object

Description

Safely extracts a nested element from a brand object using the provided key path. Returns NULL if the element doesn't exist.

Usage

brand_pluck(brand, ...)

Arguments

brand

A brand object created by read_brand_yml() or as_brand_yml().

...

One or more character strings or symbols representing the path to the nested element.

Value

The value of the nested element if it exists, NULL otherwise.

See Also

Other brand.yml helpers: brand_color_pluck(), brand_has(), with_brand_yml_path()

Examples

brand <- as_brand_yml(list(
  meta = list(name = "Example Brand"),
  color = list(primary = "#FF5733")
))

# Extract the primary color
brand_pluck(brand, "color", "primary") # "#FF5733"

# Try to extract a non-existent element
brand_pluck(brand, "color", "secondary") # NULL

Generate Sass variables for brand colors

Description

Creates Sass variables for brand colors with the brand_color_ prefix. Excludes the color palette which is handled by brand_sass_color_palette().

Usage

brand_sass_color(brand)

Arguments

brand

A list or string of YAML representing the brand, or a path to a brand.yml file.

Value

A list with one component:

  • defaults: Sass variable definitions with !default flag

See Also

Other brand.yml Sass helpers: brand_sass_color_palette(), brand_sass_defaults_bootstrap(), brand_sass_fonts(), brand_sass_typography()

Examples

brand <- list(
  color = list(
    primary = "#007bff",
    danger = "#dc3545"
  )
)

brand_sass_color(brand)

Generate Sass variables and CSS custom properties for brand color palette

Description

Converts color palette entries from a brand object to Sass variables with ⁠brand-⁠ prefix and CSS custom properties with ⁠--brand-⁠ prefix.

Usage

brand_sass_color_palette(brand)

Arguments

brand

A list or string of YAML representing the brand, or a path to a brand.yml file.

Value

A list with two components:

  • defaults: Sass variable definitions with !default flag

  • rules: CSS rules that define custom properties in ⁠:root⁠

See Also

Other brand.yml Sass helpers: brand_sass_color(), brand_sass_defaults_bootstrap(), brand_sass_fonts(), brand_sass_typography()

Examples

brand <- list(
  color = list(
    palette = list(
      primary = "#007bff",
      secondary = "#6c757d"
    )
  )
)

brand_sass_color_palette(brand)

Generate Sass variables and layer for Bootstrap defaults

Description

Creates Sass variables and a sass layer from Bootstrap defaults defined in the brand object. Allows overriding defaults from other sources like Shiny themes.

Usage

brand_sass_defaults_bootstrap(brand, overrides = "shiny.theme")

Arguments

brand

A list or string of YAML representing the brand, or a path to a brand.yml file.

overrides

Path to override defaults, e.g., "shiny.theme"

Value

A list with two components:

  • defaults: Sass variable definitions with !default flag

  • layer: A sass_layer object with functions, mixins, and rules

See Also

Other brand.yml Sass helpers: brand_sass_color(), brand_sass_color_palette(), brand_sass_fonts(), brand_sass_typography()

Examples

brand <- list(
  defaults = list(
    bootstrap = list(
      defaults = list(
        primary = "#007bff",
        enable_rounded = TRUE
      ),
      functions = "@function brand-function() { @return true; }"
    ),
    shiny = list(
      theme = list(
        defaults = list(
          primary = "#428bca"  # Override bootstrap primary
        )
      )
    )
  )
)

brand_sass_defaults_bootstrap(brand)

Generate Sass variables and CSS rules for brand fonts

Description

Creates Sass variables and CSS rules for fonts defined in the brand object. Supports Google fonts, Bunny fonts, and file-based fonts.

Usage

brand_sass_fonts(brand)

Arguments

brand

A list or string of YAML representing the brand, or a path to a brand.yml file.

Value

A list with two components:

  • defaults: Sass variables for font definitions

  • rules: CSS rules for applying fonts via classes

See Also

Other brand.yml Sass helpers: brand_sass_color(), brand_sass_color_palette(), brand_sass_defaults_bootstrap(), brand_sass_typography()

Examples

brand <- list(
  typography = list(
    fonts = list(
      list(
        family = "Roboto",
        source = "google",
        weight = c(400, 700),
        style = "normal"
      )
    )
  )
)

brand_sass_fonts(brand)

Generate Sass variables for brand typography

Description

Creates Sass variables for typography settings with the brand_typography_ prefix. Font size values in pixels are converted to rem units, and color references are resolved.

Usage

brand_sass_typography(brand)

Arguments

brand

A list or string of YAML representing the brand, or a path to a brand.yml file.

Value

A list with one component:

  • defaults: Sass variable definitions with !default flag

See Also

Other brand.yml Sass helpers: brand_sass_color(), brand_sass_color_palette(), brand_sass_defaults_bootstrap(), brand_sass_fonts()

Examples

brand <- list(
  typography = list(
    base = list(
      size = "16px",
      "line-height" = 1.5
    ),
    headings = list(
      weight = "bold",
      style = "normal"
    )
  )
)

brand_sass_typography(brand)

Create a Brand instance from a Brand YAML file.

Description

Reads a Brand YAML file or finds and reads a ⁠_brand.yml⁠ file and returns a validated Brand instance.

Usage

read_brand_yml(path = NULL)

Arguments

path

The path to the brand.yml file or a directory where ⁠_brand.yml⁠ is expected to be found.

Details

By default, read_brand_yml() finds a project-specific ⁠_brand.yml⁠ file, by looking in the current working directory directory or any parent directory for a ⁠_brand.yml⁠, ⁠brand/_brand.yml⁠ or ⁠_brand/_brand.yml⁠ file (or the same variants with a .yaml extension). When path is provided, read_brand_yml() looks for these files in the provided directory; for automatic discovery, read_brand_yml() starts the search in the working directory and moves upward to find the ⁠_brand.yml⁠ file.

Value

A normalized brand_yml list from the brand.yml file.

References

https://posit-dev.github.io/brand-yml/

Examples

# For this example: copy a brand.yml to a temporary directory
tmp_dir <- tempfile()
dir.create(tmp_dir)
file.copy(
  system.file("examples/brand-posit.yml", package = "brand.yml"),
  file.path(tmp_dir, "_brand.yml")
)

brand <- read_brand_yml(tmp_dir)

Create a flextable theme using brand colors

Description

Apply brand colors to a flextable table.

Usage

theme_brand_flextable(
  table,
  brand = NULL,
  background = NULL,
  foreground = NULL
)

Arguments

table

A flextable object to theme.

brand

One of:

  • NULL (default): Automatically detect and read a _brand.yml file

  • A path to a brand.yml file or directory containing _brand.yml

  • A brand object (as returned by read_brand_yml() or as_brand_yml())

  • FALSE: Don't use a brand file; explicit colors must be provided

background

The background color, defaults to brand.color.background. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

foreground

The foreground color, defaults to brand.color.foreground. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

Value

Returns a themed flextable object.

See Also

Other branded theming functions: theme_brand_ggplot2(), theme_brand_gt(), theme_brand_plotly(), theme_brand_thematic()

Examples

brand <- as_brand_yml('
color:
  palette:
    black: "#1A1A1A"
    white: "#F9F9F9"
    orange: "#FF6F20"
  foreground: black
  background: white
  primary: orange')

library(flextable)
theme_brand_flextable(
  flextable(head(palmerpenguins::penguins)),
  brand
)


brand <- as_brand_yml('
color:
  palette:
    black: "#1A1A1A"
    white: "#F9F9F9"
    orange: "#FF6F20"
  foreground: black
  background: white
  primary: orange')

library(flextable)
theme_brand_flextable(
  flextable(head(mtcars)),
  brand
)

Create a ggplot2 theme using brand colors

Description

Create a ggplot2 theme using explicit colors or by automatically extracting colors from a brand.yml file.

Usage

theme_brand_ggplot2(
  brand = NULL,
  background = NULL,
  foreground = NULL,
  accent = NULL,
  ...,
  base_size = 11,
  title_size = base_size * 1.2,
  title_color = NULL,
  line_color = NULL,
  rect_fill = NULL,
  text_color = NULL,
  plot_background_fill = NULL,
  panel_background_fill = NULL,
  panel_grid_major_color = NULL,
  panel_grid_minor_color = NULL,
  axis_text_color = NULL,
  plot_caption_color = NULL
)

Arguments

brand

One of:

  • NULL (default): Automatically detect and read a _brand.yml file

  • A path to a brand.yml file or directory containing _brand.yml

  • A brand object (as returned by read_brand_yml() or as_brand_yml())

  • FALSE: Don't use a brand file; explicit colors must be provided

background

The background color, defaults to brand.color.background. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

foreground

The foreground color, defaults to brand.color.foreground. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

accent

The accent color, defaults to brand.color.primary or brand.color.palette.accent. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

...

Reserved for future use.

base_size

Base font size in points. Used for the size property of ggplot2::element_text() in the text theme element.

title_size

Title font size in points. Used for the size property of ggplot2::element_text() in the title theme element. Defaults to base_size * 1.2.

title_color

Color for the color property of ggplot2::element_text() in the title theme element. Can be a valid R color or the name of a color in brand.color or brand.color.palette. If not provided, defaults to the foreground color.

line_color

Color for the color property of ggplot2::element_line() in the line theme element. Can be a valid R color or the name of a color in brand.color or brand.color.palette. If not provided, defaults to a blend of foreground and background colors.

rect_fill

Fill color for the fill property of ggplot2::element_rect() in the rect theme element. Can be a valid R color or the name of a color in brand.color or brand.color.palette. If not provided, defaults to the background color.

text_color

Color for the color property of ggplot2::element_text() in the text theme element. Can be a valid R color or the name of a color in brand.color or brand.color.palette. If not provided, defaults to a blend of foreground and background colors.

plot_background_fill

Fill color for the fill property of ggplot2::element_rect() in the plot.background theme element. Can be a valid R color or the name of a color in brand.color or brand.color.palette. If not provided, defaults to the background color.

panel_background_fill

Fill color for the fill property of ggplot2::element_rect() in the panel.background theme element. Can be a valid R color or the name of a color in brand.color or brand.color.palette. If not provided, defaults to the background color.

panel_grid_major_color

Color for the color property of ggplot2::element_line() in the panel.grid.major theme element. Can be a valid R color or the name of a color in brand.color or brand.color.palette. If not provided, defaults to a blend of foreground and background colors.

panel_grid_minor_color

Color for the color property of ggplot2::element_line() in the panel.grid.minor theme element. Can be a valid R color or the name of a color in brand.color or brand.color.palette. If not provided, defaults to a blend of foreground and background colors.

axis_text_color

Color for the color property of ggplot2::element_text() in the axis.text theme element. Can be a valid R color or the name of a color in brand.color or brand.color.palette. If not provided, defaults to a blend of foreground and background colors.

plot_caption_color

Color for the color property of ggplot2::element_text() in the plot.caption theme element. Can be a valid R color or the name of a color in brand.color or brand.color.palette. If not provided, defaults to a blend of foreground and background colors.

Value

A ggplot2::theme() object.

Branded Theming

The ⁠theme_brand_*⁠ functions can be used in two ways:

  1. With a brand.yml file: The ⁠theme_brand_*⁠ functions use read_brand_yml() to automatically detect and use a ⁠_brand.yml⁠ file in your current project. You can also explicitly pass a path to a brand.yml file or a brand object (as returned by read_brand_yml() or created with as_brand_yml()). When a brand is provided, the theme functions will use the colors defined in the brand file automatically.

  2. With explicit colors: You can directly provide colors to override the default brand colors, or you can use brand = FALSE to ignore any project ⁠_brand.yml⁠ files and only use the explicitly provided colors.

See Also

Other branded theming functions: theme_brand_flextable(), theme_brand_gt(), theme_brand_plotly(), theme_brand_thematic()

Examples

brand <- as_brand_yml('
color:
  palette:
    black: "#1A1A1A"
    white: "#F9F9F9"
    orange: "#FF6F20"
  foreground: black
  background: white
  primary: orange')

library(ggplot2)
ggplot(diamonds, aes(carat, price)) +
  geom_point() +
  theme_brand_ggplot2(brand)

Create a gt table theme using brand colors

Description

Apply brand colors to a gt table.

Usage

theme_brand_gt(table, brand = NULL, background = NULL, foreground = NULL)

Arguments

table

A gt table object to theme.

brand

One of:

  • NULL (default): Automatically detect and read a _brand.yml file

  • A path to a brand.yml file or directory containing _brand.yml

  • A brand object (as returned by read_brand_yml() or as_brand_yml())

  • FALSE: Don't use a brand file; explicit colors must be provided

background

The background color, defaults to brand.color.background. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

foreground

The foreground color, defaults to brand.color.foreground. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

Value

Returns a themed gt table object.

See Also

Other branded theming functions: theme_brand_flextable(), theme_brand_ggplot2(), theme_brand_plotly(), theme_brand_thematic()

Examples

brand <- as_brand_yml('
color:
  palette:
    black: "#1A1A1A"
    white: "#F9F9F9"
    orange: "#FF6F20"
  foreground: black
  background: white
  primary: orange')

library(gt)
theme_brand_gt(
  gt(head(palmerpenguins::penguins)),
  brand
)


brand <- as_brand_yml('
color:
  palette:
    black: "#1A1A1A"
    white: "#F9F9F9"
    orange: "#FF6F20"
  foreground: black
  background: white
  primary: orange')

library(gt)
theme_brand_gt(
  gt(head(mtcars)),
  brand
)

Create a plotly theme using brand colors

Description

Apply brand colors to a plotly plot.

Usage

theme_brand_plotly(
  plot,
  brand = NULL,
  background = NULL,
  foreground = NULL,
  accent = NULL
)

Arguments

plot

A plotly plot object to theme.

brand

One of:

  • NULL (default): Automatically detect and read a _brand.yml file

  • A path to a brand.yml file or directory containing _brand.yml

  • A brand object (as returned by read_brand_yml() or as_brand_yml())

  • FALSE: Don't use a brand file; explicit colors must be provided

background

The background color, defaults to brand.color.background. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

foreground

The foreground color, defaults to brand.color.foreground. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

accent

The accent color, defaults to brand.color.primary or brand.color.palette.accent. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

Value

Returns a themed plotly plot object.

See Also

Other branded theming functions: theme_brand_flextable(), theme_brand_ggplot2(), theme_brand_gt(), theme_brand_thematic()

Examples

brand <- as_brand_yml('
color:
  palette:
    black: "#1A1A1A"
    white: "#F9F9F9"
    orange: "#FF6F20"
  foreground: black
  background: white
  primary: orange')

library(plotly)
plot_ly(palmerpenguins::penguins, x = ~bill_length_mm, y = ~bill_depth_mm) |>
  theme_brand_plotly(brand)


brand <- as_brand_yml('
color:
  palette:
    black: "#1A1A1A"
    white: "#F9F9F9"
    orange: "#FF6F20"
  foreground: black
  background: white
  primary: orange')

library(plotly)
plot_ly(mtcars, x = ~wt, y = ~mpg) |>
  theme_brand_plotly(brand)

Create a thematic theme using brand colors

Description

Apply thematic styling using explicit colors or by automatically extracting colors from a brand.yml file. This function sets global theming for base R graphics.

Usage

theme_brand_thematic(
  brand = NULL,
  background = NULL,
  foreground = NULL,
  accent = NULL,
  ...
)

theme_brand_thematic_on(
  brand = NULL,
  background = NULL,
  foreground = NULL,
  accent = NULL,
  ...
)

Arguments

brand

One of:

  • NULL (default): Automatically detect and read a _brand.yml file

  • A path to a brand.yml file or directory containing _brand.yml

  • A brand object (as returned by read_brand_yml() or as_brand_yml())

  • FALSE: Don't use a brand file; explicit colors must be provided

background

The background color, defaults to brand.color.background. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

foreground

The foreground color, defaults to brand.color.foreground. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

accent

The accent color, defaults to brand.color.primary or brand.color.palette.accent. If provided directly, this value can be a valid R color or the name of a color in brand.color or brand.color.palette.

...

Additional arguments passed to thematic::thematic_theme() or thematic::thematic_on().

Value

thematic_theme() returns a theme object as a list (which can be activated with thematic_with_theme() or thematic_set_theme()).

thematic_on(), thematic_off(), and thematic_shiny() all return the previous global theme.

Functions

See Also

See the "Branded Theming" section of theme_brand_ggplot2() for more details on how the brand argument works.

Other branded theming functions: theme_brand_flextable(), theme_brand_ggplot2(), theme_brand_gt(), theme_brand_plotly()

Examples

brand <- as_brand_yml('
color:
  palette:
    black: "#1A1A1A"
    white: "#F9F9F9"
    orange: "#FF6F20"
  foreground: black
  background: white
  primary: orange')


library(ggplot2)

thematic::thematic_with_theme(theme_brand_thematic(brand), {
  ggplot(diamonds, aes(carat, price)) +
    geom_point()
})

Temporarily set the BRAND_YML_PATH environment variable

Description

This function sets the BRAND_YML_PATH environment variable to the specified path for the duration of the local environment. This ensures that, for the scope of the local environment, any calls to functions that automatically discover a ⁠_brand.yml⁠ file will use the path specified.

Usage

with_brand_yml_path(path, code)

local_brand_yml_path(path, .local_envir = parent.frame())

Arguments

path

The path to a brand.yml file.

code

[any]
Code to execute in the temporary environment

.local_envir

⁠[environment]⁠
The environment to use for scoping.

Value

[any]
The results of the evaluation of the code argument.

Functions

  • with_brand_yml_path(): Run code in a temporary environment with the BRAND_YML_PATH environment variable set to path.

  • local_brand_yml_path(): Set the BRAND_YML_PATH environment variable for the scope of the local environment (e.g. within the current function).

See Also

Other brand.yml helpers: brand_color_pluck(), brand_has(), brand_pluck()

Examples

# Create a temporary brand.yml file in a tempdir for this example
tmpdir <- withr::local_tempdir("brand")
path_brand <- file.path(tmpdir, "my-brand.yml")
yaml::write_yaml(
  list(color = list(primary = "#abc123")),
  path_brand
)

with_brand_yml_path(path_brand, {
  brand <- read_brand_yml()
  brand$color$primary
})