View source: R/zzz-tabexport-meta.R View source: R/tabexport.R
| tabexport | R Documentation |
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.
tabexport(..., export = NULL, file = NULL, open = FALSE, title = NULL,
sheet = NULL, overwrite = TRUE, quiet = FALSE)
... |
One or more |
export |
Output format: |
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. |
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.
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.
tab and tabmulti.
Other R4VN tables:
tab(),
tabforest(),
tablong(),
tabmeta(),
tabmulti(),
tabscale(),
tabscore(),
tabsurvey(),
vars()
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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.