Skip to content

Commit

Permalink
removing strict and quiet from tidy_add_variable_labels()
Browse files Browse the repository at this point in the history
  • Loading branch information
larmarange committed Aug 23, 2024
1 parent 089150f commit 9e15526
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 62 deletions.
2 changes: 1 addition & 1 deletion R/tidy_add_term_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ tidy_add_term_labels <- function(x,
}

if (!"var_label" %in% names(x)) {
x <- x |> tidy_add_variable_labels(model = model, quiet = quiet)
x <- x |> tidy_add_variable_labels(model = model)
}
if (!"contrasts" %in% names(x)) {
x <- x |> tidy_add_contrasts(model = model)
Expand Down
22 changes: 4 additions & 18 deletions R/tidy_add_variable_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#' A tidy tibble as produced by `tidy_*()` functions.
#' @param labels ([`formula-list-selector`][gtsummary::syntax])\cr
#' An optional named list or a named vector of custom variable labels.
#' @param interaction_sep (`string`)\cr
#' Separator for interaction terms.
#' @param model (a model object, e.g. `glm`)\cr
#' The corresponding model, if not attached to `x`.
#' @inheritParams tidy_plus_plus
Expand All @@ -36,17 +34,15 @@
#' tidy_and_attach() |>
#' tidy_add_variable_labels(
#' labels = list(
#' "(Intercept)" = "Custom intercept",
#' Sex = "Gender",
#' "Class:Age" = "Custom label"
#' "(Intercept)" ~ "Custom intercept",
#' Sex ~ "Gender",
#' "Class:Age" ~ "Custom label"
#' )
#' )
tidy_add_variable_labels <- function(x,
labels = NULL,
interaction_sep = " * ",
model = tidy_get_model(x),
quiet = FALSE,
strict = FALSE) {
model = tidy_get_model(x)) {
if (is.null(model)) {
cli::cli_abort(c(
"{.arg model} is not provided.",
Expand Down Expand Up @@ -95,16 +91,6 @@ tidy_add_variable_labels <- function(x,
var_labels <- var_labels |>
.update_vector(additional_labels)

# check if all elements of labels are in x
# show a message otherwise
not_found <- setdiff(names(labels), names(var_labels))
if (length(not_found) > 0 && !quiet) {
cli_alert_danger("{.code {not_found}} terms have not been found in {.code x}.")
}
if (length(not_found) > 0 && strict) {
cli::cli_abort("Incorrect call with `labels=`. Quitting execution.", call = NULL)
}

var_labels <- var_labels |>
.update_vector(labels)

Expand Down
6 changes: 3 additions & 3 deletions R/tidy_plus_plus.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ tidy_plus_plus <- function(model,
res <- res |>
tidy_add_variable_labels(
labels = variable_labels,
interaction_sep = interaction_sep,
quiet = quiet
interaction_sep = interaction_sep
) |>
tidy_add_term_labels(
labels = term_labels,
Expand All @@ -236,7 +235,8 @@ tidy_plus_plus <- function(model,
res <- res |>
tidy_add_header_rows(
show_single_row = {{ show_single_row }},
strict = strict, quiet = quiet
strict = strict,
quiet = quiet
)
}

Expand Down
18 changes: 4 additions & 14 deletions man/tidy_add_variable_labels.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 0 additions & 26 deletions tests/testthat/test-add_variable_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,6 @@ test_that("tidy_add_variable_labels() works for basic models", {
)
)

# no error if providing labels not corresponding to an existing variable
# but display a message
expect_error(
mod |>
tidy_and_attach() |>
tidy_add_variable_labels(
labels = list(aaa = "aaa", bbb = "bbb", ccc = 44)
),
NA
)
expect_message(
mod |>
tidy_and_attach() |>
tidy_add_variable_labels(
labels = list(aaa = "aaa", bbb = "bbb", ccc = 44)
)
)
expect_error(
mod |>
tidy_and_attach() |>
tidy_add_variable_labels(
labels = list(aaa = "aaa", bbb = "bbb", ccc = 44),
strict = TRUE
)
)

# model with only an interaction term
mod <- lm(age ~ factor(response):marker, gtsummary::trial)
res <- mod |>
Expand Down

0 comments on commit 9e15526

Please sign in to comment.