Skip to content

Commit

Permalink
Merge pull request #1381 from tidymodels/double-brackets
Browse files Browse the repository at this point in the history
fixes string interpolation failure for splines
  • Loading branch information
EmilHvitfeldt authored Oct 18, 2024
2 parents 3181a7e + f08e50d commit cfd1166
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: recipes
Title: Preprocessing and Feature Engineering Steps for Modeling
Version: 1.1.0.9000
Version: 1.1.0.9001
Authors@R: c(
person("Max", "Kuhn", , "max@posit.co", role = c("aut", "cre")),
person("Hadley", "Wickham", , "hadley@posit.co", role = "aut"),
Expand Down
10 changes: 9 additions & 1 deletion R/spline_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ spline2_create <- function(x, nm = "pred", .fn = "bSpline", df = 3, complete_set
vals <- c("bSpline", "cSpline", "iSpline", "mSpline", "naturalSpline", "bernsteinPoly")
.fn <- rlang::arg_match(.fn, vals)
fn_opts <- c(fn_opts, degree = degree)

if (.fn != "bernsteinPoly" && isTRUE(degree > (df - complete_set))) {
if (complete_set) {
cli::cli_abort(
Expand Down Expand Up @@ -46,6 +46,14 @@ spline2_create <- function(x, nm = "pred", .fn = "bSpline", df = 3, complete_set

spline_msg <- function(x) {
x <- as.character(x)
# Error messages can contain brackets (e.g. "Error in if (df < 0) { : missing value")
# For glue string interpolation, the default open/close deliminators the
# brackets. cli_abort calls rlang's abort and that can't pass the arguments
# to change the delimiters but will ignore them if they are doubled. So we
# change "{" to "{{" (and also for close). Simultaneous substitution via
# `pattern = "(\\{)|(\\})"` produces poor results so we do them one at a time.
x <- gsub("(\\{)", "\\1\\1", x)
x <- gsub("(\\})", "\\1\\1", x)
x <- strsplit(x, "\\n")[[1]]
cli::cli_abort(x)
}
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,19 @@
! The previous data will be used by `prep()`.
i The data passed using `training` will be ignored.

# spline error messages

Code
recipes:::spline_msg("Error in if (df < 0) { : missing blah blah\n")
Condition
Error in `recipes:::spline_msg()`:
! Error in if (df < 0) { : missing blah blah

---

Code
recipes:::spline_msg("craaazzyy {{}}{}{}")
Condition
Error in `recipes:::spline_msg()`:
! craaazzyy {{}}{}{}

10 changes: 10 additions & 0 deletions tests/testthat/test-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,13 @@ test_that("vars without role in predictor/outcome avoid string processing", {
expect_identical(new_lvls$chr_only_lime, list(values = NA, ordered = NA))
})

test_that("spline error messages", {
expect_snapshot(
recipes:::spline_msg("Error in if (df < 0) { : missing blah blah\n"),
error = TRUE
)
expect_snapshot(
recipes:::spline_msg("craaazzyy {{}}{}{}"),
error = TRUE
)
})

0 comments on commit cfd1166

Please sign in to comment.