针织衫Rmarkdown docx表 [英] Knitr & Rmarkdown docx tables

查看:110
本文介绍了针织衫Rmarkdown docx表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同时使用knitrrmarkdown创建Word文档时,可以使用现有的文档来设置输出样式.

When using knitr and rmarkdown together to create a word document you can use an existing document to style the output.

例如在我的yaml标头中:

For example in my yaml header:

output: 
  word_document:
    reference_docx: style.docx
    fig_caption: TRUE

在此样式中,我创建了默认的表样式-这里的目标是使kable表输出为正确的样式.

within this style i have created a default table style - the goal here is to have the kable table output in the correct style.

当我编织word文档并使用style.docx时,表格未根据表格进行样式化.

When I knit the word document and use the style.docx the tables are not stylized according to the table.

到目前为止,使用样式检查器仍然无济于事,不确定默认表样式是否是不正确的样式.

Using the style inspector has not been helpful so far, unsure if the default table style is the incorrect style to modify.

示例代码:

```{r kable}
n <- 100
x <- rnorm(n)
y <- 2*x + rnorm(n)
out <- lm(y ~ x)
library(knitr)
kable(summary(out)$coef, digits=2, caption = "Test Captions")
```

我没有可以上传用于测试的样式化文档.

I do not have a stylized document I can upload for testing unfortunately.

TL; DR:要自动设置rmarkdown和knitr的表格输出样式(通过kable)

TL;DR: Want to stylise table output from rmarkdown and knitr automatically (via kable)

更新:到目前为止,我发现在docx中更改紧凑"样式会自动更改表格的文本内容-但这不能解决表格的整体样式,例如单元格颜色和对齐方式.

Update: So far I have found that changing the 'compact' style in the docx will alter the text contents of the table automatically - but this does not address the overall table styling such as cell colour and alignment.

更新2:在对样式进行了更多研究和创建之后,我发现knitr在访问段落样式方面似乎没有问题.但是表格样式不在该样式类别之内,并且似乎不适用于我的个人测试.

Update 2: After more research and creation of styles I found that knitr seems to have no problem accessing paragraph styles. However table styles are not under that style category and don't seem to apply in my personal testing.

更新3:迷上了ReporteRs软件包-尽管它能够根据需要生成表,但是这样做很费力.宁可自动应用样式.

Update 3: Dabbled with the ReporteRs package - whilst it was able to produce the tables as a desired the syntax required to do so is laborious. Much rather the style be automatically applied.

更新4:您不能更改TableNormal样式,也不能设置Table Normal样式. XML方法不是我们想要的.我有一个VBA宏可以解决这个问题,只是想尽可能地删除该过程.

Update 4: You cannot change TableNormal style, nor does setting a Table Normal style work. The XML approach is not what we are looking for. I have a VBA macro that will do the trick, just want to remove that process if possible.

推荐答案

这实质上是建议TableNormal的答案的组合 rmarkdown.rstudio.com 上的这篇文章以及我自己的实验来展示如何使用TableNormal样式来自定义表,例如kable生成的表:

This is essentially a combination of the answer that recommends TableNormal, this post on rmarkdown.rstudio.com and my own experiments to show how to use a TableNormal style to customize tables like those generated by kable:

RMD :

---
output:
  word_document
---

```{r}
knitr::kable(cars)
```

  • 在RStudio中单击编织词". →该文档将在Word中打开,尚无任何自定义样式.
  • 在该文档中(不是新文档中的 ),添加所需的样式. 本文解释了基础知识.关键不是应用直接样式,而是修改样式.请参阅这篇文章有关Word样式基础的support.office.com .
  • 具体来说,要设置表格样式,您需要添加表格样式.我的Word版本不是英语,但是根据上面链接的文章,表样式可以通过表工具上下文选项卡上的设计选项卡"获得.
  • 选择TableNormal作为样式名称并定义所需的样式.在我的实验中,大多数样式都有效,但是有些却没有. (向第一列添加颜色并使第一行变为粗体没问题;突出显示每第二行都将被忽略.)此答案中的最后一个屏幕截图说明了这一步骤.
  • 保存文档,例如为styles.docx.
  • 修改RMD文件中的标题以使用参考DOCX(请参见此处;不要弄乱缩进–我花了10分钟才发现这个错误):

    • Click "Knit Word" in RStudio. → The document opens in Word, without any custom styles yet.
    • In that document (not in a new document), add the required styles. This article explains the basics. Key is not to apply direct styles but to modify the styles. See this article on support.office.com on Style basics in Word.
    • Specifically, to style a table you need to add a table style. My version of Word is non-English, but according to the article linked above table styles are available via "the Design tab, on the Table Tools contextual tab".
    • Choose TableNormal as style name and define the desired styles. In my experiments most styles worked, however some did not. (Adding a color to the first column and making the first row bold was no problem; highlighting every second row was ignored.) The last screenshot in this answer illustrates this step.
    • Save the document, e.g. as styles.docx.
    • Modify the header in the RMD file to use the reference DOCX (see here; don't screw up the indentation – took me 10 minutes find this mistake):

      ---
      output:
        word_document:
          reference_docx: styles.docx
      ---
      

    • 再次编织到DOCX –现在应该应用样式.

    • Knit to DOCX again – the style should now be applied.

      按照我上面描述的步骤操作,将产生以下输出:

      Following the steps I described above yields this output:

      这是用于定义TableNormal的表格样式对话框的屏幕截图.不幸的是,它是德语的,但也许有人可以提供它的英文版本:

      And here a screenshot of the table style dialog used to define TableNormal. Unfortunately it is in German, but maybe someone can provide an English version of it:

      由于这似乎不适用于大多数用户(除了我以外的任何人……),因此建议我们系统地进行测试.本质上,有4个步骤可能会出错:

      As this does not seem to work for most users (anyone but me …), I suggest we test this systematically. Essentially, there are 4 steps that can go wrong:

      • 错误的RMD(不太可能).
      • 最初生成的DOCX中的差异.
      • TableNormal样式在DOCX中的保存方式不同.
      • 使用参考DOCX格式化最终DOCX的差异.
      • Wrong RMD (unlikely).
      • Differences in the initially generated DOCX.
      • Differences in how the TableNormal style is saved in the DOCX.
      • Differences in how the reference DOCX is used to format the final DOCX.

      因此,我建议使用上面发布的相同最小RMD(pastebin上的完整代码完整代码)来找出结果的出处开始确实有所不同:

      I therefore suggest using the same minimal RMD posted above (full code on pastebin) to find out where the results start do differ:

      • My initially generated DOCX.
      • The same document with TableNormal added: reference.docx
      • The final document.

      这三个文件是在以下系统上生成的:Windows 7/R 3.3.0/RStudio 0.99.896/pandoc 1.15.2/Office 2010.

      The three files are generated on the following system: Windows 7 / R 3.3.0 / RStudio 0.99.896 / pandoc 1.15.2 / Office 2010.

      在Windows 7/R 3.2.4/RStudio 0.99.484/pandoc 1.13.1/Office 2010的系统上,我得到了相同的结果.

      I get the same results on a system with Windows 7 / R 3.2.4 / RStudio 0.99.484 / pandoc 1.13.1 / Office 2010.

      我认为最可能的罪魁祸首是pandoc和Office版本.不幸的是,我目前无法测试其他配置.现在,有趣的是看到以下内容:对于无法正常工作的用户,会发生什么……

      I suppose the most likely culprits are the pandoc and the Office versions. Unfortunately, I cannot test other configurations at the moment. Now it would be interesting to see the following: For users where it does not work, what happens …

      • ...如果您从我的 initial.docx 开始?
      • 如果这不起作用,那么如果您使用我的 reference.docx 怎么办?文件?
      • 如果没有任何效果,那么生成的XML文件(在DOCX容器内部)是否有醒目的区别?请分享您的文件和确切的版本信息.
      • … if you start from my initial.docx?
      • If that does not work, what if you use my reference.docx as reference document?
      • If nothing works, are there eye-catching differences in the generated XML files (inside the DOCX container)? Please share your files and exact version information.

      在许多用户运行这些测试的情况下,应该可以找出导致问题的原因.

      With a number of users running these tests it should be possible to find out what is causing the problems.

      这篇关于针织衫Rmarkdown docx表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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