最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

R: Is there a way to add gt tables to a docx document, while maintaining format and gtExtras elements (e.g., sparklines) and add

programmeradmin2浏览0评论

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!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论