NewBee
NewBee

Reputation: 1040

Adjust width of ctable/summarytool table

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?? :(

Upvotes: 0

Views: 209

Answers (0)

Related Questions