Skip to content

Commit

Permalink
Merge pull request #1323 from tidymodels/outcome-predictor-helpers
Browse files Browse the repository at this point in the history
add recipes_name_predictors() and recipes_outcomes()
  • Loading branch information
EmilHvitfeldt authored May 30, 2024
2 parents c63b7a8 + c96a690 commit 3dba8f2
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ export(rand_id)
export(recipe)
export(recipes_eval_select)
export(recipes_extension_check)
export(recipes_names_outcomes)
export(recipes_names_predictors)
export(recipes_pkg_check)
export(recipes_remove_cols)
export(remove_original_cols)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

* Improved error message for misspelled argument in step functions. (#1318)

* Developer helper functions `recipes_names_predictors()` and `recipes_names_outcomes()` have been added to aid variable selection in steps. (#1026)

# recipes 1.0.10

## Bug Fixes
Expand Down
4 changes: 4 additions & 0 deletions R/developer.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
#' [recipes_remove_cols()] should be used in `prep.step_*()` functions, and is
#' used to remove columns from the data set, either by using the
#' `object$removals` field or by using the `col_names` argument.
#'
#' [recipes_names_predictors()] and [recipes_names_outcomes()] should be used in
#' `prep.step_*()` functions, and are used to get names of predictors and
#' outcomes.
#'
#' [get_case_weights()] and [are_weights_used()] are functions that help you
#' extract case weights and help determine if they are used or not within the
Expand Down
33 changes: 33 additions & 0 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -955,3 +955,36 @@ recipes_remove_cols <- function(new_data, object, col_names = character()) {
}
new_data
}

#' Role indicators
#'
#' This helper function is meant to be used in `prep()` methods to identify
#' predictors and outcomes by names.
#'
#' @param info data.frame with variable information of columns.
#'
#' @return Character vector of column names.
#' @keywords internal
#'
#' @seealso [developer_functions]
#'
#' @name recipes-role-indicator
NULL

#' @rdname recipes-role-indicator
#' @export
recipes_names_predictors <- function(info) {
get_from_info(info, "predictor")
}

#' @rdname recipes-role-indicator
#' @export
recipes_names_outcomes <- function(info) {
get_from_info(info, "outcome")
}

get_from_info <- function(info, role) {
res <- info$variable[info$role == role & !is.na(info$role)]

res
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ reference:
- prepper
- recipes_eval_select
- recipes_extension_check
- recipes-role-indicator
- update.step
- title: Tidy Methods
contents:
Expand Down
4 changes: 4 additions & 0 deletions man/developer_functions.Rd

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

26 changes: 26 additions & 0 deletions man/recipes-role-indicator.Rd

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

13 changes: 6 additions & 7 deletions man/roles.Rd

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

0 comments on commit 3dba8f2

Please sign in to comment.