Knitr & Rmarkdown docx tables

前端 未结 5 964
北恋
北恋 2020-12-13 18:42

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

For example in my y

5条回答
  •  鱼传尺愫
    2020-12-13 18:55

    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)
    ```
    
    • 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
      ---
      
    • Knit to DOCX again – the style should now be applied.

    Following the steps I described above yields this output:

    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:


    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:

    • 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.

    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.

    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.

    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.

    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 …

    • … 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.

提交回复
热议问题