2

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.

elarry
  • 521
  • 2
  • 7
  • 20
  • Please check the following answer https://www.reddit.com/r/rstats/comments/8w4zkl/summary_table_from_package_qwraps2_does_not_render/ – Nareman Darwish Jan 03 '20 at 17:14
  • Thanks, @NaremanDarwish. If you meant the "summary_table from package qwraps2 does not render" question and the solution of adding `results="asis"` to the R code chunk option, I had come across them before - I have the option in my code chunk. My problem is: the `print()` output of the qwraps2_summary_table object rendered the table correctly, but couldn't generate a table label; the `knitr::kable()` generated a label for cross-referencing, but did not render the table; `qwraps2::qable()` being a function in `qwraps2` and a wrapper of `kable()` couldn't marry the two to do the trick, either... – elarry Jan 03 '20 at 17:45
  • The issue here results from the way qwraps2 uses the argument `format` and how that argument is passed to knitr::kable. The bad news is that right now, qwraps2 v 0.4.2 will not support this feature of knitr. In general I tend to explicitly write labels and captions for tables and figures in my documents and I don't rely on the automated construction from knitr. I will revisit how qwraps2 passes arguments to `knitr::kable`. Please refer to the [github issue](https://github.com/dewittpe/qwraps2/issues/84) for development updates. I will cross post here when appropriate. – Peter Jan 11 '20 at 14:33
  • @Peter Thanks very much for your reply! It'll be very useful for me (and other R Markdown beginners like me) to learn from you how you explicitly cross-reference `qwraps2` tables in R Markdown document for Word output. Meanwhile, I will watch the discussion on the GitHub issue page. Many thanks! – elarry Jan 13 '20 at 10:30

0 Answers0