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

nkkqxpd9  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(94)

我试图在我的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):


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文件的屏幕截图:

![](https://i.stack.imgur.com/t9c3R.png)

对于我使用`knitr::kable()`创建的其他表,`knitr::kable()`将生成一个表标签用于交叉引用,所以我尝试使用`knitr::kable()`而不是`print()`方法输出表。交叉引用工作,但表本身在我的.docx文件中没有正确呈现:

![](https://i.stack.imgur.com/5iozl.png)

考虑到`qwraps2::qable()`是`knitr::kable()`的 Package 器,我也尝试了它,但是交叉引用再次被破坏,它只呈现了表的一半(缺少组标题):

![](https://i.stack.imgur.com/d7XWu.png)

我在网上搜索,但似乎没有找到解决我的问题,所以真的很感激有人的帮助。
ercv8c1e

ercv8c1e1#

qwraps2的0.6.0版本修复了这个问题。在summary_table调用中添加参数qable_argskable_args将允许从一个调用到下一个调用正确传递参数,即从summary_tableqable,然后到knitr::kable
下面是一个.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")
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.

summary_table(x = tdf, summaries = summary_str, by = "disease", qable_args = list(kable_args = list(caption = "Personal summary")))

More text goes here.

knitr::kable(tdf, caption = "simple table")

More text goes here.

字符串

![](https://i.stack.imgur.com/LABUq.png)

相关问题