From 1bd2884341f3b89af64f176b27a720c85695b22b Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 7 Feb 2024 18:20:05 +0100 Subject: [PATCH] Misleading names using `pretty_names` in combination with `cut()` (#945) * Misleading names using `pretty_names` in combination with `cut()` Fixes #943 * add test --- R/format_parameters.R | 4 ++-- tests/testthat/test-format_parameters.R | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/R/format_parameters.R b/R/format_parameters.R index b425adc1b..f95334ccf 100644 --- a/R/format_parameters.R +++ b/R/format_parameters.R @@ -319,11 +319,11 @@ format_parameters.parameters_model <- function(model, ...) { if (all(grepl(pattern_cut_right, level))) { lower_bounds <- gsub(pattern_cut_right, "\\1", level) upper_bounds <- gsub(pattern_cut_right, "\\2", level) - level <- paste0(as.numeric(lower_bounds) + 1, "-", upper_bounds) + level <- paste0(">", as.numeric(lower_bounds), "-", upper_bounds) } else if (all(grepl(pattern_cut_left, level))) { lower_bounds <- gsub(pattern_cut_left, "\\1", level) upper_bounds <- gsub(pattern_cut_left, "\\2", level) - level <- paste0(lower_bounds, "-", as.numeric(upper_bounds) - 1) + level <- paste0(lower_bounds, "-<", as.numeric(upper_bounds)) } paste0(variable, " ", brackets[1], level, brackets[2]) } diff --git a/tests/testthat/test-format_parameters.R b/tests/testthat/test-format_parameters.R index bae6065c2..5785f2720 100644 --- a/tests/testthat/test-format_parameters.R +++ b/tests/testthat/test-format_parameters.R @@ -381,5 +381,20 @@ withr::with_options( "e42dep [2] * c12hour" ) }) + + test_that("format_parameters, cut", { + data(mtcars) + mtcars$grp <- cut(mtcars$mpg, breaks = c(0, 15, 20, 50)) + out <- model_parameters(lm(wt ~ grp, data = mtcars)) + expect_equal( + attributes(out)$pretty_names, + c( + `(Intercept)` = "(Intercept)", `grp(15,20]` = "grp [>15-20]", + `grp(20,50]` = "grp [>20-50]" + ), + ignore_attr = TRUE + ) + expect_identical(out$Parameter, c("(Intercept)", "grp(15,20]", "grp(20,50]")) + }) } )