diff --git a/NAMESPACE b/NAMESPACE index b9a37d9..7894166 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,6 +15,6 @@ export(orbital_dt) export(orbital_inline) export(orbital_json_read) export(orbital_json_write) -export(orbital_mutate) +export(orbital_predict) export(orbital_sql) export(to_r_fun) diff --git a/R/mutate.R b/R/mutate.R index f3f2ea9..76c1f02 100644 --- a/R/mutate.R +++ b/R/mutate.R @@ -1,41 +1,34 @@ #' Use orbital in a mutate way -#' +#' #' @param .data A data frame that can be used with mutate. #' @param x A orbital object. -#' @param only_pred A logical value to determine if whole data set should be -#' returned or just predictions. -#' +#' #' @returns A modified data frame. -#' +#' #' @examples #' library(workflows) #' library(recipes) #' library(parsnip) -#' +#' #' rec_spec <- recipe(mpg ~ ., data = mtcars) %>% #' step_normalize(all_numeric_predictors()) -#' +#' #' lm_spec <- linear_reg() -#' +#' #' wf_spec <- workflow(rec_spec, lm_spec) -#' +#' #' wf_fit <- fit(wf_spec, mtcars) -#' +#' #' orbital_obj <- orbital(wf_fit) -#' +#' #' mtcars %>% -#' orbital_mutate(orbital_obj) -#' -#' mtcars %>% -#' orbital_mutate(orbital_obj, only_pred = TRUE) +#' orbital_predict(orbital_obj) #' @export -orbital_mutate <- function(.data, x, only_pred = FALSE) { - res <- dplyr::mutate(.data, !!!orbital_inline(x)) +orbital_predict <- function(.data, x) { + pred_name <- names(x)[length(x)] - if (only_pred) { - pred_name <- names(x)[length(x)] - res <- dplyr::select(res, dplyr::any_of(pred_name)) - } + res <- dplyr::mutate(.data, !!!orbital_inline(x)) + res <- dplyr::select(res, dplyr::any_of(pred_name)) res } diff --git a/R/predict.R b/R/predict.R new file mode 100644 index 0000000..07e449f --- /dev/null +++ b/R/predict.R @@ -0,0 +1,34 @@ +#' Use orbital in a mutate way +#' +#' @param .data A data frame that can be used with mutate. +#' @param x A orbital object. +#' +#' @returns A modified data frame. +#' +#' @examples +#' library(workflows) +#' library(recipes) +#' library(parsnip) +#' +#' rec_spec <- recipe(mpg ~ ., data = mtcars) %>% +#' step_normalize(all_numeric_predictors()) +#' +#' lm_spec <- linear_reg() +#' +#' wf_spec <- workflow(rec_spec, lm_spec) +#' +#' wf_fit <- fit(wf_spec, mtcars) +#' +#' orbital_obj <- orbital(wf_fit) +#' +#' mtcars %>% +#' orbital_predict(orbital_obj) +#' @export +orbital_predict <- function(.data, x) { + res <- dplyr::mutate(.data, !!!orbital_inline(x)) + + pred_name <- names(x)[length(x)] + res <- dplyr::select(res, dplyr::any_of(pred_name)) + + res +} diff --git a/man/orbital_mutate.Rd b/man/orbital_predict.Rd similarity index 51% rename from man/orbital_mutate.Rd rename to man/orbital_predict.Rd index c33fe0c..195112a 100644 --- a/man/orbital_mutate.Rd +++ b/man/orbital_predict.Rd @@ -1,23 +1,26 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/mutate.R -\name{orbital_mutate} -\alias{orbital_mutate} +% Please edit documentation in R/mutate.R, R/predict.R +\name{orbital_predict} +\alias{orbital_predict} \title{Use orbital in a mutate way} \usage{ -orbital_mutate(.data, x, only_pred = FALSE) +orbital_predict(.data, x) + +orbital_predict(.data, x) } \arguments{ \item{.data}{A data frame that can be used with mutate.} \item{x}{A orbital object.} - -\item{only_pred}{A logical value to determine if whole data set should be -returned or just predictions.} } \value{ +A modified data frame. + A modified data frame. } \description{ +Use orbital in a mutate way + Use orbital in a mutate way } \examples{ @@ -37,8 +40,22 @@ wf_fit <- fit(wf_spec, mtcars) orbital_obj <- orbital(wf_fit) mtcars \%>\% - orbital_mutate(orbital_obj) + orbital_predict(orbital_obj) +library(workflows) +library(recipes) +library(parsnip) + +rec_spec <- recipe(mpg ~ ., data = mtcars) \%>\% + step_normalize(all_numeric_predictors()) + +lm_spec <- linear_reg() + +wf_spec <- workflow(rec_spec, lm_spec) + +wf_fit <- fit(wf_spec, mtcars) + +orbital_obj <- orbital(wf_fit) mtcars \%>\% - orbital_mutate(orbital_obj, only_pred = TRUE) + orbital_predict(orbital_obj) } diff --git a/tests/testthat/test-mutate.R b/tests/testthat/test-predict.R similarity index 74% rename from tests/testthat/test-mutate.R rename to tests/testthat/test-predict.R index a18600f..f89180d 100644 --- a/tests/testthat/test-mutate.R +++ b/tests/testthat/test-predict.R @@ -13,12 +13,7 @@ test_that("orbital works with workflows - recipe", { obj <- orbital(wf_fit) expect_identical( - mtcars %>% orbital_mutate(obj), - mtcars %>% dplyr::mutate(!!!orbital_inline(obj)) - ) - - expect_identical( - mtcars %>% orbital_mutate(obj, only_pred = TRUE), + mtcars %>% orbital_predict(obj), mtcars %>% dplyr::mutate(!!!orbital_inline(obj)) %>% dplyr::select(.pred) ) })