insightsengineering / cards

CDISC Analysis Results Data
https://insightsengineering.github.io/cards/
33 stars 2 forks source link

Add function to print errors and warnings #118

Closed ddsjoberg closed 9 months ago

ddsjoberg commented 10 months ago

Perhaps something like this:

ard <- 
  mtcars |> 
  cards::ard_continuous(
    variables = c("mpg", "hp"),
    statistics = ~list(
      mean = \(x) mean(x),
      mean_warn = \(x) {warning("This is your warning!"); mean(x)},
      errr = \(x) stop("That's it! STOP!")
    )
  )
ard

print_messages <- function(x) {
  ard_warning <- x |> dplyr::filter(cards:::map_lgl(.data$warning, Negate(is.null)))
  ard_error <- x |> dplyr::filter(cards:::map_lgl(.data$error, Negate(is.null)))

  # this section needs to be updated to: 
  #  1. add the variable level if populated
  #  2. add the grouping variables/levels if populated
  cli::cli_bullets(c(
    "The following warnings were observed:",
    "!" = "Variable {.var {ard_warning$variable[[1]]}} for statistic {.val {ard_warning$stat_name[[1]]}}: {cli::col_yellow(ard_warning$warning[[1]])}",
    "!" = "Variable {.var {ard_warning$variable[[2]]}} for statistic {.val {ard_warning$stat_name[[2]]}}: {cli::col_yellow(ard_warning$warning[[2]])}"
  ))

  cli::cli_bullets(c(
    "The following errors occured:",
    "x" = "Variable {.var {ard_error$variable[[1]]}} for statistic {.val {ard_error$stat_name[[1]]}}: {cli::col_red(ard_error$error[[1]])}",
    "x" = "Variable {.var {ard_error$variable[[2]]}} for statistic {.val {ard_error$stat_name[[2]]}}: {cli::col_red(ard_error$error[[2]])}"
  ))
}

print_messages(ard)

image

For ARDs like ard_ttest() we can have one error for every statistic returned. In these cases, the messages should be grouped by the groups, variables, and error message. We can list all the statistics the error applies applies to.

ddsjoberg commented 10 months ago

@bzkrouse what do you think of this format? should we change anything?