benjaminrich / table1

79 stars 26 forks source link

t1flex and t1kable returns error with extra.col #93

Closed eliascrapa closed 1 year ago

eliascrapa commented 2 years ago

Hi,

I wanted to convert a table1 table to a flextable or kable but both the t1flex and t1kabel return an error saying that more columns are there than expected.

Code run: overview <- table1(~ Age | Year+ Modality, data=combinedtable, overall = F, extra.col=list(P-value=pvalueANOVA), extra.col.pos = (3),caption = paste0("Table 1: Comparison of c CT and MR"))

t1flex(overview)

this gives the error: Error in flextable::add_header_row(out, values = c("", labels$groups), : colwidths' sum must be equal to the number of col_keys (8)

t1kable(overview)

this gives the error Error in htmlTable_add_header_above(kable_input, header, bold, italic, : The new header row you provided has a total of 7 columns but the original kable_input has 8.

Thanks

benjaminrich commented 1 year ago

Can you provide a Minimal Reproducible Example (MRE)? I can't run your code as it stands, so it's hard to identify the source of the issue.

eliascrapa commented 1 year ago

Hi,

sorry for not including more information. I made an example and am attaching data that reproduces the problem. The problem comes when adding the p-value column.

Data: Year | Age | Modality 2021 | 34 | CT 2019 | 72 | CT 2022 | 75 | MR 2019 | 34 | CT 2022 | 61 | CT 2022 | 61 | MR 2019 | 55 | CT 2019 | 51 | MR 2019 | 36 | CT 2022 | 50 | MR 2021 | 46 | MR 2021 | 69 | CT 2022 | 68 | MR 2021 | 33 | MR 2021 | 60 | CT 2021 | 66 | CT 2019 | 60 | CT 2019 | 71 | CT 2022 | 67 | CT 2022 | 53 | MR

Code:

library(flextable) library(table1)

data <- readRDS("data.Rds")

rndr <- function(x, name, ...) { if (!is.numeric(x)) return(render.categorical.default(x)) what <- switch(name, age = "Mean (SD)") parse.abbrev.render.code(c( what))(x) #(c( "", what))(x) }

pvalueANOVA <- function(x, ...) { y <- unlist(x) g <- factor(rep(1:length(x), times=sapply(x, length)))

if (is.numeric(y)) {

For numeric variables, perform a standard 2-sample t-test

ano <- aov(y ~ g)
p <- summary(ano)[[1]][[5]][1]

} else { p <- chisq.test(table(y, g))$p.value }

c("", sub("<", "<", format.pval(p, digits=3, eps=0.001))) } overview <- table1(~ Age| Year+ Modality, data=data, overall = F, extra.col=list(P-value \n t-test=pvalueANOVA))

t1flex(overview)

Error:

Error in flextable::add_header_row(out, values = c("", labels$groups), : colwidths' sum must be equal to the number of col_keys (8)

benjaminrich commented 1 year ago

Thanks for providing the extra info, and sorry again for the delay (I've been real busy). I pushed a fix for this in 2bd66ff. Please give it a try and see if it does what you want. Thanks.

benjaminrich commented 1 year ago

Fixed in v1.4.3