0

I will be pretty straight forward with my question, as can be seen here it is possible to get an LaTeX output from gtsummary objects with as_gt() , gt::as_latex() and a pipe.

Is it possible to do the same but to get an r markdown style code?.

I need this because I would probably need to do some minor editing to the tables and because this way I can knit them in *.pdf or *.docx

By "r markdown style code" I mean that I would like an output in the console that looks something like this, so I can paste it in some R Markdown document and format it even further:

|  **Grupos de edad** | **Abril** | **Mayo** | **Junio** | **Julio** |
|--------------------:|----------:|---------:|----------:|----------:|
|         **18 a 24** |     5.72% |    5.97% |     6.83% |     5.37% |
|         **25 a 39** |    44.45% |   40.19% |    36.75% |    42.41% |
|         **40 a 59** |    41.04% |   44.21% |    47.15% |    42.26% |
|        **60 o más** |     8.78% |    9.35% |     8.80% |    9.54\$ |
| **No especificado** |      0.0% |    0.28% |     0.47% |     0.43% |
|           **Total** |    100.0% |   100.0% |    100.0% |    100.0% |
|                   n |   4572879 |  3966146 |   4459403 |   3529408 |
|                     |           |          |           |           |

esteban
  • 102
  • 8

2 Answers2

0

I am not sure what you mean by "r markdown style code". But you can convert a gtsummary table to multiple output types and continue formatting from there. For example, the flextable package has great support for Word output. If you've got an .Rmd document being rendered to Word, convert your gtsummary table to flextable, and you can use one of the (very) many flextable functions to continue formatting your table. In the example below, we highlight some of the cells in the table.

library(gtsummary)
library(flextable)
packageVersion("gtsummary")
#> [1] '1.5.2'

tbl <-
  trial %>%
  select(age, grade, trt) %>%
  tbl_summary(by = trt) %>%
  as_flex_table() %>%
  style(i = 2,   # second row
        j = 2:3, # columns 2 and 3 
        pr_t = fp_text_default(shading.color = "yellow"))

enter image description here Created on 2022-03-10 by the reprex package (v2.0.1)

You can read more about gtsummary and Rmarkdown here https://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
  • Just edited my question to show an example of desired output and what I mean by "r markdown style code" – esteban Mar 11 '22 at 00:51
  • 1
    Ah, I see now, thanks. That is called markdown syntax. You can convert a gtsummary to markdown format with `trial %>% tbl_summary(include = age) %>% as_kable(format = "pipe")` – Daniel D. Sjoberg Mar 11 '22 at 01:23
  • thanks to you, sorry for not using the correct name for what I was refering to. – esteban Mar 11 '22 at 03:52
0

I am not sure if it works for you, but one possible way is to add a latex code chunk in your r markdown. For example,

library(gtsummary)
library(gt)
mod1 <- lm(mpg~cyl, data = mtcars)
tbl_regression(mod1) %>% as_gt() %>% as_latex()

This will give you the latex code of the table. Now insert a latex code chunk in your r markdown document. The header of that code chunk will have

```{=latex}
your latex code
```

Don't forget to add = sign in the latex code chunk. You can edit your latex code whatever way you want. This will be valid even if your output format is word_document.

Eva
  • 663
  • 5
  • 13
  • I think this could work, although it would be wonderful to get an raw output which I can paste on a R markdown document. – esteban Mar 11 '22 at 00:59
  • As Daniel Sjoberg has said, you can try to edit it as flex table or kableextra object. Another alternative for you might be to store the table first. For example, tbl1 <- tbl_regression(mod1). Then Output <- ifelse(knitr::is_word_output, your_flex_table_code, your_latex_code) should also work. – Eva Mar 11 '22 at 01:40