HTML formatted tables in rmarkdown word document

吃可爱长大的小学妹 提交于 2019-12-25 09:11:32

问题


I have to compare some values in data and display weekly trends. I want to show if a value has increased from last week. I have created an rmarkdown report to do it. An example code is shown below, which works perfectly when output: html_document but output is messed up when using output: word_document

---
title: "trials"
author: "Foo Bar"
date: "15 December 2016"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r cars, echo=FALSE, cache=FALSE, message=FALSE}

library(dplyr, quietly = TRUE)
library(abind, quietly = TRUE)
virginica <- iris %>% filter(Species == "virginica") %>% head() %>% select(-Species)
setosa <- iris %>% filter(Species == "setosa") %>% head() %>% select(-Species)

diff_mat <- virginica - setosa


diff_mat[diff_mat<0] <- '<font color="green">&dArr; </font>'
diff_mat[diff_mat>0] <- '<font color="red">&uArr; </font>'
diff_mat[diff_mat == 0] <- '<font color="blue">&hArr; </font>'

datArray <- abind::abind(virginica, diff_mat, along=3)

fin_dat <- apply(datArray,1:2, function(x)paste(x[1],x[2], sep = " "))

knitr::kable(fin_dat, format = "html",
      escape = FALSE, table.attr = "border=1",
      caption = "Changes across species")

```

How can I format the word version of the document similar to the html version?


回答1:


Perhaps render("my_document.rmd", "Grmd::docx_document"). This works decently for html tables but is not compatiple with using a styles template. It does keep the table structure, including column spanners.



来源:https://stackoverflow.com/questions/41156265/html-formatted-tables-in-rmarkdown-word-document

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