在 R Markdown 中格式化表格以导出到 MS Word 文档

Formatting tables in R Markdown to export to MS Word document

我已经开始在 R Markdown 中使用 expss 在 Knitr 的帮助下生成 table。我想为我需要以 Microsoft Word 格式准备的报告自动执行 table 和分析。

编织到 HTML 时,table 看起来很棒。 Word 中的 table 显示为纯文本行,不像 table。 expss 是否支持将 tables 导出到 Word?有关于如何操作的说明吗?

使用 kable 和 dplyr 生成的表格在 Word 中显示正确。但是,我正在努力重现用 expss 制作的 HTML tables。

library(expss)
data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disp = "Displacement (cu.in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "Weight (1000 lbs)",
                      qsec = "1/4 mile time",
                      vs = "Engine",
                      vs = c("V-engine" = 0,
                             "Straight engine" = 1),
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      gear = "Number of forward gears",
                      carb = "Number of carburetors"
)

cro(mtcars$am, mtcars$vs)

我希望我的 Word tables 看起来像 HTML table 示例,可以在 link 或 HTML table 例子

如果它们看起来像我的 R 控制台输出中的 tables,我也会很高兴

Word 中的 table 输出如下所示:

引擎

V 引擎

直列发动机

传输

自动

12

7

手动

6

7

#病例总数

18

14

expss 使用 htmlTable 包进行 table 渲染。不幸的是,htmlTable 不支持文字输出。 但是,您可以使用 split_table_to_dfkable 函数。它们在 Microsoft Word 中为您提供类似 table 的输出。参见示例:

library(expss)
library(knitr)
data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disp = "Displacement (cu.in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "Weight (1000 lbs)",
                      qsec = "1/4 mile time",
                      vs = "Engine",
                      vs = c("V-engine" = 0,
                             "Straight engine" = 1),
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      gear = "Number of forward gears",
                      carb = "Number of carburetors"
)

cro(mtcars$am, mtcars$vs) %>% 
    split_table_to_df() %>% 
    kable()