kaz-yos / tableone

R package to create "Table 1", description of baseline characteristics with or without propensity score weighting
https://cran.r-project.org/web/packages/tableone/index.html
214 stars 40 forks source link

Arguments in kableone and print #47

Open malcolmbarrett opened 5 years ago

malcolmbarrett commented 5 years ago

As brought up in #32, kableone() only lets you pass arguments to kable() and not to print.tableone().

I think there are a couple of related issues here. Right now I'm thinking a good PR for this will do the following:

1) Move the code currently in print.tableone to as.data.frame.tableone with printToggle = FALSE to avoid capture.output() 2) change print.tableone to something like

print.tableone <- function(x, {arguments}, ...) {
  x <- as.data.frame(x, {arguments})
  print(x, ...)
}

3) Change kableone() to something like

kableone <- function(x, kable_args = list(), print_args = list()) {
 #  need to evaluate arguments as well
 # ...
  x <- as.data.frame(x, print_args)
  knitr::kable(x, kable_args)
}
malcolmbarrett commented 5 years ago

Actually, I'm realizing that simply defining as.data.frame() might be enough to make kable() work out of box with a tableone object as it calls as.data.frame() internally...

DavisBrian commented 5 years ago

I have a slightly modified version of as.data.frame() that I've been using with kable() fairly successfully.

as.data.frame.TableOne <- function(x, ...) {

  capture.output(print(x, showAllLevels = TRUE, ...) -> x)

  y <- as.data.frame(x)
  y$charactersitic <- na_if(rownames(x), "")
  y <- y %>%
    fill(charactersitic, .direction = "down") %>%
    select(charactersitic, everything())

  rownames(y) <- NULL
  y 
}
Aytan-sudo commented 4 years ago

Before seeing that the problem has already been addressed here, I posted a question on stackoverflow :

https://stackoverflow.com/questions/63700004/missing-the-spaces-with-kableone-package-tableone

The problem with as.data.frame is that he replaces the spaces with characters, like "X..." to indent (with kableone in PDF). The result is not really readable ... And so, options to format other things, like p-value in bold when <0.05 would be greatly appreciated for Rmarkdown report !

iago-pssjd commented 9 months ago

I do not understand this issue and documentation. They claim kableone arguments are only for kable, but if I include the print.TableOne argument nonormal = TRUE it works.