From acdeecea0d37b1d364e1fc33d481ebb975de5f08 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Sat, 7 Dec 2024 09:02:48 +0100 Subject: [PATCH] Workaround around the problem of formula.tools --- DESCRIPTION | 2 +- NEWS.md | 7 +++++++ R/missRanger.R | 17 +++++++++++++---- packaging.R | 2 +- tests/testthat/test-helper.R | 8 ++++++++ 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f9cde66..7191c25 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: missRanger Title: Fast Imputation of Missing Values -Version: 2.6.0 +Version: 2.6.1 Authors@R: person("Michael", "Mayer", , "mayermichael79@gmail.com", role = c("aut", "cre")) Description: Alternative implementation of the beautiful 'MissForest' diff --git a/NEWS.md b/NEWS.md index c958b21..bed8842 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# missRanger 2.6.1 + +### Improvement + +Solves an incompatibility with the {formula.tools} package. `formula.tools:::as.character.formula()` breaks `base::as.character()` for formulas, which prevented {missRanger} from working, see also +https://github.com/decisionpatterns/formula.tools/issues/11. We have added a workaround in [#81](https://github.com/mayer79/missRanger/pull/81). + # missRanger 2.6.0 ### Major bug fix diff --git a/R/missRanger.R b/R/missRanger.R index e94febe..f5a5a6a 100644 --- a/R/missRanger.R +++ b/R/missRanger.R @@ -426,11 +426,20 @@ missRanger <- function( if (!inherits(formula, "formula")) { stop("'formula' should be a formula!") } - formula <- as.character(formula) - if (length(formula) != 3L) { - stop("Formula must have left and right hand side. If it has: Don't load {formula.tools}. It breaks base R's as.character()") + out <- as.character(formula) + if (length(out) == 1L) { + # {formula.tools} seems to be loaded, which breaks base's as.character(). + # This is a workaround. + out <- strsplit(out, "~", fixed = TRUE)[[1L]] + if (any(out == "")) { + stop("Formula must have left and right hand side.") + } + out <- c("~", out) + } + if (length(out) != 3L) { + stop("Formula must have left and right hand side.") } - return(lapply(formula[2:3], FUN = .string_parser, data = data)) + return(lapply(out[2:3], FUN = .string_parser, data = data)) } # Checks if response type can be used in ranger (or easily converted to) diff --git a/packaging.R b/packaging.R index 6c0183f..664f379 100644 --- a/packaging.R +++ b/packaging.R @@ -15,7 +15,7 @@ library(usethis) use_description( fields = list( Title = "Fast Imputation of Missing Values", - Version = "2.6.0", + Version = "2.6.1", Description = "Alternative implementation of the beautiful 'MissForest' algorithm used to impute mixed-type data sets by chaining random forests, introduced by Stekhoven, D.J. and Buehlmann, P. (2012) . Under the hood, it uses the diff --git a/tests/testthat/test-helper.R b/tests/testthat/test-helper.R index 7562f24..d946263 100644 --- a/tests/testthat/test-helper.R +++ b/tests/testthat/test-helper.R @@ -31,6 +31,14 @@ test_that(".formula_parser() works", { .formula_parser(. - Species ~ . - Species - Sepal.Length, data = iris), list(names(iris[1:4]), names(iris[2:4])) ) + + # Non-syntactic name + ir <- iris + colnames(ir)[1] <- "a b" + expect_equal( + .formula_parser(Sepal.Width ~ . - Petal.Length - Petal.Width - Species, ir), + list("Sepal.Width", c("a b", "Sepal.Width")) + ) }) test_that(".check_response() works", {