Skip to content

Commit

Permalink
orbital_mutate -> orbital_predict()
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Jun 21, 2024
1 parent 8093e41 commit 40bab52
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 37 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
35 changes: 14 additions & 21 deletions R/mutate.R
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 34 additions & 0 deletions R/predict.R
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 26 additions & 9 deletions man/orbital_mutate.Rd → man/orbital_predict.Rd

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
Expand Up @@ -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)
)
})

0 comments on commit 40bab52

Please sign in to comment.