Add attributes to a function that specify the expected results. It is used when ard_summary() or ard_mvsummary() errors and constructs an ARD with the correct structure when the results cannot be calculated.

as_cards_fn(f, stat_names)

is_cards_fn(f)

get_cards_fn_stat_names(f)

Arguments

f

(function)
a function

stat_names

(character)
a character vector of the expected statistic names returned by function f

Value

an ARD data frame of class 'card'

Examples

# When there is no error, everything works as if we hadn't used `as_card_fn()`
ttest_works <-
  as_cards_fn(
    \(x) t.test(x)[c("statistic", "p.value")],
    stat_names = c("statistic", "p.value")
  )
ard_summary(
  mtcars,
  variables = mpg,
  statistic = ~ list(ttest = ttest_works)
)
#> # An ARD data frame: 2 × 8
#>   variable context stat_name stat_label     stat fmt_fun warning error 
#> * <chr>    <chr>   <chr>     <chr>        <list>  <list> <list>  <list>
#> 1 mpg      summary statistic statistic  1.89e+ 1       1 <NULL>  <NULL>
#> 2 mpg      summary p.value   p.value    1.53e-18       1 <NULL>  <NULL>

# When there is an error and we use `as_card_fn()`,
#   we will see the same structure as when there is no error
ttest_error <-
  as_cards_fn(
    \(x) {
      t.test(x)[c("statistic", "p.value")]
      stop("Intentional Error")
    },
    stat_names = c("statistic", "p.value")
  )
ard_summary(
  mtcars,
  variables = mpg,
  statistic = ~ list(ttest = ttest_error)
)
#> # An ARD data frame: 2 × 8
#>   variable context stat_name stat_label stat   fmt_fun warning error            
#> * <chr>    <chr>   <chr>     <chr>      <list> <list>  <list>  <list>           
#> 1 mpg      summary statistic statistic  <NULL> <fn>    <NULL>  Intentional Error
#> 2 mpg      summary p.value   p.value    <NULL> <fn>    <NULL>  Intentional Error

# if we don't use `as_card_fn()` and there is an error,
#   the returned result is only one row
ard_summary(
  mtcars,
  variables = mpg,
  statistic = ~ list(ttest = \(x) {
    t.test(x)[c("statistic", "p.value")]
    stop("Intentional Error")
  })
)
#> # An ARD data frame: 1 × 8
#>   variable context stat_name stat_label stat   fmt_fun warning error            
#> * <chr>    <chr>   <chr>     <chr>      <list> <list>  <list>  <list>           
#> 1 mpg      summary ttest     ttest      <NULL> <fn>    <NULL>  Intentional Error