Cross-referencing summary tables by qwraps2::summary_table()

750 Views Asked by At

I was trying to cross-reference a summary table created using qwraps2::summary_table() in my R Markdown Word report, but it did not work with my .Rmd file below.

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

```

Screenshot of the .docx file using print() as the output method for the qwraps2_summary_table object:

enter image description here

For my other tables created using knitr::kable(), knitr::kable() would generate a table label for cross-referencing, so I tried to output the table using knitr::kable() instead of the print() method. The cross-reference worked, but the table itself was not rendered correctly in my .docx file:

enter image description here

Considering that qwraps2::qable() is a wrapper for knitr::kable(), I tried it, too, but the cross-reference was broken again, and it only half-rendered the table (missing the group titles):

enter image description here

I searched online but did not seem to find a solution to my issue, so will really appreciate someone's help.

1

There are 1 best solutions below

0
Peter On

Version 0.6.0 of qwraps2 has a fix for this problem. The addition of the arguments qable_args and kable_args in the summary_table call will allow for the correct passing of arguments from one call to the next, that is, from summary_table to qable and then to knitr::kable.

An example .Rmd file and a screen shot of the resulting .docx file, created by calling rmarkdown::render on the .Rmd file, are below:

---
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", qable_args = list(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.

enter image description here