5. Exporting to Word and PowerPoint

Editable tables and sharp figures for reports and slides

For function and argument descriptions, open Reference tables in the Lesson companion.

This page exports the Moorfields Table 1 from part 3 and the Ghana sepsis figures from part 4. The studies are separate; we combine their outputs only to practise exporting. Allow 20 minutes for the table and two figures.

Choose the output for its destination

Use RTF for an editable Word table and PNG for a picture in Word or PowerPoint. SVG and PDF keep lines and shapes sharp when resized. The optional sections save clickable HTML and create complete Office files.

Prepare the export examples

Run this cell first, even if you just finished part 4. Each page has its own R session. The scripts rebuild the objects without writing export files.

The setup code is available as moorfields-table.R and sepsis-figures.R. In local R you can reuse the objects from earlier parts instead.

tbl and table_gt hold Table 1. In this lesson, figure is the sepsis PC1 boxplot; the other plot objects come from part 4.

A reusable download button

Run this helper once before exporting. It creates the browser’s download links; you do not need to study its internals.

Files written in browser R disappear when you reload the page. Download them before leaving. In local R, the files save to your working directory.

Export Table 1 for Word

Open the RTF in Word, then save as .docx. The cells and text remain editable. Check column widths, title, and footnotes after opening: fonts and spacing can change between formats.

HTML keeps the gt styling for a browser report or a display reference.

Export a high-resolution figure

Choose the physical size, then the resolution. This saves the sepsis boxplot at seven by five inches and 300 dots per inch (dpi).

The image is 2100 × 1500 pixels. At seven inches wide it has 300 pixels per inch; at fourteen inches it has 150. Changing a resolution label cannot add pixels. plot = figure names the plot explicitly; without it, ggsave() uses the last plot displayed.

Try dpi = 150, download again, and compare the two files at the same size. The lower-resolution image has 1050 × 750 pixels.

Insert the file with Insert → Pictures in Word or PowerPoint. Keep its aspect ratio and check the labels at the final page or slide size. A screenshot may contain far fewer pixels than the exported file.

Give each figure the size it needs

A plot’s panels, labels, and legends determine the space it needs. Give heatmaps more height for their labelled rows and multi-panel figures more width. Export the heatmap and patient paths at different shapes:

Keep the source credit when sharing these CC BY 4.0 data. The exported heatmap receives one without changing pattern_heatmap; the boxplot already has a credit.

Open each image at the size you intend to use. If labels overlap, increase width or height. If you halve a figure’s width, its text keeps the same point size and may no longer fit.

Sizes for journals

Nature’s guide gives 89 mm for one column and 183 mm for two. Check the target journal’s guide and export near the printed size. Set units = "mm" to use its measurements directly.

This replaces the trajectory’s theme with base_size = 8. It is safe here because trajectory has no additional theme() settings. Doing the same to figure would also reset its legend position and bold title; add those settings again if needed.

Try width = 89, height = 70. The two legends crowd that single-column version, so it needs fewer legend entries or a legend below the plot.

Export vector versions

SVG stores the chart as shapes, so it remains sharp when resized. Current Microsoft 365 Word and PowerPoint can insert SVG; check the Microsoft guidance for your platform.

Vector output does not need a DPI setting. An inserted SVG is a graphic, not an Office chart with an embedded spreadsheet.

These heatmaps contain about 1,500 tiles, which suit vector output. Tens of thousands of points can make a large, slow vector file; PNG is often easier for that case.

If the wrong plot saves, supply plot =. If text is cut off, enlarge the figure or reduce base_size. Heatmaps need height for their rows and width for their panels. Export a blurry image again at its final size and 300 dpi. If a browser file disappears after a reload, rerun its export cell.

ggsave() accepts ggplots. The interactive widget needs the next method.

Share the interactive plot as a web page

An HTML widget keeps hover and click controls. Run this in local R after the two setup scripts, with an output/ folder in your project. It needs Pandoc, which browser R does not have.

library(plotly)

patient_key <- highlight_key(patients, ~patient_id)

clickable <- ggplot(patient_key, aes(
    x = P23, y = P4, group = patient_id, colour = outcome,
    text = paste0("Patient ", patient_id,
                  "<br>Visit: ", visit,
                  "<br>Pattern 23: ", round(P23, 2),
                  "<br>Pattern 4: ", round(P4, 2))
  )) +
  geom_path(alpha = 0.3) +
  geom_point(size = 1) +
  facet_wrap(~ outcome) +
  scale_colour_manual(values = line_colours) +
  labs(x = "Pattern 23 value", y = "Pattern 4 value", colour = "28-day outcome") +
  theme_classic(base_size = 12)

interactive_paths <- ggplotly(clickable, tooltip = "text") |>
  plotly::highlight(on = "plotly_click", color = "black")

# Pack the widget and its supporting files into one HTML page.
htmlwidgets::saveWidget(
  interactive_paths, "output/sepsis_paths_interactive.html",
  selfcontained = TRUE, title = "Ghana sepsis cohort: patient paths"
)

plotly::highlight() becomes part of interactive_paths, so click highlighting stays in the saved file. The prefix distinguishes it from flextable’s different highlight() function.

selfcontained = TRUE packs the widget into one file, usually several megabytes because it includes plotly. Install rmarkdown if R asks for it. If Pandoc is missing, install Pandoc or use selfcontained = FALSE. The latter creates the HTML plus sepsis_paths_interactive_files; share both.

Word and PowerPoint cannot play the widget. Insert a static paths picture and link to the HTML for readers who want to explore it without R.

Keep it sharp in Word and PowerPoint

Insert the saved file, keep its aspect ratio, and inspect it at its final size. Where available, choose High fidelity and avoid downsampling the high-resolution image. Check the saved document or its PDF too.

Microsoft’s guides cover image resolution and compression. An editable table has no pixel-resolution requirement.

Optional: create Word and PowerPoint downloads

The following cells run in the browser. Load the packages, run the Word example, then run PowerPoint: the last step reuses the styled table and heatmap file.

For local R, download seminar-exports.R into the lesson folder. Put the prepared CSVs and both setup scripts in data/, install the packages listed in the script, then run source("seminar-exports.R"). It writes every export on this page, including interactive HTML, to output/.

An editable Word table and figures

as_flex_table() reuses the calculations in tbl. Style it separately: gt styling does not transfer. Explicit widths and include = -autofit avoid automatic sizing in browser R.

The table widths total 6.2 inches. The boxplot uses 6.2 * 5 / 7 for its height to keep its 7 × 5 shape; the heatmap is square. body_add_break() starts each figure on a new page.

Try changing a title or header colour, rerun, then download the report.

An editable table and figures in PowerPoint

The slides contain an editable table, an editable trajectory drawing, and a heatmap picture. Table widths total 8.5 inches in the default ten-inch-wide template. Its 14-point font is larger than the Word table’s. If ft or sepsis_heatmap.png is missing, run the Word cell first.

The trajectory drawing is editable shape by shape, not an Office chart linked to data. The heatmap stays a picture. For a department template, use read_pptx("template.pptx") with its layouts and placeholders.