Knitr & Rmarkdown docx tables

前端 未结 5 959
北恋
北恋 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 19:03

    I was able to get my word output to use a default table style that I defined in a reference .docx.

    Instead of 'TableNormal', the table style it defaulted to was 'Table'.

    I discovered this by knitting an rmarkdown with a kable.

    ---
    date: "December 1, 2017"
    output: 
      word_document:
        reference_docx: Template.docx
    ---
    `r knitr::kable(source)`
    

    Then I took a look at that generated document's XML to see what style it had defaulted to.

    require(XML)
    
    docx.file <- "generated_doc.docx"
    
    ## unzip the docx converted by Pandoc
    system(paste("unzip", docx.file, "-d temp_dir"))
    document.xml <- "temp_dir/word/document.xml"
    
    doc <- xmlParse(document.xml)
    tblStyle <- getNodeSet(xmlRoot(doc), "//w:tblStyle")
    
    tblStyle
    

    I defined the 'Table' style to put some color and borders in the reference docx. This works for one standard table style throughout the document, I haven't found a way to use different styles throughout.

    This stayed true even after I opened the reference doc and edited it.

提交回复
热议问题