通过 qwraps2::summary_table() 交叉引用汇总表

问题描述 投票:0回答:1

我试图在我的 R Markdown Word 报告中交叉引用使用

qwraps2::summary_table()
创建的汇总表,但它不适用于下面的 .Rmd 文件。

---
title: "Summary table in R Markdown"
output: bookdown::word_document2
---

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

A summary of the group is in Table \@ref(tab:summ-tab):

```{r summ-tab, results="asis"}

library(tidyverse)
library(qwraps2)
options(qwraps2_markup = "markdown")

tdf <- tibble(
  age = runif(n = 30, min = 20, max = 40),
  sex = sample(c("M", "F"), size = 30, replace = TRUE),
  bmi = runif(n = 30, min = 15, max = 45),
  disease = sample(1:2, size = 30, replace = TRUE)
)

summary_str <- list(
  "Sex, n (%)" = list(
    "Male" = ~ n_perc0(sex == "M", digits = 1),
    "Female" = ~ n_perc0(sex == "F", digits = 1)
  ),
  "Age, mean &plusmn; SD" = list(
    "Age (yr)" = ~ mean_sd(age, digits = 0)
  ),
  "BMI, mean &plusmn; SD" = list(
    "BMI (kg m^-2^)" = ~ mean_sd(bmi, digits = 1)
  )
)

tdf %>%
  group_by(disease) %>% 
  summary_table(summary_str) %>% 
  print()      
  # knitr::kable(caption = "Personal summary")
  # qable(caption = "Personal summary")

```

使用

print()
作为
qwraps2_summary_table
对象的输出方法的 .docx 文件的屏幕截图:

对于使用

knitr::kable()
创建的其他表格,
knitr::kable()
会生成用于交叉引用的表格标签,因此我尝试使用
knitr::kable()
而不是
print()
方法来输出表格。交叉引用有效,但表格本身在我的 .docx 文件中未正确呈现:

考虑到

qwraps2::qable()
knitr::kable()
的包装,我也尝试了,但是交叉引用再次被破坏,并且它只渲染了一半的表格(缺少组标题):

我在网上搜索,但似乎没有找到解决我的问题的方法,因此非常感谢有人的帮助。

r ms-word bookdown cross-reference qwraps2
1个回答
0
投票

qwraps2 0.6.0 版本已修复此问题。在

qable_args
调用中添加参数
kable_args
summary_table
将允许将参数从一个调用正确传递到下一个调用,即从
summary_table
qable
,然后到
knitr::kable
.

目前,v0.6.0 仅在 https://github.com/dewittpe/qwraps2 上可用,但几天后将发布到 CRAN。

示例 .Rmd 文件和生成的 .docx 文件的屏幕截图(通过在 .Rmd 文件上调用

rmarkdown::render
创建)如下:

---
title: "Summary table in R Markdown"
output: bookdown::word_document2
---

```{r setup, include=FALSE}
library(qwraps2)
options(qwraps2_markup = "markdown")
```

```{r label = "summ_tab_build", include = FALSE}
tdf <- data.frame(
  age = runif(n = 30, min = 20, max = 40),
  sex = sample(c("M", "F"), size = 30, replace = TRUE),
  bmi = runif(n = 30, min = 15, max = 45),
  disease = sample(1:2, size = 30, replace = TRUE)
)

summary_str <- list(
  "Sex, n (%)" = list(
    "Male" = ~ n_perc0(sex == "M", digits = 1),
    "Female" = ~ n_perc0(sex == "F", digits = 1)
  ),
  "Age, mean &plusmn; SD" = list(
    "Age (yr)" = ~ mean_sd(age, digits = 0)
  ),
  "BMI, mean &plusmn; SD" = list(
    "BMI (kg m^-2^)" = ~ mean_sd(bmi, digits = 1)
  )
)
```

A summary of the the data by disease group is in Table \@ref(tab:stab) and is
generated via `qwraps2::summary_table`.
More text goes here ...  Table \@ref(tab:stab2) uses `knitr::kable` directly.

```{r label = "stab", echo = FALSE, results = "asis"}
summary_table(x = tdf, summaries = summary_str, by = "disease", kable_args = list(caption = "Personal summary"))
```

More text goes here.

```{r label = 'stab2', echo = FALSE, results = "asis"}
knitr::kable(tdf, caption = "simple table")
```

More text goes here.

© www.soinside.com 2019 - 2024. All rights reserved.