harrelfe / Hmisc

Harrell Miscellaneous
Other
204 stars 81 forks source link

Issue with the print function #165

Open sylvainjuchet opened 1 year ago

sylvainjuchet commented 1 year ago

I have an issue when using the print function with describe. For example:

getHdata(pbc) w <- describe(pbc) print(w, which='categorical')

Give exactly the same result as:

getHdata(pbc) w <- describe(pbc) print(w, which='continuous')

If I'm using the plot function it works well. Do you have an idea why ? This raise a lot of issues when trying to work with qreport.

Thanks

couthcommander commented 1 year ago

I don't believe print.describe supports the "which" argument. You could write your own, using a similar determination as Hmisc:

isCat <- function(x, reqChar = FALSE) {
  if('Sum' %in% names(x$counts)) return(TRUE)
  v <- x$values
  meetCrit <- length(v) && is.list(v) && all(names(v) == c("value", "frequency")) && length(v$value) <= 20
  if(reqChar) meetCrit && is.character(v$value) else meetCrit
}
printDesc <- function(x, type = 'cat', ...) {
  valIsCat <- vapply(x, isCat, logical(1), ...)
  switch(type, cat = x[valIsCat], cont = x[!valIsCat], x)
}
printDesc(w)
printDesc(w, 'cont')
printDesc(w, 'both') # same as `print(w)`

In this example, using reqChar = FALSE allows the "stage" variable to be classified as categorical even though it's values are not character.

I'm not sure if this would work in the context of "qreport".