Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ S3method(orbital,workflow)
S3method(orbital,xgb.Booster)
S3method(predict,orbital_class)
S3method(print,orbital_class)
S3method(show_query,orbital_class)
export(augment)
export(orbital)
export(orbital_dt)
Expand All @@ -71,4 +72,5 @@ export(orbital_json_read)
export(orbital_json_write)
export(orbital_r_fun)
export(orbital_sql)
importFrom(dplyr,show_query)
importFrom(generics,augment)
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- `adjust_predictions_custom()`
- `adjust_probability_threshold()`

* Added `show_query()` method for orbital objects. (#106)

# orbital 0.3.1

* Fixed bug where PCA steps didn't work if they were trained with more than 99 predictors. (#82)
Expand Down
16 changes: 16 additions & 0 deletions R/show_query.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#' @importFrom dplyr show_query
#' @export
show_query.orbital_class <- function(x, con, ...) {
x <- unclass(orbital_sql(x, con))

if (length(x) == 0) {
x <- paste0("[empty]")
} else {
x <- paste0(
x,
ifelse(rlang::names2(x) == "", "", paste0(" AS ", rlang::names2(x)))
)
}

cat(x, sep = "\n")
}
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/show_query.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# show_query works

Code
show_query(orbital_obj, con = con)
Output
CASE WHEN ((`disp` IS NULL)) THEN 230.721875 WHEN NOT ((`disp` IS NULL)) THEN `disp` END AS disp
(`disp` - 230.721875) / 123.938693831382 AS disp
20.090625 + (`disp` * -5.10814813429143) AS .pred

2 changes: 1 addition & 1 deletion tests/testthat/test-adjust_equivocal_zone.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ test_that("arrow - adjust_equivocal_zone works", {
mtcars_tbl <- arrow::as_arrow_table(mtcars_eq)

expect_identical(
dplyr::pull(predict(orb_fit, mtcars_tbl), .pred_class, as_vector = TRUE),
dplyr::collect(predict(orb_fit, mtcars_tbl))$.pred_class,
exp
)
})
27 changes: 27 additions & 0 deletions tests/testthat/test-show_query.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
test_that("show_query works", {
skip_if_not_installed("recipes")
skip_if_not_installed("parsnip")
skip_if_not_installed("workflows")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")
skip_on_cran()

rec_spec <- recipes::recipe(mpg ~ disp, data = mtcars) |>
recipes::step_impute_mean(recipes::all_numeric_predictors()) |>
recipes::step_normalize(recipes::all_numeric_predictors())

lm_spec <- parsnip::linear_reg()

wf_spec <- workflows::workflow(rec_spec, lm_spec)
wf_fit <- parsnip::fit(wf_spec, data = mtcars)

orbital_obj <- orbital(wf_fit)

con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")

expect_snapshot(
show_query(orbital_obj, con = con)
)

DBI::dbDisconnect(con)
})
44 changes: 30 additions & 14 deletions tests/testthat/test-step_impute_mode.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ test_that("step_impute_mode works", {
mtcars$carb <- letters[mtcars$carb]
mtcars[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars) |>
rec <- recipes::recipe(mpg ~ ., data = mtcars, strings_as_factors = FALSE) |>
recipes::step_impute_mode(recipes::all_nominal_predictors()) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec)))

Expand All @@ -26,10 +26,10 @@ test_that("step_impute_mode only calculates what is sufficient", {
mtcars$carb <- letters[mtcars$carb]
mtcars[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars) |>
rec <- recipes::recipe(mpg ~ ., data = mtcars, strings_as_factors = FALSE) |>
recipes::step_impute_mode(recipes::all_nominal_predictors()) |>
recipes::step_rm(gear) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

expect_identical(
names(orbital(rec)),
Expand All @@ -45,9 +45,9 @@ test_that("step_impute_mode works with empty selections", {
mtcars$carb <- letters[mtcars$carb]
mtcars[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars) |>
rec <- recipes::recipe(mpg ~ ., data = mtcars, strings_as_factors = FALSE) |>
recipes::step_impute_mode() |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec)))

Expand All @@ -67,9 +67,13 @@ test_that("spark - step_impute_mode works", {
mtcars_impute_mode$carb <- letters[mtcars$carb]
mtcars_impute_mode[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars_impute_mode) |>
rec <- recipes::recipe(
mpg ~ .,
data = mtcars_impute_mode,
strings_as_factors = FALSE
) |>
recipes::step_impute_mode(recipes::all_nominal_predictors()) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars_impute_mode, !!!orbital_inline(orbital(rec)))

Expand All @@ -93,9 +97,13 @@ test_that("SQLite - step_impute_mode works", {
mtcars_impute_mode$carb <- letters[mtcars$carb]
mtcars_impute_mode[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars_impute_mode) |>
rec <- recipes::recipe(
mpg ~ .,
data = mtcars_impute_mode,
strings_as_factors = FALSE
) |>
recipes::step_impute_mode(recipes::all_nominal_predictors()) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars_impute_mode, !!!orbital_inline(orbital(rec)))

Expand All @@ -120,9 +128,13 @@ test_that("duckdb - step_impute_mode works", {
mtcars_impute_mode$carb <- letters[mtcars$carb]
mtcars_impute_mode[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars_impute_mode) |>
rec <- recipes::recipe(
mpg ~ .,
data = mtcars_impute_mode,
strings_as_factors = FALSE
) |>
recipes::step_impute_mode(recipes::all_nominal_predictors()) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars_impute_mode, !!!orbital_inline(orbital(rec)))

Expand All @@ -146,9 +158,13 @@ test_that("arrow - step_impute_mode works", {
mtcars_impute_mode$carb <- letters[mtcars$carb]
mtcars_impute_mode[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars_impute_mode) |>
rec <- recipes::recipe(
mpg ~ .,
data = mtcars_impute_mode,
strings_as_factors = FALSE
) |>
recipes::step_impute_mode(recipes::all_nominal_predictors()) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars_impute_mode, !!!orbital_inline(orbital(rec)))

Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-step_novel.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ test_that("step_novel works", {
mtcars$gear <- letters[mtcars$gear]
mtcars$carb <- letters[mtcars$carb]

rec <- recipes::recipe(mpg ~ ., data = mtcars) |>
rec <- recipes::recipe(mpg ~ ., data = mtcars, strings_as_factors = FALSE) |>
recipes::step_novel(recipes::all_nominal_predictors()) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

mtcars[1, 10] <- "aaaaa"

Expand All @@ -30,10 +30,10 @@ test_that("step_novel only calculates what is sufficient", {
mtcars$gear <- letters[mtcars$gear]
mtcars$carb <- letters[mtcars$carb]

rec <- recipes::recipe(mpg ~ ., data = mtcars) |>
rec <- recipes::recipe(mpg ~ ., data = mtcars, strings_as_factors = FALSE) |>
recipes::step_novel(recipes::all_nominal_predictors()) |>
recipes::step_rm(gear) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

expect_identical(
names(orbital(rec)),
Expand All @@ -49,9 +49,9 @@ test_that("step_novel works with empty selections", {
mtcars$gear <- letters[mtcars$gear]
mtcars$carb <- letters[mtcars$carb]

rec <- recipes::recipe(mpg ~ ., data = mtcars) |>
rec <- recipes::recipe(mpg ~ ., data = mtcars, strings_as_factors = FALSE) |>
recipes::step_novel() |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

mtcars[1, 10] <- "aaaaa"

Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-step_other.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ test_that("step_other works", {
mtcars$gear[1] <- "aa"
mtcars$carb[1] <- "aa"

rec <- recipes::recipe(mpg ~ ., data = mtcars) |>
rec <- recipes::recipe(mpg ~ ., data = mtcars, strings_as_factors = FALSE) |>
recipes::step_other(recipes::all_nominal_predictors()) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec)))

Expand All @@ -32,10 +32,10 @@ test_that("step_other only calculates what is sufficient", {
mtcars$gear[1] <- "aa"
mtcars$carb[1] <- "aa"

rec <- recipes::recipe(mpg ~ ., data = mtcars) |>
rec <- recipes::recipe(mpg ~ ., data = mtcars, strings_as_factors = FALSE) |>
recipes::step_other(recipes::all_nominal_predictors()) |>
recipes::step_rm(gear) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

expect_identical(
names(orbital(rec)),
Expand All @@ -53,9 +53,9 @@ test_that("step_other works with empty selections", {
mtcars$gear[1] <- "aa"
mtcars$carb[1] <- "aa"

rec <- recipes::recipe(mpg ~ ., data = mtcars) |>
rec <- recipes::recipe(mpg ~ ., data = mtcars, strings_as_factors = FALSE) |>
recipes::step_other() |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec)))

Expand Down
20 changes: 2 additions & 18 deletions tests/testthat/test-step_select.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
test_that("step_select works", {
skip_if_not_installed("recipes")

mtcars0 <- mtcars
mtcars0$zv <- 0

rec_exp <- recipes::recipe(mpg ~ ., data = mtcars) |>
recipes::step_select(recipes::all_predictors()) |>
recipes::prep()

expect_null(orbital(rec_exp$steps[[1]]))

rec <- recipes::recipe(mpg ~ ., data = mtcars0) |>
recipes::step_select(recipes::all_predictors()) |>
recipes::prep()

expect_null(orbital(rec$steps[[1]]))

expect_identical(orbital(rec), orbital(rec_exp))
# The step is predecated
expect_true(TRUE)
})

test_that("step_select only calculates what is sufficient", {
Expand Down
32 changes: 24 additions & 8 deletions tests/testthat/test-step_unknown.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ test_that("spark - step_unknown works", {
mtcars_unknown$carb <- letters[mtcars$carb]
mtcars_unknown[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars_unknown) |>
rec <- recipes::recipe(
mpg ~ .,
data = mtcars_unknown,
strings_as_factors = FALSE
) |>
recipes::step_unknown(recipes::all_nominal_predictors()) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars_unknown, !!!orbital_inline(orbital(rec)))

Expand All @@ -97,9 +101,13 @@ test_that("SQLite - step_unknown works", {
mtcars_unknown$carb <- letters[mtcars$carb]
mtcars_unknown[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars_unknown) |>
rec <- recipes::recipe(
mpg ~ .,
data = mtcars_unknown,
strings_as_factors = FALSE
) |>
recipes::step_unknown(recipes::all_nominal_predictors()) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars_unknown, !!!orbital_inline(orbital(rec)))

Expand All @@ -124,9 +132,13 @@ test_that("duckdb - step_unknown works", {
mtcars_unknown$carb <- letters[mtcars$carb]
mtcars_unknown[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars_unknown) |>
rec <- recipes::recipe(
mpg ~ .,
data = mtcars_unknown,
strings_as_factors = FALSE
) |>
recipes::step_unknown(recipes::all_nominal_predictors()) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars_unknown, !!!orbital_inline(orbital(rec)))

Expand All @@ -150,9 +162,13 @@ test_that("arrow - step_unknown works", {
mtcars_unknown$carb <- letters[mtcars$carb]
mtcars_unknown[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars_unknown) |>
rec <- recipes::recipe(
mpg ~ .,
data = mtcars_unknown,
strings_as_factors = FALSE
) |>
recipes::step_unknown(recipes::all_nominal_predictors()) |>
recipes::prep(strings_as_factors = FALSE)
recipes::prep()

res <- dplyr::mutate(mtcars_unknown, !!!orbital_inline(orbital(rec)))

Expand Down
Loading