Skip to content

Commit

Permalink
protect tests and examples from missing suggested packages
Browse files Browse the repository at this point in the history
  • Loading branch information
topepo committed Oct 22, 2024
1 parent 9112801 commit c3ba770
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 3 deletions.
4 changes: 3 additions & 1 deletion R/blueprint-recipe-default.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
#' - It adds an intercept column onto `new_data` if `intercept = TRUE`.
#'
#' @export
#' @examples
#' @examplesIf rlang::is_installed(c("recipes"))
#' # example code
#'
#' library(recipes)
#'
#' # ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion R/mold.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#' - `extras`: Either `NULL` if the blueprint returns no extra information,
#' or a named list containing the extra information.
#'
#' @examples
#' @examplesIf rlang::is_installed(c("recipes"))
#' # See the method specific documentation linked in Description
#' # for the details of each blueprint, and more examples.
#'
Expand Down
2 changes: 1 addition & 1 deletion R/tune.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#'
#' @export
#'
#' @examples
#' @examplesIf rlang::is_installed(c("recipes"))
#' tune()
#' tune("your name here")
#'
Expand Down
4 changes: 4 additions & 0 deletions man/default_recipe_blueprint.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/mold.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/tune.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions tests/testthat/test-forge-recipe.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
test_that("simple forge works", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")
matrix_bp <- default_recipe_blueprint(composition = "matrix")

Expand Down Expand Up @@ -53,6 +56,9 @@ test_that("simple forge works", {
})

test_that("asking for the outcome works", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")
matrix_bp <- default_recipe_blueprint(composition = "matrix")

Expand Down Expand Up @@ -82,6 +88,8 @@ test_that("asking for the outcome works", {
})

test_that("asking for the outcome when it isn't there fails", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")
sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")
rec <- recipes::recipe(Species ~ Sepal.Length, data = iris)
x1 <- mold(rec, iris)
Expand All @@ -102,6 +110,9 @@ test_that("asking for the outcome when it isn't there fails", {
})

test_that("outcomes steps get processed", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")
rec <- recipes::recipe(Sepal.Width ~ Sepal.Length, data = iris)
rec <- recipes::step_log(rec, Sepal.Width)
Expand All @@ -123,6 +134,8 @@ test_that("outcomes steps get processed", {
})

test_that("missing predictor columns fail appropriately", {
skip_if_not_installed("recipes")

x <- mold(
recipes::recipe(Species ~ Sepal.Length + Sepal.Width, data = iris),
iris
Expand All @@ -140,6 +153,8 @@ test_that("missing predictor columns fail appropriately", {
})

test_that("novel predictor levels are caught", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")
sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")

dat <- data.frame(
Expand Down Expand Up @@ -174,6 +189,9 @@ test_that("novel predictor levels are caught", {
})

test_that("novel predictor levels can be ignored and handled by recipes", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

dat <- data.frame(
y = 1:4,
f = factor(letters[1:4])
Expand Down Expand Up @@ -227,6 +245,9 @@ test_that("novel predictor levels can be ignored and handled by recipes", {
})

test_that("novel predictor levels without any data are silently removed", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")

dat <- data.frame(
Expand Down Expand Up @@ -268,6 +289,9 @@ test_that("novel predictor levels without any data are silently removed", {
})

test_that("novel outcome levels are caught", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")

dat <- data.frame(
Expand Down Expand Up @@ -305,6 +329,9 @@ test_that("novel outcome levels are caught", {
})

test_that("original predictor and outcome classes / names are recorded", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")

rec <- recipes::recipe(Sepal.Length ~ Species, data = iris)
Expand Down Expand Up @@ -351,6 +378,9 @@ test_that("original predictor and outcome classes / names are recorded", {
})

test_that("new data classes are caught", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")
iris2 <- iris
iris2$Species <- as.character(iris2$Species)
Expand Down Expand Up @@ -401,6 +431,9 @@ test_that("new data classes are caught", {
})

test_that("new data classes can interchange integer/numeric", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")
iris2 <- iris
iris2$Sepal.Length <- as.integer(iris2$Sepal.Length)
Expand Down Expand Up @@ -432,6 +465,9 @@ test_that("new data classes can interchange integer/numeric", {
})

test_that("an `extras` slot exists for `roles`", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")
rec <- recipes::recipe(Species ~ Sepal.Length, data = iris)
x1 <- mold(rec, iris)
Expand All @@ -451,6 +487,8 @@ test_that("an `extras` slot exists for `roles`", {
})

test_that("non-standard roles generated in the recipe are returned by both `mold()` and `forge()`", {
skip_if_not_installed("recipes")

# This is a convention we follow to have consistency between `mold()` and `forge()`.
# Both should have the same `.$extras$roles$<name>` slot names.

Expand All @@ -474,6 +512,8 @@ test_that("non-standard roles generated in the recipe are returned by both `mold
})

test_that("`forge()` returns a slot for non standard roles that aren't required at `bake()` time", {
skip_if_not_installed("recipes")

# This is a convention we follow to have consistency between `mold()` and `forge()`.
# Both should have the same `.$extras$roles$<name>` slot names, even if there
# wasn't any data there at `forge()` time (because it wasn't a required role).
Expand Down Expand Up @@ -512,6 +552,8 @@ test_that("`forge()` returns a slot for non standard roles that aren't required
})

test_that("required non standard roles can be dropped during the baking process", {
skip_if_not_installed("recipes")

rec <- recipes::recipe(Species ~ ., iris)
rec <- recipes::update_role(rec, Sepal.Width, new_role = "dummy1")
rec <- recipes::update_role(rec, Sepal.Length, new_role = "dummy2")
Expand Down Expand Up @@ -543,6 +585,8 @@ test_that("required non standard roles can be dropped during the baking process"
})

test_that("`forge()` will error if required non standard roles are missing", {
skip_if_not_installed("recipes")

rec <- recipes::recipe(Species ~ ., iris)
rec <- recipes::update_role(rec, Sepal.Width, new_role = "dummy1")

Expand All @@ -556,6 +600,8 @@ test_that("`forge()` will error if required non standard roles are missing", {
})

test_that("recipes will error if the role is declared as not required, but really was", {
skip_if_not_installed("recipes")

rec <- recipes::recipe(Species ~ ., iris)
rec <- recipes::update_role(rec, Sepal.Width, new_role = "dummy1")
rec <- recipes::update_role_requirements(rec, "dummy1", bake = FALSE)
Expand All @@ -568,6 +614,8 @@ test_that("recipes will error if the role is declared as not required, but reall
})

test_that("`NA` roles are treated as extra roles that are required at `forge()` time", {
skip_if_not_installed("recipes")

# `Petal.Length`, `Petal.Width` have `NA` roles
rec <- recipes::recipe(iris)
rec <- recipes::update_role(rec, Sepal.Length, new_role = "predictor")
Expand All @@ -592,6 +640,8 @@ test_that("`NA` roles are treated as extra roles that are required at `forge()`
})

test_that("`NA` roles can be declared as not required at `forge()` time", {
skip_if_not_installed("recipes")

# Petal.Length, Petal.Width have `NA` roles
rec <- recipes::recipe(iris)
rec <- recipes::update_role(rec, Sepal.Length, new_role = "predictor")
Expand All @@ -614,6 +664,8 @@ test_that("`NA` roles can be declared as not required at `forge()` time", {
})

test_that("`extras` only hold roles that actually exist in the data", {
skip_if_not_installed("recipes")

df <- tibble(y = 1, x = 1, z = 2)

rec <- recipes::recipe(y ~ ., df)
Expand All @@ -628,6 +680,9 @@ test_that("`extras` only hold roles that actually exist in the data", {
})

test_that("Missing y value still returns `NULL` if no outcomes are asked for", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")
rec <- recipes::recipe(~Sepal.Width, data = iris)
x1 <- mold(rec, iris)
Expand All @@ -637,6 +692,9 @@ test_that("Missing y value still returns `NULL` if no outcomes are asked for", {
})

test_that("Missing y value returns 0 column tibble if outcomes are asked for", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

sparse_bp <- default_recipe_blueprint(composition = "dgCMatrix")
rec <- recipes::recipe(~Sepal.Width, data = iris)
x1 <- mold(rec, iris)
Expand All @@ -652,6 +710,9 @@ test_that("Missing y value returns 0 column tibble if outcomes are asked for", {
})

test_that("Predictors with multiple roles are only included once before baking (#120)", {
skip_if_not_installed("recipes")
skip_if_not_installed("Matrix")

rec <- recipes::recipe(Species ~ ., iris) # Implicit "predictor" role too
rec <- recipes::add_role(rec, Sepal.Length, new_role = "test1")
rec <- recipes::add_role(rec, Sepal.Length, new_role = "test2")
Expand All @@ -673,6 +734,8 @@ test_that("Predictors with multiple roles are only included once before baking (
})

test_that("`strings_as_factors` is used at `bake()` time", {
skip_if_not_installed("recipes")

df <- tibble(y = "a", x = "b")
rec <- recipes::recipe(y ~ x, data = df)

Expand All @@ -689,6 +752,8 @@ test_that("`strings_as_factors` is used at `bake()` time", {
})

test_that("`forge()` is compatible with hardhat 0.2.0 molded blueprints with a basic recipe", {
skip_if_not_installed("recipes")

path <- test_path("data", "hardhat-0.2.0-post-mold-recipe.rds")
object <- readRDS(path)

Expand All @@ -710,6 +775,8 @@ test_that("`forge()` is compatible with hardhat 0.2.0 molded blueprints with a b
})

test_that("`forge()` is compatible with hardhat 0.2.0 molded blueprints with a recipe with a nonstandard role", {
skip_if_not_installed("recipes")

path <- test_path("data", "hardhat-0.2.0-post-mold-recipe-nonstandard-role.rds")
object <- readRDS(path)

Expand Down
Loading

0 comments on commit c3ba770

Please sign in to comment.