Skip to content

Commit 6ef4eb0

Browse files
add show_query() method (#106)
1 parent fdc03b3 commit 6ef4eb0

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ S3method(orbital,workflow)
6363
S3method(orbital,xgb.Booster)
6464
S3method(predict,orbital_class)
6565
S3method(print,orbital_class)
66+
S3method(show_query,orbital_class)
6667
export(augment)
6768
export(orbital)
6869
export(orbital_dt)
@@ -71,4 +72,5 @@ export(orbital_json_read)
7172
export(orbital_json_write)
7273
export(orbital_r_fun)
7374
export(orbital_sql)
75+
importFrom(dplyr,show_query)
7476
importFrom(generics,augment)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- `adjust_predictions_custom()`
77
- `adjust_probability_threshold()`
88

9+
* Added `show_query()` method for orbital objects. (#106)
10+
911
# orbital 0.3.1
1012

1113
* Fixed bug where PCA steps didn't work if they were trained with more than 99 predictors. (#82)

R/show_query.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#' @importFrom dplyr show_query
2+
#' @export
3+
show_query.orbital_class <- function(x, con, ...) {
4+
x <- unclass(orbital_sql(x, con))
5+
6+
if (length(x) == 0) {
7+
x <- paste0("[empty]")
8+
} else {
9+
x <- paste0(
10+
x,
11+
ifelse(rlang::names2(x) == "", "", paste0(" AS ", rlang::names2(x)))
12+
)
13+
}
14+
15+
cat(x, sep = "\n")
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# show_query works
2+
3+
Code
4+
show_query(orbital_obj, con = con)
5+
Output
6+
CASE WHEN ((`disp` IS NULL)) THEN 230.721875 WHEN NOT ((`disp` IS NULL)) THEN `disp` END AS disp
7+
(`disp` - 230.721875) / 123.938693831382 AS disp
8+
20.090625 + (`disp` * -5.10814813429143) AS .pred
9+

tests/testthat/test-show_query.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
test_that("show_query works", {
2+
skip_if_not_installed("recipes")
3+
skip_if_not_installed("parsnip")
4+
skip_if_not_installed("workflows")
5+
skip_if_not_installed("DBI")
6+
skip_if_not_installed("RSQLite")
7+
skip_on_cran()
8+
9+
rec_spec <- recipes::recipe(mpg ~ disp, data = mtcars) |>
10+
recipes::step_impute_mean(recipes::all_numeric_predictors()) |>
11+
recipes::step_normalize(recipes::all_numeric_predictors())
12+
13+
lm_spec <- parsnip::linear_reg()
14+
15+
wf_spec <- workflows::workflow(rec_spec, lm_spec)
16+
wf_fit <- parsnip::fit(wf_spec, data = mtcars)
17+
18+
orbital_obj <- orbital(wf_fit)
19+
20+
con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
21+
22+
expect_snapshot(
23+
show_query(orbital_obj, con = con)
24+
)
25+
26+
DBI::dbDisconnect(con)
27+
})

0 commit comments

Comments
 (0)