| Title: | Collapsible 'HTML' Tables from Hierarchical Data |
|---|---|
| Description: | Creates collapsible, expandable 'HTML' tables from hierarchical data. Supports data frame input with multi-level grouping, custom column formatters, bottom-up rollup aggregation, and CSS-variable-based theming. Works in 'Shiny' applications, R Markdown, 'Quarto', and the 'RStudio' Viewer. |
| Authors: | Derek Underwood [aut, cre] |
| Maintainer: | Derek Underwood <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.0 |
| Built: | 2026-06-04 11:10:27 UTC |
| Source: | https://github.com/derekunderwood/nestable |
Define a table column
col_def( key, header = NULL, format = function(x) base::format(x), color = NULL, rollup = "sum", width = NULL )col_def( key, header = NULL, format = function(x) base::format(x), color = NULL, rollup = "sum", width = NULL )
key |
Character. The value key — the column name in your data frame (or
the name used in each node's |
header |
Character. Column header text. Defaults to a title-cased
version of |
format |
Function |
color |
Function |
rollup |
How parent rows are aggregated. Either a shortcut string
( |
width |
CSS width string (e.g. |
A named list describing the column.
Convert a flat data frame into a nested node tree
df_to_tree( df, name_col, value_cols, group_col = NULL, total = NULL, node_values = list() )df_to_tree( df, name_col, value_cols, group_col = NULL, total = NULL, node_values = list() )
df |
A data frame. |
name_col |
Column name to use as the node label (leaf rows). |
value_cols |
Character vector of value column names. |
group_col |
Character vector of grouping columns, outermost first.
Each element adds one nesting level. |
total |
Optional string. When non- |
node_values |
Optional named list of pre-supplied values for group (and total) nodes. Each name is a node label; each value is a named list of column values that should be displayed as-is rather than rolled up from children. Useful when aggregated figures (e.g. time-weighted returns) are already known and differ from a simple weighted average of the leaves. Example — supply a pre-computed return for the "Technology" sector and the "Mag 7" grand total: node_values = list( "Technology" = list(ytd_return = 2.5), "Mag 7" = list(ytd_return = 4.1) ) Any column not listed for a node still falls back to rollup from children. |
A list of node() objects suitable for passing to nestable().
Returns a formatting function for use as format_fn in col_def().
fmt_currency(prefix = "$", suffix = "", digits = 2L, big_mark = ",")fmt_currency(prefix = "$", suffix = "", digits = 2L, big_mark = ",")
prefix |
Character prepended before the number. Default |
suffix |
Character appended after the number. Default |
digits |
Integer decimal places. Default |
big_mark |
Thousands separator. Default |
A function function(x) -> character.
Returns a formatting function for use as format_fn in col_def().
fmt_percent(digits = 2L, plus = TRUE)fmt_percent(digits = 2L, plus = TRUE)
digits |
Integer decimal places. Default |
plus |
Logical. Prefix non-negative values with |
A function function(x) -> character.
Create a nestable collapsible HTML table
nestable( data_root, columns, theme = nestable_theme(), name_col = "name", name_header = NULL, name_col_width = NULL, uid = new_widget_uid() )nestable( data_root, columns, theme = nestable_theme(), name_col = "name", name_header = NULL, name_col_width = NULL, uid = new_widget_uid() )
data_root |
A list of top-level |
columns |
Column specification. Three forms are accepted:
|
theme |
A theme list from |
name_col |
Character. The node label key — the |
name_header |
Character. Header label for the first (name/label) column.
|
name_col_width |
CSS width string (e.g. |
uid |
Character. Widget UID prefix for HTML element |
An htmltools::browsable() tagList. Renders inline in R Markdown,
Quarto, and the RStudio Viewer; use inside shiny::renderUI() or
renderNestable() in Shiny apps.
Every argument maps to a CSS custom property (--ntbl-*) set inline on
the widget's wrapper <div>, so multiple instances with different themes
can coexist on the same page without conflict.
nestable_theme( title = "", font_family = "-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif", font_size = "14px", table_bg = "#ffffff", table_shadow = "0 1px 4px rgba(0,0,0,.12)", table_radius = "6px", table_max_w = "680px", header_bg = "#37474f", header_color = "#ffffff", row_border = "#eceff1", row_hover_bg = "#f9fbe7", parent_weight = "600", toggle_color = "#546e7a", indent_px = 20L, zoom = "normal" )nestable_theme( title = "", font_family = "-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif", font_size = "14px", table_bg = "#ffffff", table_shadow = "0 1px 4px rgba(0,0,0,.12)", table_radius = "6px", table_max_w = "680px", header_bg = "#37474f", header_color = "#ffffff", row_border = "#eceff1", row_hover_bg = "#f9fbe7", parent_weight = "600", toggle_color = "#546e7a", indent_px = 20L, zoom = "normal" )
title |
Character. Title shown above the table. Default |
font_family |
CSS font-family string. |
font_size |
CSS font-size string. Default |
table_bg |
Table background colour. Default |
table_shadow |
CSS box-shadow for the table. |
table_radius |
CSS border-radius. Default |
table_max_w |
CSS max-width. Default |
header_bg |
Header row background. Default |
header_color |
Header row text colour. Default |
row_border |
Row separator colour. Default |
row_hover_bg |
Row hover background. Default |
parent_weight |
CSS font-weight for parent rows. Default |
toggle_color |
Colour of the expand/collapse arrow. Default |
indent_px |
Integer pixels of indentation per nesting level. Default |
zoom |
CSS zoom level applied to the entire widget. Accepts any valid
CSS |
A named list of theme values.
Use with renderNestable() in the server. These are thin wrappers over
shiny::uiOutput() and shiny::renderUI() — no htmlwidgets dependency
is required.
nestableOutput(outputId, ...) renderNestable(expr, env = parent.frame(), quoted = FALSE)nestableOutput(outputId, ...) renderNestable(expr, env = parent.frame(), quoted = FALSE)
outputId |
The output variable name. |
... |
Additional arguments passed to |
expr |
An expression returning a |
env |
The environment in which to evaluate |
quoted |
Logical. Is |
A Shiny UI element.
A Shiny render function.
Define a tree node
node(name, ..., .values = list())node(name, ..., .values = list())
name |
Display label shown in the Name column. |
... |
Child |
.values |
Named list of column values. For leaf nodes supply all values here. For parent nodes any value supplied here overrides the computed rollup for that column; omitted columns are still computed from children. |
A named list with elements name, values, and children.
Convert data frame rows into leaf nodes
rows_to_nodes(df, name_col, value_cols)rows_to_nodes(df, name_col, value_cols)
df |
A data frame. |
name_col |
Column name to use as the node label. |
value_cols |
Character vector of column names to carry as |
A list of node() objects.
Returns a rollup function for use as rollup_fn in col_def(). Computes
the weighted average of vals using another key's values as weights.
weighted_rollup(weight_key)weighted_rollup(weight_key)
weight_key |
Character. The value key to use as weights (e.g.
|
A function function(vals, child_values) -> numeric.