Skip to contents

Automatically detects the output format based on file extension and exports the table using the appropriate specialized function. Provides a unified interface for table export across all supported formats.

Usage

autotable(table, file, ...)

Arguments

table

Data frame, data.table, or matrix to export. Can be output from desctable(), survtable(), fit(), uniscreen(), fullfit(), compfit(), multifit(), or any tabular data structure.

file

Character string specifying the output filename. The file extension determines the export format:

...

Additional arguments passed to the format-specific function. See the documentation for individual functions for available parameters:

PDF

table2pdf() - orientation, paper, margins, fit_to_page, etc.

DOCX

table2docx() - font_size, font_family, caption, etc.

HTML

table2html() - format_headers, zebra_stripes, etc.

PPTX

table2pptx() - font_size, font_family, caption, etc.

TEX

table2tex() - caption, format_headers, align, etc.

RTF

table2rtf() - font_size, font_family, caption, etc.

Common parameters across formats include:

caption

Table caption (supported by most formats)

font_size

Base font size in points (PDF, DOCX, PPTX, RTF)

format_headers

Format column headers (all formats)

bold_significant

Bold significant p-values (all formats)

p_threshold

Threshold for p-value bolding (all formats)

indent_groups

Indent factor levels (all formats)

condense_table

Condense to essential rows (all formats)

zebra_stripes

Alternating background colors (most formats)

Value

Invisibly returns the file path. Called primarily for its side effect of creating the output file.

Details

This function provides a convenient wrapper around format-specific export functions, automatically routing to the appropriate function based on the file extension. All parameters are passed through to the underlying function, so the full range of format-specific options remains available.

For format-specific advanced features, you may prefer to use the individual export functions directly:

  • PDF exports support orientation, paper size, margins, and auto-sizing

  • DOCX/PPTX/RTF support font customization and flextable formatting

  • HTML supports CSS styling, responsive design, and custom themes

  • TeX generates standalone LaTeX source with booktabs styling

Examples

# Create example data
data(clintrial)
data(clintrial_labels)
tbl <- desctable(clintrial, by = "treatment",
    variables = c("age", "sex"), labels = clintrial_labels)

# Auto-detect format from extension
if (requireNamespace("xtable", quietly = TRUE)) {
  autotable(tbl, file.path(tempdir(), "example.html"))
}
#> Table exported to /tmp/Rtmp9Tmfn9/example.html

# \donttest{
# Load example data
data(clintrial)
data(clintrial_labels)

# Create a regression table
results <- fit(
    data = clintrial,
    outcome = "os_status",
    predictors = c("age", "sex", "treatment"),
    labels = clintrial_labels
)

# Test that LaTeX can actually compile (needed for PDF export)
has_latex <- local({
  if (!nzchar(Sys.which("pdflatex"))) return(FALSE)
  test_tex <- file.path(tempdir(), "summata_latex_test.tex")
  writeLines(c("\\documentclass{article}",
               "\\usepackage{booktabs}",
               "\\begin{document}", "test",
               "\\end{document}"), test_tex)
  result <- tryCatch(
    system2("pdflatex", c("-interaction=nonstopmode",
            paste0("-output-directory=", tempdir()), test_tex),
            stdout = FALSE, stderr = FALSE),
    error = function(e) 1L)
  result == 0L
})

# Export automatically detects format from extension
autotable(results, file.path(tempdir(), "results.html"))  # Creates HTML file
#> Table exported to /tmp/Rtmp9Tmfn9/results.html
autotable(results, file.path(tempdir(), "results.docx"))  # Creates Word document
#> Table exported to /tmp/Rtmp9Tmfn9/results.docx
autotable(results, file.path(tempdir(), "results.pptx"))  # Creates PowerPoint slide
#> Table exported to /tmp/Rtmp9Tmfn9/results.pptx
autotable(results, file.path(tempdir(), "results.tex"))   # Creates LaTeX source
#> Table exported to /tmp/Rtmp9Tmfn9/results.tex
autotable(results, file.path(tempdir(), "results.rtf"))   # Creates RTF document
#> Table exported to /tmp/Rtmp9Tmfn9/results.rtf
if (has_latex) {
  autotable(results, file.path(tempdir(), "results.pdf")) # Creates PDF
}

# Pass format-specific parameters
if (has_latex) {
  autotable(results, file.path(tempdir(), "results.pdf"), 
             orientation = "landscape",
             paper = "a4",
             font_size = 10)
}

autotable(results, file.path(tempdir(), "results.docx"),
           caption = "Table 1: Logistic Regression Results",
           font_family = "Times New Roman",
           condense_table = TRUE)
#> Table exported to /tmp/Rtmp9Tmfn9/results.docx

autotable(results, file.path(tempdir(), "results.html"),
           zebra_stripes = TRUE,
           dark_header = TRUE,
           bold_significant = TRUE)
#> Table exported to /tmp/Rtmp9Tmfn9/results.html

# Works with any summata table output
desc <- desctable(clintrial,
                  by = "treatment",
                  variables = c("age", "sex", "bmi"))
if (has_latex) {
  autotable(desc, file.path(tempdir(), "demographics.pdf"))
}

comparison <- compfit(
    data = clintrial,
    outcome = "os_status",
    model_list = list(
        base = c("age", "sex"),
        full = c("age", "sex", "treatment", "stage")
    )
)
#> Auto-detected binary outcome, using logistic regression
#> Fitting base with 2 predictors...
#> Fitting full with 4 predictors...
autotable(comparison, file.path(tempdir(), "model_comparison.docx"))
#> Table exported to /tmp/Rtmp9Tmfn9/model_comparison.docx

# }