0

I am creating cross tables in rmarkdown using summarytools::ctable.

The tables look good in rmarkdown, except that the first column that has a lot of text is messing up the format if the table output.

I want to wrap the text of the first column so that the actual data does not get squished in the rmarkdown output.

Here is my code:

library(magrittr)
library(dplyr)
library(gtsummary)
library(summarytools)
require(pander)
library(knitr)
library(stringr)
library(readr)

data_in <- readr::read_table2('q1    q2 survey
No  somelongresponsethattakesupmorethanonline--somelongresponsethattakesupmorethanonline c1
No  somelongresponsethattakesupmorethanonline--somelongresponsethattakesupmorethanonline c2
No  somelongresponsethattakesupmorethanonline--somelongresponsethattakesupmorethanonline c1
No  somelongresponsethattakesupmorethanonline--somelongresponsethattakesupmorethanonline c2
No  Yes c1
No  Always c2
Yes No c1
Yes No c2
Yes No c1
NA  NA c2
NA  NA c1
NA  NA c2
NA  NA c1
')


vct_cat <- data_in %>% names(.)



create_xtab <- function(v1, name) {
  ctable(v1,
         data_in$survey,
         prop = 'c',
         chisq = TRUE, 
         # dnn =c(name,'survey'),
         totals = FALSE)
}


for (i in vct_cat) {

  
  tbl <- create_xtab(data_in[[i]], colnames(data_in[i]))   # build summarytools table
  print(tbl)       # print table
}

I know that we can do this with kable Adjusting width of tables made with kable() in RMarkdown documents. But the ctable type is weird and does not accept kable.

Any idea how I can format this table, so that the first column allows text wrapping?? :(

NewBee
  • 990
  • 1
  • 7
  • 26
  • in your `tbl` code it says you are creating a gtsummay table but it doesn't seem like you are creating a gtsummary object, did you mean to use different code – Mike Mar 16 '22 at 20:26
  • @Mike Thank you, that was an error in the comment, not the code! TY – NewBee Mar 16 '22 at 21:38
  • if you try to add newlines with `\n` you get this error: `Error in tableExpand_cpp(cells, cols.width, justify, sep.cols, style) : Pandoc does not support newlines in simple or Rmarkdown table format!` your best option might be to make the text shorter in your `data_in` and make a footnote of what the data represents – Mike Mar 17 '22 at 13:37

0 Answers0