-
Notifications
You must be signed in to change notification settings - Fork 42
Add outcome_names() method for workflows using add_variables() #994
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,9 @@ | ||||||
#' Determine names of the outcome data in a workflow | ||||||
#' | ||||||
#' @param x An object. | ||||||
#' @param ... Not used. | ||||||
#' @param ... Further arguments passed to or from other methods (such as `data`). | ||||||
#' @param data The training set data (if needed). | ||||||
#' @param call The call to be displayed in warnings or errors. | ||||||
#' @return A character string of variable names | ||||||
#' @keywords internal | ||||||
#' @examples | ||||||
|
@@ -39,20 +41,39 @@ outcome_names.recipe <- function(x, ...) { | |||||
|
||||||
#' @export | ||||||
#' @rdname outcome_names | ||||||
outcome_names.workflow <- function(x, ...) { | ||||||
if (!is.null(x$fit$fit)) { | ||||||
outcome_names.workflow <- function(x, ..., call = caller_env()) { | ||||||
if (!is.null(x$pre$mold)) { | ||||||
y_vals <- extract_mold(x)$outcomes | ||||||
res <- colnames(y_vals) | ||||||
} else { | ||||||
preprocessor <- extract_preprocessor(x) | ||||||
res <- outcome_names(preprocessor) | ||||||
res <- outcome_names(preprocessor, ..., call = call) | ||||||
} | ||||||
res | ||||||
} | ||||||
|
||||||
#' @export | ||||||
#' @rdname outcome_names | ||||||
outcome_names.tune_results <- function(x, ...) { | ||||||
outcome_names.workflow_variables <- function( | ||||||
x, | ||||||
data = NULL, | ||||||
..., | ||||||
call = caller_env() | ||||||
) { | ||||||
if (is.null(data)) { | ||||||
cli::cli_abort( | ||||||
"To determine the outcome names when {.fn add_variables} is used, please | ||||||
pass the training set data to the {.arg data} argument.", | ||||||
call = call | ||||||
) | ||||||
} | ||||||
res <- rlang::eval_tidy(x$outcomes, data, env = call) | ||||||
res | ||||||
} | ||||||
|
||||||
#' @export | ||||||
#' @rdname outcome_names | ||||||
outcome_names.tune_results <- function(x, ..., call = caller_env()) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this change in function signature for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're welcome to suggest a change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I don't think the addition is necessary (and tests pass without it) so I'd just leave things as is. |
||||||
att <- attributes(x) | ||||||
if (any(names(att) == "outcomes")) { | ||||||
res <- att$outcomes | ||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# workflows + variables | ||
|
||
Code | ||
outcome_names(wflow_1) | ||
Condition | ||
Error: | ||
! To determine the outcome names when `add_variables()` is used, please pass the training set data to the `data` argument. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an error message for the user or for us?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, digging around a bit more here and I think there is a little messaging mismatch to resolve in that error message:
Do you mean that the user of
outcome_names()
should use thedata
argument ofoutcome_names()
? The user ofoutcome_names()
is presumably just us sinceoutcome_names()
is labeled with@keywords internal
. In that case, I wouldn't pass in the call tocli_abort()
and possibly remove the reference toadd_variables()
. The context of how we got into this probably lives better elsewhere, likely wherever we call it from and need to adddata
, so intune_grid()
orint_pctl()
.If you mean the general tidymodels user the current error message is probably confusing: they only call
tune_grid()
orint_pctl()
and couldn't use thatdata
argument tooutcome_names()
.tune_grid()
does not have adata
argument andint_pctl()
has.data
but that is not the same as thedata
foroutcome_name()
. (And imagining a very desperate user, looking into the mentionedadd_variables()
also does not reveal adata
argument.)I'm guessing path 1 is the right one here?