-
Notifications
You must be signed in to change notification settings - Fork 21
Export recompose()
#231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Export recompose()
#231
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,88 @@ | ||
| # This is the same as the "recomposition" at the end of recipes::bake() | ||
|
|
||
| recompose <- function(data, composition) { | ||
| if (identical(composition, "tibble")) { | ||
| data | ||
| } else if (identical(composition, "dgCMatrix")) { | ||
| convert_matrix(data, sparse = TRUE) | ||
| } else if (identical(composition, "matrix")) { | ||
| convert_matrix(data, sparse = FALSE) | ||
| } else { | ||
| abort("Internal error: Unknown `composition` type.") | ||
| } | ||
| } | ||
| #' Recompose a data frame into another form | ||
| #' | ||
| #' @description | ||
| #' `recompose()` takes a data frame and converts it into one of: | ||
| #' - A tibble | ||
| #' - A data frame | ||
| #' - A matrix | ||
| #' - A sparse matrix (using the Matrix package) | ||
| #' | ||
| #' This is an internal function used only by hardhat and recipes. | ||
| #' | ||
| #' @inheritParams rlang::args_dots_empty | ||
| #' | ||
| #' @param data A data frame. | ||
| #' | ||
| #' @param composition One of: | ||
| #' - `"tibble"` to convert to a tibble. | ||
| #' - `"data.frame"` to convert to a base data frame. | ||
| #' - `"matrix"` to convert to a matrix. All columns must be numeric. | ||
| #' - `"dgCMatrix"` to convert to a sparse matrix. All columns must be numeric, | ||
| #' and the Matrix package must be installed. | ||
| #' | ||
| #' @returns | ||
| #' The output type is determined from the `composition`. | ||
| #' | ||
| #' @export | ||
| #' @keywords internal | ||
|
Member
Author
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. Internal as I don't really want people using this |
||
| #' | ||
| #' @examples | ||
| #' df <- vctrs::data_frame(x = 1) | ||
| #' | ||
| #' recompose(df) | ||
| #' recompose(df, composition = "matrix") | ||
| #' | ||
| #' # All columns must be numeric to convert to a matrix | ||
| #' df <- vctrs::data_frame(x = 1, y = "a") | ||
| #' try(recompose(df, composition = "matrix")) | ||
| recompose <- function(data, | ||
| ..., | ||
| composition = "tibble") { | ||
| check_dots_empty0(...) | ||
| check_data_frame(data) | ||
|
|
||
| composition <- arg_match0( | ||
| arg = composition, | ||
| values = c("tibble", "data.frame", "matrix", "dgCMatrix") | ||
| ) | ||
|
|
||
| convert_matrix <- function(x, sparse = TRUE) { | ||
| is_num <- vapply(x, is.numeric, logical(1)) | ||
|
|
||
| if (!all(is_num)) { | ||
| num_viol <- sum(!is_num) | ||
| if (num_viol < 5) { | ||
| abort( | ||
| paste0( | ||
| "Columns (", | ||
| paste0("`", names(is_num)[!is_num], "`", collapse = ", "), | ||
| ") are not numeric; cannot convert to matrix." | ||
| ) | ||
| ) | ||
| } else { | ||
| abort( | ||
| paste0( | ||
| num_viol, | ||
| " columns are not numeric; cannot ", | ||
| "convert to matrix." | ||
| ) | ||
| ) | ||
| switch( | ||
| composition, | ||
| tibble = { | ||
| coerce_to_tibble(data) | ||
| }, | ||
| data.frame = { | ||
| new_data_frame(data, n = vec_size(data)) | ||
| }, | ||
| matrix = { | ||
| coerce_to_matrix(data) | ||
| }, | ||
| dgCMatrix = { | ||
| data <- coerce_to_matrix(data) | ||
| coerce_to_sparse(data) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| # At this point, all cols are numeric so we can just use as.matrix() | ||
| res <- as.matrix(x) | ||
| coerce_to_matrix <- function(data, error_call = caller_env()) { | ||
| numeric <- map_lgl(data, is.numeric) | ||
|
|
||
| if (sparse) { | ||
| if (!is_installed("Matrix")) { | ||
| abort("The Matrix package must be installed to use a 'dgCMatrix' `composition`") | ||
| } | ||
| res <- Matrix::Matrix(res, sparse = TRUE) | ||
| if (!all(numeric)) { | ||
| loc <- which(!numeric) | ||
| loc <- names(data)[loc] | ||
|
|
||
| message <- c( | ||
| "{.arg data} must only contain numeric columns.", | ||
| i = "These columns aren't numeric: {.str {loc}}." | ||
| ) | ||
|
|
||
| cli::cli_abort(message, call = error_call) | ||
| } | ||
|
|
||
| res | ||
| as.matrix(data) | ||
| } | ||
|
|
||
| coerce_to_sparse <- function(data, error_call = caller_env()) { | ||
| check_installed("Matrix", call = error_call) | ||
| Matrix::Matrix(data, sparse = TRUE) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # can be both missing levels and have new levels | ||
|
|
||
| Code | ||
| mold(y ~ f, dat, blueprint = bp2) | ||
| Condition | ||
| Error in `recompose()`: | ||
| ! `data` must only contain numeric columns. | ||
| i These columns aren't numeric: "f". | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # columns must be numeric when coercing to matrix | ||
|
|
||
| Code | ||
| recompose(df, composition = "matrix") | ||
| Condition | ||
| Error in `recompose()`: | ||
| ! `data` must only contain numeric columns. | ||
| i These columns aren't numeric: "y" and "z". | ||
|
|
||
| # columns must be numeric when coercing to sparse matrix | ||
|
|
||
| Code | ||
| recompose(df, composition = "dgCMatrix") | ||
| Condition | ||
| Error in `recompose()`: | ||
| ! `data` must only contain numeric columns. | ||
| i These columns aren't numeric: "y" and "z". | ||
|
|
||
| # checks for data frame input | ||
|
|
||
| Code | ||
| recompose(1) | ||
| Condition | ||
| Error in `recompose()`: | ||
| ! `data` must be a data frame, not the number 1. | ||
|
|
||
| # dots must be empty | ||
|
|
||
| Code | ||
| recompose(data.frame(), 1) | ||
| Condition | ||
| Error in `recompose()`: | ||
| ! `...` must be empty. | ||
| x Problematic argument: | ||
| * ..1 = 1 | ||
| i Did you forget to name an argument? | ||
|
|
||
| # validates `composition` | ||
|
|
||
| Code | ||
| recompose(data.frame(), composition = "foo") | ||
| Condition | ||
| Error in `recompose()`: | ||
| ! `composition` must be one of "tibble", "data.frame", "matrix", or "dgCMatrix", not "foo". | ||
|
|
||
| --- | ||
|
|
||
| Code | ||
| recompose(data.frame(), composition = 1) | ||
| Condition | ||
| Error in `recompose()`: | ||
| ! `composition` must be a string or character vector. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I added support for
"data.frame"specifically for recipes, but hardhat won't use it