Skip to contents

Mostly for internal use, but can be useful when users supply a function to the method argument of datasummary_correlation.

Usage

datasummary_correlation_format(
  x,
  fmt,
  leading_zero = FALSE,
  diagonal = NULL,
  upper_triangle = NULL
)

Arguments

x

square numeric matrix

fmt

how to format numeric values: integer, user-supplied function, or modelsummary function.

  • Integer: Number of decimal digits

  • User-supplied functions:

    • Any function which accepts a numeric vector and returns a character vector of the same length.

  • modelsummary functions:

    • fmt = fmt_significant(2): Two significant digits (at the term-level)

    • fmt = fmt_sprintf("%.3f"): See ?sprintf

    • fmt = fmt_identity(): unformatted raw values

leading_zero

boolean. If FALSE, leading zeros are removed

diagonal

character or NULL. If character, all elements of the diagonal are replaced by the same character (e.g., "1").

upper_triangle

character or NULL. If character, all elements of the upper triangle are replaced by the same character (e.g., "" or ".").

Examples

library(modelsummary)

dat <- mtcars[, c("mpg", "hp", "disp")]

cor_fun <- function(x) {
  out <- cor(x, method = "kendall")
  datasummary_correlation_format(
    out,
    fmt = 2,
    upper_triangle = "x",
    diagonal = ".")
}

datasummary_correlation(dat, method = cor_fun)
#> <table class="table" style="width: auto !important; margin-left: auto; margin-right: auto;">
#>  <thead>
#>   <tr>
#>    <th style="text-align:left;">   </th>
#>    <th style="text-align:right;"> mpg </th>
#>    <th style="text-align:right;"> hp </th>
#>    <th style="text-align:right;"> disp </th>
#>   </tr>
#>  </thead>
#> <tbody>
#>   <tr>
#>    <td style="text-align:left;"> mpg </td>
#>    <td style="text-align:right;"> . </td>
#>    <td style="text-align:right;"> x </td>
#>    <td style="text-align:right;"> x </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:left;"> hp </td>
#>    <td style="text-align:right;"> −.74 </td>
#>    <td style="text-align:right;"> . </td>
#>    <td style="text-align:right;"> x </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:left;"> disp </td>
#>    <td style="text-align:right;"> −.77 </td>
#>    <td style="text-align:right;"> .67 </td>
#>    <td style="text-align:right;"> . </td>
#>   </tr>
#> </tbody>
#> </table>