I am trying to output a few gt tables to a word document. The tables include sparklines (from gtExtras gt_plt_sparklines).
Sample code I use:
library(data.table)
library(gt)
library(tibble)
library(gtExtras)
library(officer)
library(rvg)
# Create the data table
dt <- data.table(
Name = c("Jim", "Tim", "Tom"),
Min = sample(1:50, 3),
Max = sample(51:100, 3),
Trend = list(
sample(1:100, 10),
sample(1:100, 10),
sample(1:100, 10)
)
)
# Format using gt
gt_table <- dt %>%
gt() %>%
tab_spanner(
label = "Values",
columns = c(Min, Max)
) %>%
gt_plt_sparkline(column = Trend, same_limit = TRUE)
# Open "test.docx" and add gt table as an image
doc <- read_docx("test.docx")
doc <- body_add_gt(doc, gt_table)
# Save the document
print(doc, target = "result.docx")
The result I want looks like this in R Studio Viewer:
But the result I get in the word document looks like this...
Is there a simple way to output well formatted gt tables to a word document? I've had similar issues trying to use Quarto.
Any advice much appreciated!