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

时间:2019-04-05 20:22:17

标签: r ms-word r-markdown knitr expss

我已经开始在R Markdown中使用expss来借助Knitr生成表。我想使表格和分析自动化,以便以Microsoft Word格式准备报告。

编织成HTML时,表格看起来很棒。 Word中的表显示为纯文本行,与表不同。 expss支持将表导出到Word吗?是否有有关操作方法的说明?

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

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表看起来像可以在此link或HTML图片表示例中找到的HTML表示例

enter image description here

如果它们看起来像我的R Console输出中的表格,我也会很高兴

enter image description here

Word中的表输出如下所示:

引擎

V型引擎

直引擎

传输

自动

12

7

手册

6

7

#总数

18

14

1 个答案:

答案 0 :(得分:0)

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

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()