| govReactable | R Documentation |
This function is opinionated and sets table defaults that are in
keeping with the wider GOV.UK design system. Some defaults can be overridden,
such as highlight=TRUE and borderless=TRUE, however some are fixed, such
as showSortIcon=FALSE as the default sort icon is inaccessible. Additional
arguments from reactable::reactable can be passed to customise the table.
govReactable(
df,
right_col = NULL,
page_size = 10,
highlight = TRUE,
borderless = TRUE,
min_widths = list(),
columns = list(),
...
)
df |
A dataframe used to generate the table |
right_col |
A vector of column names that should be right-aligned. By default, numeric data is right-aligned, and character data is left-aligned |
page_size |
The default number of rows displayed per page (default: 10) |
highlight |
Highlight table rows on hover |
borderless |
Remove inner borders from table |
min_widths |
Customise minimum column width using a list of columns and minimum width in pixels |
columns |
Customise individual columns, for example to fix the
number of decimal places shown. Give a named list, where each name
matches a column in |
... |
Additional arguments passed to |
This function inserts a government-styled table using reactable.
You can use this in R markdown or Quarto documents, or use
renderGovReactable() and govReactableOutput() for tables in R Shiny.
govReactableOutput() gives the ability to add a caption, for static
tables made using just govReactable(), use heading_text() to add
captions to tables.
A reactable HTML widget styled with GOV.UK classes
Other Govstyle tables tabs and accordions:
accordion(),
govReactable-shiny,
govTable(),
govTabs()
# Example static table using govReactable
if (interactive()) {
govReactable(
iris,
right_col = c(
"Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"
)
)
govReactable(
iris,
right_col = c(
"Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"
),
highlight = FALSE,
page_size = 5,
min_widths = list(
Sepal.Length = 75,
Sepal.Width = 75,
Petal.Length = 75,
Petal.Width = 75
)
)
# Show one column to a fixed number of decimal places, leaving every
# other column and style (sorting, alignment, etc.) untouched
sales_data <- data.frame(
shop = c("Shop A", "Shop B", "Shop C"),
growth = c(4, 4.7, 12.34)
)
govReactable(
sales_data,
columns = list(
growth = reactable::colDef(
format = reactable::colFormat(digits = 1)
)
)
)
# A more involved example: only `percent` is customised, so `region`
# and `count` keep the normal GOV.UK defaults untouched
# (counts shown as whole numbers alongside a percentage column fixed
# to 1 decimal place)
count_pct_data <- data.frame(
region = c("North", "South", "East"),
count = c(1234, 56, 789),
percent = c(4, 4.7, 12.34)
)
govReactable(
count_pct_data,
right_col = c("count", "percent"),
columns = list(
percent = reactable::colDef(
format = reactable::colFormat(digits = 1)
)
)
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.