-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from topepo/output-steps
Output steps
- Loading branch information
Showing
18 changed files
with
851 additions
and
134 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,131 @@ | ||
#' Reorganize Measurements to Two Columns | ||
#' | ||
#' `step_measure_output_long` creates a *specification* of a recipe | ||
#' step that converts measures to a format with columns for the measurement and | ||
#' the corresponding location (i.e., "long" format). | ||
#' | ||
#' @family input/output steps | ||
#' @inheritParams recipes::step_center | ||
#' @param values_to A single character string for the column containing the | ||
#' analytical maesurement. | ||
#' @param location_to A single character string for the column containing the | ||
#' location of the measurement (e.g. wavenumber or index). | ||
#' @details | ||
#' This step is designed convert analytical measurements from their internal | ||
#' data structure to a two column format. | ||
#' @examples | ||
#' library(dplyr) | ||
#' | ||
#' data(glucose_bioreactors) | ||
#' bioreactors_small$batch_sample <- NULL | ||
#' | ||
#' small_tr <- bioreactors_small[ 1:200,] | ||
#' small_te <- bioreactors_small[201:210,] | ||
#' | ||
#' small_rec <- | ||
#' recipe(glucose ~ . , data = small_tr) %>% | ||
#' update_role(batch_id, day, new_role = "id columns") %>% | ||
#' step_measure_input_wide(`400`:`3050`) %>% | ||
#' prep() | ||
#' | ||
#' # Before reformatting: | ||
#' | ||
#' small_rec %>% bake(new_data = small_te) | ||
#' | ||
#' # After reformatting: | ||
#' | ||
#' output_rec <- | ||
#' small_rec %>% | ||
#' step_measure_output_long() %>% | ||
#' prep() | ||
#' | ||
#' output_rec %>% bake(new_data = small_te) | ||
#' | ||
#' @export | ||
|
||
step_measure_output_long <- | ||
function(recipe, | ||
values_to = ".measure", | ||
location_to = ".location", | ||
role = "predictor", | ||
trained = FALSE, | ||
skip = FALSE, | ||
id = rand_id("measure_output_long")) { | ||
add_step( | ||
recipe, | ||
step_measure_output_long_new( | ||
values_to = values_to, | ||
location_to = location_to, | ||
trained = trained, | ||
role = role, | ||
skip = skip, | ||
id = id | ||
) | ||
) | ||
} | ||
|
||
step_measure_output_long_new <- | ||
function(values_to, location_to, role, trained, skip, id) { | ||
step( | ||
subclass = "measure_output_long", | ||
values_to = values_to, | ||
location_to = location_to, | ||
role = role, | ||
trained = trained, | ||
skip = skip, | ||
id = id | ||
) | ||
} | ||
|
||
#' @export | ||
prep.step_measure_output_long <- function(x, training, info = NULL, ...) { | ||
check_has_measure(training, match.call()) | ||
step_measure_output_long_new( | ||
values_to = x$values_to, | ||
location_to = x$location_to, | ||
role = x$role, | ||
trained = TRUE, | ||
skip = x$skip, | ||
id = x$id | ||
) | ||
} | ||
|
||
#' @export | ||
bake.step_measure_output_long <- function(object, new_data, ...) { | ||
rnm <- c(".measures.value", ".measures.location") | ||
names(rnm) <- c(object$values_to, object$location_to) | ||
new_data %>% | ||
tidyr::unnest(cols = c(.measures), names_sep = ".") %>% | ||
dplyr::rename(!!rnm) | ||
} | ||
|
||
#' @export | ||
print.step_measure_output_long <- | ||
function(x, width = max(20, options()$width - 30), ...) { | ||
title <- "Restructure analytical measurements to long format" | ||
print_step(rlang::quos("<internal data>"), rlang::quos("<internal data>"), | ||
x$trained, title, width) | ||
invisible(x) | ||
} | ||
|
||
#' @rdname tidy.recipe | ||
#' @export | ||
tidy.step_measure_output_long <- function(x, ...) { | ||
if (is_trained(x)) { | ||
res <- tibble(terms = na_chr, | ||
value = na_dbl) | ||
} else { | ||
res <- tibble(terms = na_chr, | ||
value = na_dbl) | ||
} | ||
res$id <- x$id | ||
res | ||
} | ||
|
||
value_to_tibble <- function(x, prefix = "measure_") { | ||
x <- matrix(x$value, nrow = 1) | ||
colnames(x) <- recipes::names0(ncol(x), prefix = prefix) | ||
dplyr::as_tibble(x) | ||
} | ||
|
||
|
This file contains 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,127 @@ | ||
#' Reorganize Measurements to Separate Columns | ||
#' | ||
#' `step_measure_output_wide` creates a *specification* of a recipe | ||
#' step that converts measures to multiple columns (i.e., "wide" format). | ||
#' @family input/output steps | ||
#' @inheritParams recipes::step_center | ||
#' @param prefix A character string used to name the new columns. | ||
#' @details | ||
#' This step is designed convert analytical measurements from their internal | ||
#' data structure to separate columns. | ||
#' | ||
#' Wide outputs can be helpful when you want to use standard recipes steps with | ||
#' the measuresments, such as [recipes::step_pca()], [recipes::step_pls()], and | ||
#' so on. | ||
#' @examples | ||
#' library(dplyr) | ||
#' | ||
#' data(glucose_bioreactors) | ||
#' bioreactors_small$batch_sample <- NULL | ||
#' | ||
#' small_tr <- bioreactors_small[ 1:200,] | ||
#' small_te <- bioreactors_small[201:210,] | ||
#' | ||
#' small_rec <- | ||
#' recipe(glucose ~ . , data = small_tr) %>% | ||
#' update_role(batch_id, day, new_role = "id columns") %>% | ||
#' step_measure_input_wide(`400`:`3050`) %>% | ||
#' prep() | ||
#' | ||
#' # Before reformatting: | ||
#' | ||
#' small_rec %>% bake(new_data = small_te) | ||
#' | ||
#' # After reformatting: | ||
#' | ||
#' output_rec <- | ||
#' small_rec %>% | ||
#' step_measure_output_wide() %>% | ||
#' prep() | ||
#' | ||
#' output_rec %>% bake(new_data = small_te) | ||
#' | ||
#' @export | ||
|
||
step_measure_output_wide <- | ||
function(recipe, | ||
prefix = "measure_", | ||
role = "predictor", | ||
trained = FALSE, | ||
skip = FALSE, | ||
id = rand_id("measure_output_wide")) { | ||
add_step( | ||
recipe, | ||
step_measure_output_wide_new( | ||
prefix = prefix, | ||
trained = trained, | ||
role = role, | ||
skip = skip, | ||
id = id | ||
) | ||
) | ||
} | ||
|
||
step_measure_output_wide_new <- | ||
function(prefix, role, trained, skip, id) { | ||
step( | ||
subclass = "measure_output_wide", | ||
prefix = prefix, | ||
role = role, | ||
trained = trained, | ||
skip = skip, | ||
id = id | ||
) | ||
} | ||
|
||
#' @export | ||
prep.step_measure_output_wide <- function(x, training, info = NULL, ...) { | ||
check_has_measure(training, match.call()) | ||
step_measure_output_wide_new( | ||
prefix = x$prefix, | ||
role = x$role, | ||
trained = TRUE, | ||
skip = x$skip, | ||
id = x$id | ||
) | ||
} | ||
|
||
#' @export | ||
bake.step_measure_output_wide <- function(object, new_data, ...) { | ||
|
||
non_meas <- names(new_data) | ||
non_meas <- non_meas[non_meas != ".measures"] | ||
|
||
res <- | ||
new_data %>% | ||
tidyr::unnest(cols = c(.measures)) %>% | ||
dplyr::mutate(location = gsub(" ", "0", format(location))) %>% | ||
tidyr::pivot_wider( | ||
id_cols = c(dplyr::all_of(non_meas)), | ||
names_from = "location", | ||
values_from = "value" | ||
) %>% | ||
dplyr::rename_with(~ paste0(object$prefix, .x), c(-dplyr::all_of(non_meas))) | ||
} | ||
|
||
#' @export | ||
print.step_measure_output_wide <- | ||
function(x, width = max(20, options()$width - 30), ...) { | ||
title <- "Restructure analytical measurements to wide format" | ||
print_step(rlang::quos("<internal data>"), rlang::quos("<internal data>"), x$trained, title, width) | ||
invisible(x) | ||
} | ||
|
||
#' @rdname tidy.recipe | ||
#' @export | ||
tidy.step_measure_output_wide <- function(x, ...) { | ||
if (is_trained(x)) { | ||
res <- tibble(terms = na_chr, | ||
value = na_dbl) | ||
} else { | ||
res <- tibble(terms = na_chr, | ||
value = na_dbl) | ||
} | ||
res$id <- x$id | ||
res | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.