tabexport: Export One or More R4VN Tables

View source: R/zzz-tabexport-meta.R View source: R/tabexport.R

tabexportR Documentation

Export One or More R4VN Tables

Description

Accepts objects created by tab() or tabmulti(), as well as data frames and matrices, and returns their data or exports them to HTML, Word, Excel, PDF, or PNG.

Usage

tabexport(..., export = NULL, file = NULL, open = FALSE, title = NULL,
          sheet = NULL, overwrite = TRUE, quiet = FALSE)

Arguments

...

One or more r4vn_tab, r4vn_tabmulti, data-frame, or matrix objects. A list of tables is also accepted.

export

Output format: "html", "docx", "xlsx", "pdf", "png", or a vector of formats. NULL or FALSE returns data only. TRUE is equivalent to "html". Aliases "word" and "excel" are accepted.

file

Base output path. The extension is added automatically.

open

Logical. Open the last exported file.

title

Optional report title.

sheet

Optional Excel sheet names.

overwrite

Logical. Overwrite existing files.

quiet

Logical. Suppress export messages.

Details

Word and Excel use the flat $data component stored in R4VN table objects. HTML, PDF, and PNG retain the original HTML presentation.

Optional packages are required for some formats:

  • Excel: openxlsx or writexl;

  • Word: officer and flextable;

  • PDF: pagedown and Chrome or Chromium;

  • PNG: webshot2 and a compatible browser.

With export = NULL, one table returns a data frame and multiple tables return a named list of data frames.

Value

When export = NULL or FALSE, returns a data frame or list. Otherwise invisibly returns an object of class r4vn_export containing data, files, objects, and call.

See Also

tab and tabmulti.

Other R4VN tables: tab(), tabforest(), tablong(), tabmeta(), tabmulti(), tabscale(), tabscore(), tabsurvey(), vars()

Examples

set.seed(2026)
dat <- data.frame(
  age = round(rnorm(80, 45, 12)),
  sex = factor(sample(c("Female", "Male"), 80, TRUE)),
  bmi = round(rnorm(80, 23, 3), 1)
)

tb <- tab(
  dat,
  vars = vars(c.age, b2.sex, q.bmi),
  title = "Descriptive characteristics",
  show = FALSE
)

# Return a flat data frame without creating a file.
exported_data <- tabexport(tb)
head(exported_data)

# Export HTML to a temporary location.
html_result <- tabexport(
  tb,
  export = "html",
  file = file.path(tempdir(), "r4vn_example"),
  quiet = TRUE
)
html_result$files
unlink(html_result$files)


if (requireNamespace("officer", quietly = TRUE) &&
    requireNamespace("flextable", quietly = TRUE) &&
    requireNamespace("openxlsx", quietly = TRUE)) {
  ex <- tabexport(
    tb,
    export = c("html", "docx", "xlsx"),
    file = tempfile("R4VN_report_"),
    open = FALSE
  )
  unlink(ex$files)
}


# Extended usage examples

t1 <- tab(iris, vars = vars(c.Sepal.Length, c.Sepal.Width, Species),
          show = FALSE)
t2 <- tab(iris, vars = vars(c.Sepal.Length, c.Petal.Length), by = Species,
          test = TRUE, show = FALSE)

# Return the underlying data frame
tabexport(t1, export = NULL)

# One or several output formats, with several tables in one report
ex1 <- tabexport(t1, t2, export = "html", file = tempfile("Iris_tables_"), open = FALSE)
unlink(ex1$files)
if (requireNamespace("officer", quietly = TRUE) &&
    requireNamespace("flextable", quietly = TRUE) &&
    requireNamespace("openxlsx", quietly = TRUE)) {
  ex2 <- tabexport(t1, t2, export = c("html", "docx", "xlsx"),
                   file = tempfile("Iris_analysis_"), title = "Iris analysis", open = FALSE)
  unlink(ex2$files)
}

# PDF/PNG export is available when its external rendering tools are installed.


R4VN documentation built on Sept. 30, 2026, 5:13 p.m.