Skip to contents

Renders the spec to a self-contained HTML fragment and wraps it in an htmltools::tagList suitable for inline embedding in Quarto / Rmd chunks, RStudio / Positron viewer panes, pkgdown reference pages, and Shiny UIs.

Usage

# S3 method for class 'tabular_spec'
as.tags(x, ..., id = NULL)

Arguments

x

The tabular_spec to convert. <tabular_spec>: required.

...

Reserved. Ignored.

id

Wrapping div id. <character(1) | NULL>: default NULL (auto-generate). Pass an explicit id when you need to target the table from external CSS or JavaScript.

Value

An htmltools::tagList containing a <style> block plus a wrapping <div> containing the table. Knitr, htmltools, and RStudio / Positron viewer panes all know how to render it.

Details

Fragment extraction. Tabular's HTML backend emits a full <!DOCTYPE html> document with a <style> block in the head and the table inside <body>. For inline embedding we extract the <style> and <body> content separately and re- wrap them in an htmltools::tagList:

<style>...table CSS...</style>
<div id="..." style="overflow-x:auto;max-width:100%;">
  ...table content...
</div>

The wrapping <div> gets a random unique id (so multiple tables on the same page have CSS-scopable hooks) and overflow-x: auto so wide tables get a horizontal scrollbar instead of overflowing their container.

See also

Renders via: print.tabular_spec, knit_print().

Terminal verb: emit().

Examples

# `as.tags()` converts a spec into an htmltools tagList you can drop into
# a custom HTML page, a Shiny UI, or a Quarto / Rmd chunk. `print()` and
# `knit_print()` call it under the hood, so you seldom call it directly --
# but it is the seam for composing several tables into one container.
s1 <- tabular(cdisc_saf_demo, titles = "Demographics")
s2 <- tabular(cdisc_saf_ae, titles = "AE overall")

# Compose two tables into one parent tagList. Autoprinting `tables` in a
# Quarto / Rmd chunk renders both inline (via knit_print); embed it with
# htmltools::save_html() or a Shiny renderUI().
tables <- htmltools::tagList(
  htmltools::as.tags(s1),
  htmltools::as.tags(s2)
)

# The common path is autoprinting a spec: the viewer at an interactive
# prompt, an inline live table under pkgdown / knitr, and HTML source
# under R CMD check. This is the gt / flextable / tinytable convention --
# end on a bare table object and let the registered print method choose,
# with no browsable() / if (interactive()) wrapper, so R CMD check never
# launches a browser.
s1

 

Demographics

 

variablestat_labelplacebodrug_50drug_100Total
Age (years)n869672254
Age (years)Mean (SD)75.2 (8.59)76.0 (8.11)73.8 (7.94)75.1 (8.25)
Age (years)Median76.078.075.577.0
Age (years)Q1, Q369.2, 81.871.0, 82.070.5, 79.070.0, 81.0
Age (years)Min, Max52, 8951, 8856, 8851, 89
Sex, n (%)F53 (61.6)55 (57.3)35 (48.6)143 (56.3)
Sex, n (%)M33 (38.4)41 (42.7)37 (51.4)111 (43.7)
Race, n (%)WHITE78 (90.7)90 (93.8)62 (86.1)230 (90.6)
Race, n (%)BLACK OR AFRICAN AMERICAN8 (9.3)6 (6.2)9 (12.5)23 (9.1)
Race, n (%)ASIAN0 (0.0)0 (0.0)0 (0.0)0 (0.0)
Race, n (%)AMERICAN INDIAN OR ALASKA NATIVE0 (0.0)0 (0.0)1 (1.4)1 (0.4)