| table2html | R Documentation |
Converts a data frame, data.table, or matrix to HTML format with optional CSS styling for web display, HTML documents, or embedding in web applications. Generates clean, standards-compliant HTML with professional styling options including responsive design support, color schemes, and interactive features. Requires xtable for export.
table2html(
table,
file,
caption = NULL,
format_headers = TRUE,
variable_padding = FALSE,
bold_significant = TRUE,
bold_variables = FALSE,
p_threshold = 0.05,
indent_groups = FALSE,
condense_table = FALSE,
condense_quantitative = FALSE,
zebra_stripes = FALSE,
stripe_color = "#EEEEEE",
dark_header = FALSE,
include_css = TRUE,
...
)
table |
Data frame, data.table, or matrix to export. Can be output from
|
file |
Character string specifying the output HTML filename. Must have
|
caption |
Character string. Optional caption displayed below the table.
Supports basic HTML formatting. Default is |
format_headers |
Logical. If |
variable_padding |
Logical. If |
bold_significant |
Logical. If |
bold_variables |
Logical. If |
p_threshold |
Numeric. Threshold for bold p-value formatting. Only
used when |
indent_groups |
Logical. If |
condense_table |
Logical. If |
condense_quantitative |
Logical. If |
zebra_stripes |
Logical. If |
stripe_color |
Character string. HTML color specification for zebra
stripes. Can use hex codes ( |
dark_header |
Logical. If |
include_css |
Logical. If |
... |
Additional arguments passed to |
Output Format:
The function generates standards-compliant HTML5 markup with:
Semantic <table> structure
Proper <thead> and <tbody> sections
Accessible header cells (<th>)
Clean, readable markup
Optional embedded CSS styling
Standalone vs. Embedded:
Standalone HTML (include_css = TRUE):
Can be opened directly in web browsers
Includes all necessary styling
Self-contained, portable
Suitable for sharing via email or web hosting
Embedded HTML (include_css = FALSE):
For inclusion in existing HTML documents
No CSS included (use parent document's styles)
Smaller file size
Integrates with web frameworks (Shiny, R Markdown, Quarto)
CSS Styling:
When include_css = TRUE, the function applies professional styling:
Table: Border-collapse, sans-serif font (Arial), 20px margin
Cells: 8px vertical × 12px horizontal padding, left-aligned text
Borders: 1px solid #DDD (light gray)
Headers: Bold text, light gray background (#F2F2F2)
Numeric columns: Center-aligned (auto-detected)
Caption: Bold, 1.1em font, positioned below table
With dark_header = TRUE:
Header background: Black (#000000)
Header text: White (#FFFFFF)
Creates high contrast, modern appearance
With zebra_stripes = TRUE:
Alternating variable groups receive background color
Default color: #EEEEEE (light gray)
Applied via CSS class .zebra-stripe
Groups entire variable (all factor levels together)
Hierarchical Display:
The indent_groups option creates visual hierarchy using HTML
non-breaking spaces:
<td><b>Treatment</b></td> <!-- Variable name --> <td> Control</td> <!-- Indented level --> <td> Active</td> <!-- Indented level -->
Integration with R Markdown/Quarto:
For R Markdown or Quarto documents:
# Generate HTML fragment (no CSS) table2html(results, "table.html", include_css = FALSE)
Then include in your document chunk with results='asis':
cat(readLines("table.html"), sep = "\n")
Or directly render without file:
# For inline display
htmltools::HTML(
capture.output(
print(xtable::xtable(results), type = "html")
)
)
Integration with Shiny:
For Shiny applications:
# In server function
output$results_table <- renderUI({
table2html(results_data(), "temp.html", include_css = FALSE)
HTML(readLines("temp.html"))
})
# Or use directly with DT package for interactive tables
output$interactive_table <- DT::renderDT({
results_data()
})
Accessibility:
The generated HTML follows accessibility best practices:
Semantic table structure
Proper header cells (<th>) with scope attributes
Clear visual hierarchy
Adequate color contrast (when using default styles)
Screen reader friendly markup
Invisibly returns NULL. Creates an HTML file at the specified
location that can be opened in web browsers or embedded in HTML documents.
autotable for automatic format detection,
table2pdf for PDF output,
table2tex for LaTeX output,
table2docx for Word documents,
table2pptx for PowerPoint,
table2rtf for Rich Text Format,
fit for regression tables,
desctable for descriptive tables
Other export functions:
autotable(),
table2docx(),
table2pdf(),
table2pptx(),
table2rtf(),
table2tex()
data(clintrial)
data(clintrial_labels)
# Create example table
results <- fit(
data = clintrial,
outcome = "os_status",
predictors = c("age", "sex", "treatment", "stage"),
labels = clintrial_labels
)
# Example 1: Basic HTML export (standalone)
if (requireNamespace("xtable", quietly = TRUE)) {
table2html(results, file.path(tempdir(), "results.html"))
}
# Example 2: With caption
table2html(results, file.path(tempdir(), "captioned.html"),
caption = "Table 1: Multivariable Logistic Regression Results")
# Example 3: For embedding (no CSS)
table2html(results, file.path(tempdir(), "embed.html"),
include_css = FALSE)
# Include in your HTML document
# Example 4: Hierarchical display
table2html(results, file.path(tempdir(), "indented.html"),
indent_groups = TRUE)
# Example 5: Condensed table
table2html(results, file.path(tempdir(), "condensed.html"),
condense_table = TRUE)
# Example 6: With zebra stripes
table2html(results, file.path(tempdir(), "striped.html"),
zebra_stripes = TRUE,
stripe_color = "#F0F0F0")
# Example 7: Dark header style
table2html(results, file.path(tempdir(), "dark.html"),
dark_header = TRUE)
# Example 8: Combination styling
table2html(results, file.path(tempdir(), "styled.html"),
zebra_stripes = TRUE,
dark_header = TRUE,
bold_significant = TRUE)
# Example 9: Custom stripe color
table2html(results, file.path(tempdir(), "blue_stripes.html"),
zebra_stripes = TRUE,
stripe_color = "#E3F2FD") # Light blue
# Example 10: Disable significance bolding
table2html(results, file.path(tempdir(), "no_bold.html"),
bold_significant = FALSE)
# Example 11: Stricter significance threshold
table2html(results, file.path(tempdir(), "strict.html"),
bold_significant = TRUE,
p_threshold = 0.01)
# Example 12: No header formatting
table2html(results, file.path(tempdir(), "raw_headers.html"),
format_headers = FALSE)
# Example 13: Descriptive statistics table
desc_table <- desctable(clintrial, by = "treatment",
variables = c("age", "sex", "bmi"), labels = clintrial_labels)
table2html(desc_table, file.path(tempdir(), "baseline.html"),
caption = "Table 1: Baseline Characteristics by Treatment Group")
# Example 14: For R Markdown (no CSS, for inline display)
table2html(results, file.path(tempdir(), "rmd_table.html"),
include_css = FALSE,
indent_groups = TRUE)
# Then in R Markdown, use a chunk with results='asis' to display inline:
cat(readLines(file.path(tempdir(), "rmd_table.html")), sep = "\n")
# Example 15: Email-friendly version
table2html(results, file.path(tempdir(), "email.html"),
include_css = TRUE, # Self-contained
zebra_stripes = TRUE,
caption = "Regression Results - See Attached")
# Can be directly included in HTML emails
# Example 16: Publication-ready web version
table2html(results, file.path(tempdir(), "publication.html"),
caption = "Table 2: Multivariable Analysis of Risk Factors",
indent_groups = TRUE,
zebra_stripes = FALSE, # Clean look
bold_significant = TRUE,
dark_header = FALSE)
# Example 17: Modern dark theme
table2html(results, file.path(tempdir(), "dark_theme.html"),
dark_header = TRUE,
stripe_color = "#2A2A2A", # Dark gray stripes
zebra_stripes = TRUE)
# Example 18: Minimal styling for custom CSS
table2html(results, file.path(tempdir(), "minimal.html"),
include_css = FALSE,
format_headers = FALSE,
bold_significant = FALSE)
# Apply your own CSS classes and styling
# Example 19: Model comparison table
models <- list(
base = c("age", "sex"),
full = c("age", "sex", "treatment", "stage")
)
comparison <- compfit(
data = clintrial,
outcome = "os_status",
model_list = models
)
table2html(comparison, file.path(tempdir(), "comparison.html"),
caption = "Model Comparison Statistics")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.