Skip to content

Commit

Permalink
tryCatch() re-work
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg committed Mar 8, 2024
1 parent f08eca3 commit d48fb3c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
25 changes: 10 additions & 15 deletions R/ard_vif.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,20 @@ ard_vif <- function(x, ...) {
# check inputs ---------------------------------------------------------------
check_not_missing(x)

vif <- car::vif(x, ...) |>
cards::eval_capture_conditions()
vif <- cards::eval_capture_conditions(car::vif(x, ...))

# if vif failed, set result as NULL, error will be kept through eval_capture_conditions()
if (is.null(vif$result)) {
# try to capture variable names from `terms()`
lst_terms <- cards::eval_capture_conditions(attr(stats::terms(x), "term.labels"))
# we cannot get variable names, error out
if (!is.null(lst_terms[["error"]])) {
cli::cli_abort(
c("There was an error running {.fun car::vif}. See below.", x = vif[["error"]])
)
}
vif$result <- dplyr::tibble(
variable =
tryCatch(
varnames <- attr(stats::terms(x), "term.labels"),
error = function(e) {
cli::cli_abort(
c(
"There was an error running {.fun car::vif}. See below.",
x = vif[["error"]]
),
call = env_parent(n = 3L)
)
}
),
variable = lst_terms[["result"]],
VIF = list(NULL),
GVIF = list(NULL),
aGVIF = list(NULL),
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/_snaps/ard_vif.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
Code
ard_vif(cards::ADSL)
Condition
Warning:
Unknown or uninitialised column: `terms`.
Error in `ard_vif()`:
! There was an error running `car::vif()`. See below.
x no applicable method for 'vcov' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"
Expand Down

0 comments on commit d48fb3c

Please sign in to comment.