简体   繁体   English

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

[英]Table captions underneath tables if using gtsummary + flextable in officedown

I feel like I am missing something easy.我觉得我错过了一些简单的东西。 Fingers crossed thats it.手指交叉就是这样。

Im trying to produce tables using gtsummary and flextable in officedown.我试图在 officedown 中使用 gtsummary 和 flextable 制作表格。 I'm using the yaml options from the "Advanced word document" template in officedown.我正在使用 officedown 中“高级 word 文档”模板中的 yaml 选项。

Below is my code.下面是我的代码。 The first 2 tables have the captions underneath and the third is on top, as it should be!前两张桌子下面有标题,第三张在上面,应该是这样!

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)!)

Not an expert in flextable and officedown .不是flextableofficedown的专家。 But one option which worked for me was to set the position of the table captions for the R Markdown document via但是对我有用的一个选项是通过设置 R Markdown 文档的表格标题的 position

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

Full reproducible code:完整的可重现代码:

---
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)!)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM