在R Markdown中格式化表格以导出到MS Word文档 [英] Formatting tables in R Markdown to export to MS Word document

查看:541
本文介绍了在R Markdown中格式化表格以导出到MS Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始在R Markdown中使用 expss 在Knitr的帮助下生成表.我想使表格和分析自动化,以便以Microsoft Word格式准备一份报告.

I've started using expss in R Markdown for generating tables with the help of Knitr. I would like to automate the tables and analysis for a report I need to prepare in Microsoft Word format.

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

When knitting to HTML, the tables look wonderful. The tables in Word are displayed as rows of plain text and does not resemble a table. Does expss support exports of tables to Word? Are there instructions on how to do it?

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

Tables generated with kable and dplyr display correctly in Word. However, I'm struggling to reproduce the HTML tables made with expss.

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表示例一样或在此HTML图片表格示例中

I'm hoping that my Word tables would look like the HTML table examples that could be found at this link or in this Image of HTML table example

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

I would also be happy if they look like the the tables in my R Console output

Word中的表输出如下所示:

The table output in Word looks like this:

引擎

V型引擎

直引擎

传输

自动

12

7

手册

6

7

#总案例

18

14

推荐答案

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

expss uses htmlTable package for table rendering. Unfortunately, htmlTable doesn't support word output. However, you can use split_table_to_df and kable functions. They give you table-like output in the Microsoft Word. See example:

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

这篇关于在R Markdown中格式化表格以导出到MS Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆