making a two column text block in ms-word with knitr

余生颓废 提交于 2019-12-12 02:58:58

问题


I'm trying to recreate an existing letter, for bulk generation. I'm using knitr and a reference document in the YAML for styling of the output doc. The whole thing has gone pretty well, except for the two column address block. I have tried kable, pasting the strings with \t tabs between, and playing with the formatting of the reference doc. No dice.

This is the output I want (obviously without the underscores...) :

Date

Client: Foo & Bar____________________ Client's Client: Bar
Address: 1234 Blah St._______________ ID: Numbersnumbers
More Address: City, St. Zip____________ Last Piece: of Info

Body - in paragraph form.

The problem is that the left column is variable length so the right is not justified. Also, kable has a required column label and when I used "nbsp", it still left a dividing line above the block.

Is there a good way to do this that I'm missing?

Thanks in advance.


回答1:


If you're willing to try a package that isn't on CRAN yet, you can install the Gmisc package from GitHub. (Gmisc version 1.1 is on CRAN, but the tools you need to render the docx are in version 1.2, which hasn't been released yet)

library(devtools)
install_github("gforge/Gmisc")

Gmisc gives you the option of knitting a docx_document that will preserve HTML formatting. This gives you a lot more flexibility of formatting than the usual Word output. The file will render as an HTML file, but if you right click and open it with MS Word, you'll see the formatting is preserved.

---
title: ""
output: Gmisc::docx_document
---

```{r, echo=FALSE, message=FALSE}
library(pixiedust)
```

Date: `r format(Sys.Date(), format = "%B %d, %Y")`

```{r, echo=FALSE}
DFrame <- 
  data.frame(
    left = c("Client: Foo & Bar",
             "Address: 1234 Blah St",
             "More Address: City, State ZIP"),
    right = c("Client's Client: Bar",
              "ID: Numbers numbers",
              "Last Piece: of Info"),
    stringsAsFactors = FALSE)

dust(DFrame) %>%
  sprinkle_colnames("", "") %>%
  sprinkle(cols = 1, width = 3, width_units = "in")%>%
  sprinkle_print_method("html")
```



回答2:


I ended up using ReporteRs to generate the documents. You have to build the components of the document (and its formatting) in the R script instead of having an Rmd file, but you can still specify a reference doc for additional styling like headers/footers. You also get a LOT of flexibility for tables, which is cool.



来源:https://stackoverflow.com/questions/34731204/making-a-two-column-text-block-in-ms-word-with-knitr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!