Zhi*_*ang 2 r ms-word r-markdown kable
---
output:
word_document: default
---
```{r setup, include=FALSE}
data("mtcars")
library(tidyverse)
library(xtable)
library(sjPlot)
library(kableExtra)
```
```{r, results='asis'}
df <- mtcars %>%
group_by(cyl) %>%
summarise(disp = mean(disp),
wt = mean(wt),
n = n()
)
kable(df)
# tab_df(df)
# xtable(df)
```
Run Code Online (Sandbox Code Playgroud)
我已经尝试过xtable、tab_df、 和kable生成带有表格的Word文档。当“编织到 HTML 文档”时,所有表格看起来都很好。当“knit to Word”时,xtable 不显示表格tab_df,而是kable生成一个只有一列的表格:
kable(df)
cyl
disp
wt
n
4
105.1364
2.285727
Run Code Online (Sandbox Code Playgroud)
flextable过去几天我进行了很多尝试,当您必须使用 Word 时,这可能是最佳选择:
---
output:
word_document: default
---
```{r setup, include=FALSE}
data("mtcars")
library(tidyverse)
library(flextable)
```
```{r, results='asis'}
mtcars %>%
group_by(cyl) %>%
summarise(disp = mean(disp),
wt = mean(wt),
n = n()
) %>%
flextable() %>%
align(part = "all") %>% # left align
set_caption(caption = "Table 1: Example") %>%
font(fontname = "Calibri (Body)", part = "all") %>%
fontsize(size = 10, part = "body") %>%
# add footer if you want
# add_footer_row(values = "* p < 0.05. ** p < 0.01. *** p < 0.001.",
# colwidths = 4) %>%
theme_booktabs() %>% # default theme
autofit()
```
Run Code Online (Sandbox Code Playgroud)