Skip to content

Commit

Permalink
more machinery from recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Oct 16, 2024
1 parent cc34d8a commit 9586c09
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,86 @@ no_param <-
component_id = character(0)
)

find_tune_id <- function(x, call = caller_env()) {
if (length(x) == 0L) {
return(NA_character_)
}
if (rlang::is_quosures(x)) {
.x <- try(purrr::map(x, rlang::eval_tidy), silent = TRUE)
if (inherits(.x, "try-error")) {
x <- purrr::map(x, rlang::quo_get_expr)
} else {
x <- .x
}
}
id <- tune_id(x, call = call)

if (!is.na(id)) {
return(id)
}

if (is.atomic(x) | is.name(x) | length(x) == 1) {
return(NA_character_)
}

tunable_elems <- vector("character", length = length(x))
for (i in seq_along(x)) {
tunable_elems[i] <- find_tune_id(x[[i]], call = call)
}
tunable_elems <- tunable_elems[!is.na(tunable_elems)]

if (length(tunable_elems) == 0) {
tunable_elems <- NA_character_
}

if (sum(tunable_elems == "", na.rm = TRUE) > 1) {
offenders <- paste0(deparse(x), collapse = "")
cli::cli_abort(
c(
"Only one tunable value is currently allowed per argument.",
"The current argument has {.val {offenders}}."
),
call = call
)
}

return(tunable_elems)
}

tune_id <- function(x, call = caller_env()) {
if (is.null(x)) {
return(NA_character_)
} else {
if (rlang::is_quosures(x)) {
.x <- try(purrr::map(x, rlang::eval_tidy), silent = TRUE)
if (inherits(.x, "try-error")) {
x <- purrr::map(x, rlang::quo_get_expr)
} else {
x <- .x
}
if (is.null(x)) {
return(NA_character_)
}
}

if (is.call(x)) {
if (rlang::call_name(x) == "tune") {
if (length(x) > 1) {
return(x[[2]])
} else {
return("")
}

return(x$id)
} else {
return(NA_character_)
}
}
}

NA_character_
}

# new_adjustment -------------------------------------------------------------
# These values are used to specify "what will we need for the adjustment?" and
# "what will we change?". For the outputs, we cannot change the probabilities
Expand Down

0 comments on commit 9586c09

Please sign in to comment.