使用knitr在word文档中显示相关矩阵
Display correlation matrix in word document using knitr
我有 .rmd 文件,我想在下面编织并使用 table 创建一个 word 文档。问题是,编织后我没有得到 table,而是一个在另一个下面。
如果还有其他选择,我愿意接受
---
title: "Correlation table"
author: "mk"
date: "17/01/2022"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(scipub)
library(htmlTable)
library(magrittr)
library(mvtnorm)
set.seed(666L)
```
```{r,echo=FALSE,include=FALSE}
dat <- as.data.frame(rmvnorm(50, sigma = toeplitz(3:1)))
colnames(dat) <- c("V1", "V2", "V3")
cortable <- correltable(dat, html = FALSE)
table2 <- as.data.frame(matrix(as.character(cortable$table), nrow = 3))
caption <- cortable[["caption"]]
Means <- formatC(colMeans(dat))
Sds <- formatC(apply(dat, 2L, sd))
table1 <- data.frame(Mean = Means, SD = Sds)
```
```{r, results='asis',echo=FALSE}
css.cell <- matrix("padding: 5px;", ncol = 6L, nrow = 4L)
css.cell[, 1L] <-
paste(css.cell[, 1L], "font-weight: bold;") # <-- bold row names
cbind(table1, table2) %>%
addHtmlTableStyle(css.cell = css.cell) %>%
htmlTable(caption = caption)
```
简单的方法是:
---
title: "Correlation table"
author: "mk"
date: "17/01/2022"
output: word_document
---
knitr::opts_chunk$set(echo = TRUE)
library(scipub)
library(htmlTable)
library(magrittr)
library(mvtnorm)
set.seed(666L)
dat <- as.data.frame(rmvnorm(50, sigma = toeplitz(3:1)))
colnames(dat) <- c("V1", "V2", "V3")
cortable <- correltable(dat, html = FALSE)
table2 <- as.data.frame(matrix(as.character(cortable$table), nrow = 3))
caption <- cortable[["caption"]]
Means <- formatC(colMeans(dat))
Sds <- formatC(apply(dat, 2L, sd))
table1 <- data.frame(Mean = Means, SD = Sds)
css.cell <- matrix("padding: 5px;", ncol = 6L, nrow = 4L)
css.cell[, 1L] <-
paste(css.cell[, 1L], "font-weight: bold;") # <-- bold row names
cbind(table1, table2) %>%
addHtmlTableStyle(css.cell = css.cell) %>%
htmlTable(caption = caption)
df <- cbind(table1, table2)
knitr::kable(df)
您可以用不同的方式玩转风格:
kable
: https://bookdown.org/yihui/rmarkdown-cookbook/kable.html
flextable
:
officeverse
设置:https://ardata-fr.github.io/officeverse/officer-for-word.html#tables
问候,Grzegorz
我有 .rmd 文件,我想在下面编织并使用 table 创建一个 word 文档。问题是,编织后我没有得到 table,而是一个在另一个下面。
如果还有其他选择,我愿意接受
---
title: "Correlation table"
author: "mk"
date: "17/01/2022"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(scipub)
library(htmlTable)
library(magrittr)
library(mvtnorm)
set.seed(666L)
```
```{r,echo=FALSE,include=FALSE}
dat <- as.data.frame(rmvnorm(50, sigma = toeplitz(3:1)))
colnames(dat) <- c("V1", "V2", "V3")
cortable <- correltable(dat, html = FALSE)
table2 <- as.data.frame(matrix(as.character(cortable$table), nrow = 3))
caption <- cortable[["caption"]]
Means <- formatC(colMeans(dat))
Sds <- formatC(apply(dat, 2L, sd))
table1 <- data.frame(Mean = Means, SD = Sds)
```
```{r, results='asis',echo=FALSE}
css.cell <- matrix("padding: 5px;", ncol = 6L, nrow = 4L)
css.cell[, 1L] <-
paste(css.cell[, 1L], "font-weight: bold;") # <-- bold row names
cbind(table1, table2) %>%
addHtmlTableStyle(css.cell = css.cell) %>%
htmlTable(caption = caption)
```
简单的方法是:
---
title: "Correlation table"
author: "mk"
date: "17/01/2022"
output: word_document
---
knitr::opts_chunk$set(echo = TRUE)
library(scipub)
library(htmlTable)
library(magrittr)
library(mvtnorm)
set.seed(666L)
dat <- as.data.frame(rmvnorm(50, sigma = toeplitz(3:1)))
colnames(dat) <- c("V1", "V2", "V3")
cortable <- correltable(dat, html = FALSE)
table2 <- as.data.frame(matrix(as.character(cortable$table), nrow = 3))
caption <- cortable[["caption"]]
Means <- formatC(colMeans(dat))
Sds <- formatC(apply(dat, 2L, sd))
table1 <- data.frame(Mean = Means, SD = Sds)
css.cell <- matrix("padding: 5px;", ncol = 6L, nrow = 4L)
css.cell[, 1L] <-
paste(css.cell[, 1L], "font-weight: bold;") # <-- bold row names
cbind(table1, table2) %>%
addHtmlTableStyle(css.cell = css.cell) %>%
htmlTable(caption = caption)
df <- cbind(table1, table2)
knitr::kable(df)
您可以用不同的方式玩转风格:
kable
: https://bookdown.org/yihui/rmarkdown-cookbook/kable.htmlflextable
:officeverse
设置:https://ardata-fr.github.io/officeverse/officer-for-word.html#tables
问候,Grzegorz