【问题标题】:Formatting tables in R Markdown to export to MS Word document在 R Markdown 中格式化表格以导出到 MS Word 文档
【发布时间】:2019-08-27 19:08:02
【问题描述】:

我已经开始在 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 表格看起来像 HTML 表格示例,可以在此 link 或此 HTML 表格示例图片中找到

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

Word 中的表格输出如下所示:

引擎

V引擎

直引擎

传输

自动

12

7

手动

6

7

#病例总数

18

14

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-07-20
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多