| table2pptx | R Documentation |
Converts a data frame, data.table, or matrix to a Microsoft PowerPoint slide
(.pptx) with a formatted table using the flextable and officer
packages. Creates presentation-ready slides with extensive control over table
formatting, positioning, and layout. Tables can be further edited in PowerPoint
after creation. Ideal for creating data-driven presentations and conference talks.
table2pptx(
table,
file,
caption = NULL,
font_size = 10,
font_family = "Arial",
format_headers = TRUE,
bold_significant = TRUE,
bold_variables = FALSE,
p_threshold = 0.05,
indent_groups = FALSE,
condense_table = FALSE,
condense_quantitative = FALSE,
zebra_stripes = FALSE,
dark_header = FALSE,
width = NULL,
align = NULL,
template = NULL,
layout = "Title and Content",
master = "Office Theme",
left = 0.5,
top = 1.5,
return_ft = FALSE,
...
)
table |
Data frame, data.table, or matrix to export. Can be output from
|
file |
Character string specifying the output PPTX filename. Must have
|
caption |
Character string. Optional title displayed in the slide's title
placeholder or as text box above the table. Default is |
font_size |
Numeric. Base font size in points for table content. Default is 10. Typical range for presentations: 10-14 points. Larger than print documents for visibility at distance. |
font_family |
Character string. Font family name for the table. Must be
installed on the system. Default is |
format_headers |
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 |
dark_header |
Logical. If |
width |
Numeric. Table width in inches. If |
align |
Character vector specifying column alignment. Options:
|
template |
Character string. Path to custom PPTX template file. If
|
layout |
Character string. Name of slide layout to use from template.
Default is
|
master |
Character string. Name of slide master to use. Default is
|
left |
Numeric. Horizontal position from left edge of slide in inches. Default is 0.5. Standard slide is 10 inches wide. |
top |
Numeric. Vertical position from top edge of slide in inches. Default is 1.5 (leaves room for title). Standard slide is 7.5 inches tall. Adjust based on table size and layout. |
return_ft |
Logical. If |
... |
Additional arguments passed to |
Package Requirements:
Requires:
flextable - Table creation and formatting
officer - PowerPoint manipulation
Install: install.packages(c("flextable", "officer"))
Slide Dimensions:
Standard PowerPoint slide:
Width: 10 inches (25.4 cm)
Height: 7.5 inches (19.05 cm)
Aspect ratio: 4:3 (standard) or 16:9 (widescreen)
Safe content area (with margins):
Width: ~9 inches
Height: ~6 inches (accounting for title)
Positioning:
The left and top parameters control table placement:
(0, 0) = Top-left corner of slide
Default (0.5, 1.5) = Standard position with title room
Center: left = (10 - table_width) / 2
When caption is provided:
Attempts to use title placeholder (if layout supports)
Falls back to text box above table
Automatically adjusts table position downward
Slide Layouts:
Different layouts serve different purposes:
Title and Content (default):
Has title and content placeholders
Caption goes in title area
Table in content area
Most common for data slides
Blank:
No predefined areas
Maximum flexibility
Use absolute positioning (left, top)
Good for custom layouts
Title-Only:
Title area only
Large space for table
Good for data-heavy slides
Custom Templates:
Use organizational or conference templates:
table2pptx(table, "branded.pptx",
template = "company_template.pptx",
layout = "Content Layout", # Name from template
master = "Company Theme") # Name from template
To find layout and master names in template:
pres <- officer::read_pptx("template.pptx")
officer::layout_summary(pres)
Multiple Slides:
Creating presentations with multiple tables:
# Each call creates new presentation - combine after
table2pptx(table1, "slide1.pptx", caption = "Results Part 1")
table2pptx(table2, "slide2.pptx", caption = "Results Part 2")
# Then manually combine in PowerPoint, or:
# Use officer to create multi-slide presentation
pres <- officer::read_pptx()
# Add first table
ft1 <- table2pptx(table1, "temp1.pptx", return_ft = TRUE)
pres <- officer::add_slide(pres)
pres <- officer::ph_with(pres, ft1,
location = officer::ph_location(left = 0.5, top = 1.5))
# Add second table
ft2 <- table2pptx(table2, "temp2.pptx", return_ft = TRUE)
pres <- officer::add_slide(pres)
pres <- officer::ph_with(pres, ft2,
location = officer::ph_location(left = 0.5, top = 1.5))
print(pres, target = "combined.pptx")
Further Customization:
Access the flextable object for advanced formatting:
ft <- table2pptx(table, "base.pptx", return_ft = TRUE)
# Customize
ft <- flextable::color(ft, j = "p-value", color = "red")
ft <- flextable::bg(ft, i = 1, bg = "yellow")
ft <- flextable::bold(ft, i = ~ estimate > 0, j = "estimate")
# Save to new slide
pres <- officer::read_pptx()
pres <- officer::add_slide(pres)
pres <- officer::ph_with(pres, ft,
location = officer::ph_location(left = 0.5, top = 1.5))
print(pres, target = "custom.pptx")
Behavior depends on return_ft:
return_ft = FALSEInvisibly returns a list with:
file - Path to created file
caption - Caption/title text
layout - Layout name used
master - Master name used
template - Template path (if provided)
position - List with left and top coordinates
Flextable accessible via attr(result, "flextable")
return_ft = TRUEDirectly returns the flextable object
Always creates a .pptx file at the specified location.
autotable for automatic format detection,
table2docx for Word documents,
table2pdf for PDF output,
table2html for HTML tables,
table2rtf for Rich Text Format,
table2tex for LaTeX output,
flextable for table customization,
read_pptx for PowerPoint manipulation
Other export functions:
autotable(),
table2docx(),
table2html(),
table2pdf(),
table2rtf(),
table2tex()
# Create example data
data(clintrial)
data(clintrial_labels)
tbl <- desctable(clintrial, by = "treatment",
variables = c("age", "sex"), labels = clintrial_labels)
# Basic PowerPoint export
if (requireNamespace("flextable", quietly = TRUE) &&
requireNamespace("officer", quietly = TRUE)) {
table2pptx(tbl, file.path(tempdir(), "example.pptx"))
}
old_width <- options(width = 180)
# Load data
data(clintrial)
data(clintrial_labels)
# Create regression table
results <- fit(
data = clintrial,
outcome = "os_status",
predictors = c("age", "sex", "treatment"),
labels = clintrial_labels
)
# Example 1: Basic PowerPoint slide
table2pptx(results, file.path(tempdir(), "results.pptx"))
# Example 2: With title
table2pptx(results, file.path(tempdir(), "titled.pptx"),
caption = "Multivariable Regression Results")
# Example 3: Larger font for visibility
table2pptx(results, file.path(tempdir(), "large_font.pptx"),
font_size = 12,
caption = "Main Findings")
# Example 4: Condensed for slide space
table2pptx(results, file.path(tempdir(), "condensed.pptx"),
condense_table = TRUE,
caption = "Key Results")
# Example 5: Dark header for emphasis
table2pptx(results, file.path(tempdir(), "dark.pptx"),
dark_header = TRUE,
caption = "Risk Factors")
# Example 6: With zebra stripes
table2pptx(results, file.path(tempdir(), "striped.pptx"),
zebra_stripes = TRUE)
# Example 7: Blank layout with custom positioning
table2pptx(results, file.path(tempdir(), "blank.pptx"),
layout = "Blank",
left = 1,
top = 1.5,
width = 8)
# Example 8: Get flextable for customization
ft <- table2pptx(results, file.path(tempdir(), "base.pptx"), return_ft = TRUE)
# Customize the returned flextable object
ft <- flextable::color(ft, j = "p-value", color = "darkred")
# Example 9: Presentation-optimized table
table2pptx(results, file.path(tempdir(), "presentation.pptx"),
caption = "Main Analysis Results",
font_size = 11,
condense_table = TRUE,
zebra_stripes = TRUE,
dark_header = TRUE,
bold_significant = TRUE)
# Example 10: Descriptive statistics slide
desc <- desctable(
data = clintrial,
by = "treatment",
variables = c("age", "sex", "bmi"),
labels = clintrial_labels
)
table2pptx(desc, file.path(tempdir(), "baseline.pptx"),
caption = "Baseline Characteristics",
font_size = 10)
# Example 11: Conference presentation style
table2pptx(results, file.path(tempdir(), "conference.pptx"),
caption = "Study Outcomes",
font_family = "Calibri",
font_size = 14, # Large for big rooms
dark_header = TRUE,
condense_table = TRUE)
options(old_width)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.