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.
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
variable stat_label placebo drug_50 drug_100 Total Age (years) n 86 96 72 254 Age (years) Mean (SD) 75.2 (8.59) 76.0 (8.11) 73.8 (7.94) 75.1 (8.25) Age (years) Median 76.0 78.0 75.5 77.0 Age (years) Q1, Q3 69.2, 81.8 71.0, 82.0 70.5, 79.0 70.0, 81.0 Age (years) Min, Max 52, 89 51, 88 56, 88 51, 89 Sex, n (%) F 53 (61.6) 55 (57.3) 35 (48.6) 143 (56.3) Sex, n (%) M 33 (38.4) 41 (42.7) 37 (51.4) 111 (43.7) Race, n (%) WHITE 78 (90.7) 90 (93.8) 62 (86.1) 230 (90.6) Race, n (%) BLACK OR AFRICAN AMERICAN 8 (9.3) 6 (6.2) 9 (12.5) 23 (9.1) Race, n (%) ASIAN 0 (0.0) 0 (0.0) 0 (0.0) 0 (0.0) Race, n (%) AMERICAN INDIAN OR ALASKA NATIVE 0 (0.0) 0 (0.0) 1 (1.4) 1 (0.4)