Skip to content

Commit

Permalink
Merge pull request #81 from mayer79/formula-tools
Browse files Browse the repository at this point in the history
Workaround around the problem of formula.tools
  • Loading branch information
mayer79 authored Dec 7, 2024
2 parents f3cd709 + acdeece commit 5e69834
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"))
Description: Alternative implementation of the beautiful 'MissForest'
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 13 additions & 4 deletions R/missRanger.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packaging.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) <doi:10.1093/bioinformatics/btr597>. Under the hood, it uses the
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit 5e69834

Please sign in to comment.