Table 如果在 officedown 中使用 gtsummary + flextable,则在表格下方添加标题

Table captions underneath tables if using gtsummary + flextable in officedown

我觉得我错过了一些简单的东西。祈祷就是这样。

我正在尝试在 officedown 中使用 gtsummary 和 flextable 生成表格。我正在使用 officedown 中“高级 word 文档”模板中的 yaml 选项。

下面是我的代码。前两张表的标题在下面,第三张在上面,这是应该的!

R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] flextable_0.6.10     forcats_0.5.1        stringr_1.4.0       
 [4] dplyr_1.0.7          purrr_0.3.4          readr_2.0.2         
 [7] tidyr_1.1.4          tibble_3.1.5         ggplot2_3.3.5       
[10] tidyverse_1.3.1      gtsummary_1.5.0      palmerpenguins_0.1.0
[13] janitor_2.1.0        officer_0.4.1        officedown_0.2.3    


---
date:  "Last compiled on `r format(Sys.time(), '%d %B, %Y')`"
author: "Kristy Robledo"
title: "Table captions example"
output: 
  officedown::rdocx_document:
    mapstyles:
      Normal: ['First Paragraph']
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, fig.cap = TRUE)
library(officedown)
library(officer)
library(palmerpenguins)
library(gtsummary)
library(tidyverse)
library(flextable)
```


## My tables

```{r,tab.id="peng" }

penguins %>%
  tbl_summary(by=species) %>%
  as_flex_table() %>%
  set_caption("Flextable Caption by set_caption") 

```

```{r,tab.id="peng2", tab.cap="Caption by knitr" }

penguins %>%
  tbl_summary(by=species) %>%
  as_flex_table() 

```

### Tables from example

```{r tab.cap="caption 1", tab.id="mtcars"}
head(mtcars)
```


## Reference

see  table \@ref(tab:mtcars) for how I want it and these tables for underneath! 
(table \@ref(tab:peng) and  table \@ref(tab:peng2)!)

不是 flextableofficedown 方面的专家。但是对我有用的一个选择是通过

为 R Markdown 文档设置 table 标题的位置

knitr::opts_chunk$set(tab.topcaption = TRUE).

完整的可重现代码:

---
date:  "Last compiled on `r format(Sys.time(), '%d %B, %Y')`"
author: "Kristy Robledo"
title: "Table captions example"
output: 
  officedown::rdocx_document:
    mapstyles:
      Normal: ['First Paragraph']
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, fig.cap = TRUE)
library(officedown)
library(officer)
library(palmerpenguins)
library(gtsummary)
library(tidyverse)
library(flextable)
```

```{r}
knitr::opts_chunk$set(tab.topcaption = TRUE)
```

## My tables

```{r,tab.id="peng" }
penguins %>%
  tbl_summary(by=species) %>%
  as_flex_table() %>%
  set_caption("Flextable Caption by set_caption") 

```

```{r, tab.id="peng2", tab.cap="Caption by knitr"}
penguins %>%
  tbl_summary(by=species) %>%
  as_flex_table() 
```

### Tables from example

```{r tab.cap="caption 1", tab.id="mtcars"}
head(mtcars)
```

## Reference

see  table \@ref(tab:mtcars) for how I want it and these tables for underneath! 
(table \@ref(tab:peng) and  table \@ref(tab:peng2)!)