Renaming categorical variables and their levels, for instance for summary tables, can be fiddly. This function generates code in which only the new names need to be modified, and which can then be passed to either rename_cat_variables() or directly to report_cat_vars()

get_rename_tribbles(
  data,
  ...,
  show = TRUE,
  which = c("both", "vars", "levels"),
  max_levels = 20
)

Arguments

data

A dataframe that contains the variables - only used to extract their possible levels.

...

The variables to be included in the rename tribbles. If none are specified, all are included, unless there are more than max_level variables in the data

show

Logical - should the output be printed to the console. In any case, it is returned invisibly

which

Should tribble code be generated for variables ("vars"), levels ("levels") or both ("both") (default)

max_levels

The maximum number of levels before a variable is dropped from the levels_tribble. Defaults to 20

Value

Code to be edited and passed to tibble::tribble() to create var_names and level_names arguments for rename_cat_variables() and report_cat_vars(). 'New' variable and level names are minimally transformed, but are primarily intended to be placeholders.

Details

Only categorical variables should be passed to the function if code for levels is requested. If a variable has more than 20 distinct values, it is dropped from the levels-tribble-code, unless the max_levels argument is increased.

Examples

get_rename_tribbles(iris)
#> var_names <-  tibble::tribble(
#>   ~old,            ~new,            
#>    "Sepal.Length",  "Sepal.length", 
#>    "Sepal.Width",   "Sepal.width",  
#>    "Petal.Length",  "Petal.length", 
#>    "Petal.Width",   "Petal.width",  
#>    "Species",       "Species"
#> )
#> level_names <-  tibble::tribble(
#>   ~var,       ~level_old,    ~level_new,    
#>    "Species",  "setosa",      "Setosa",     
#>    "Species",  "versicolor",  "Versicolor", 
#>    "Species",  "virginica",   "Virginica"
#> )