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 ± SD" = list(
    "Age (yr)" = ~ mean_sd(age, digits = 0)
  ),
  "BMI, mean ± 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:
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:
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):
I searched online but did not seem to find a solution to my issue, so will really appreciate someone's help.


