The function replace text values in a flextable with labels. The labels are defined with character named vector.

The function is not written to be fast but to be handy. It does not replace the values in the underlying dataset but replace the defined content in the flextable (as defined with compose()).

labelizor(x, j = NULL, labels, part = "all")

Arguments

x

a flextable object

j

columns selection

labels

a named vector whose names will be used to identify values to replace and values will be used as labels.

part

partname of the table (one of 'all', 'body', 'header', 'footer')

Examples

z <- summarizor(
  x = CO2[-c(1, 4)],
  by = "Treatment",
  overall_label = "Overall")

tab_1 <- tabulator(
  x = z,
  rows = c("variable", "stat"),
  columns = "Treatment",
  blah = as_paragraph(
    as_chunk(
      fmt_2stats(
        stat = stat,
        num1 = value1, num2 = value2,
        cts = cts, pcts = percent
      )
    )
  )
)

ft_1 <- as_flextable(tab_1, separate_with = "variable")


ft_1 <- labelizor(
  x = ft_1, j = c("stat", "variable"),
  labels = c(mean_sd = "Mean (SD)", median_iqr = "Median (IQR)",
             range = "Range", missing = "Missing")
)

ft_1
#> a flextable object.
#> col_keys: `variable`, `stat`, `dummy1`, `nonchilled@blah`, `dummy2`, `chilled@blah`, `dummy3`, `Overall@blah` 
#> header has 1 row(s) 
#> body has 24 row(s) 
#> original dataset sample: 
#>   variable stat cts@nonchilled cts@chilled cts@Overall percent@nonchilled
#> 1    Plant  Qn1              5           0           5              0.125
#> 2    Plant  Qn2              7           0           7              0.175
#> 3    Plant  Qn3              7           0           7              0.175
#> 4    Plant  Qc1              0           7           7              0.000
#> 5    Plant  Qc3              0           7           7              0.000
#>   percent@chilled percent@Overall data_type@nonchilled data_type@chilled
#> 1       0.0000000      0.06097561             discrete          discrete
#> 2       0.0000000      0.08536585             discrete          discrete
#> 3       0.0000000      0.08536585             discrete          discrete
#> 4       0.1666667      0.08536585             discrete          discrete
#> 5       0.1666667      0.08536585             discrete          discrete
#>   data_type@Overall value1@nonchilled value1@chilled value1@Overall
#> 1          discrete                NA             NA             NA
#> 2          discrete                NA             NA             NA
#> 3          discrete                NA             NA             NA
#> 4          discrete                NA             NA             NA
#> 5          discrete                NA             NA             NA
#>   value2@nonchilled value2@chilled value2@Overall blah@nonchilled blah@chilled
#> 1                NA             NA             NA                             
#> 2                NA             NA             NA                             
#> 3                NA             NA             NA                             
#> 4                NA             NA             NA                             
#> 5                NA             NA             NA                             
#>   blah@Overall dummy1 nonchilled@blah dummy2 chilled@blah dummy3 Overall@blah
#> 1                                                                            
#> 2                                                                            
#> 3                                                                            
#> 4                                                                            
#> 5