Skip to content

Commit 5e69834

Browse files
authored
Merge pull request #81 from mayer79/formula-tools
Workaround around the problem of formula.tools
2 parents f3cd709 + acdeece commit 5e69834

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: missRanger
22
Title: Fast Imputation of Missing Values
3-
Version: 2.6.0
3+
Version: 2.6.1
44
Authors@R:
55
person("Michael", "Mayer", , "mayermichael79@gmail.com", role = c("aut", "cre"))
66
Description: Alternative implementation of the beautiful 'MissForest'

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# missRanger 2.6.1
2+
3+
### Improvement
4+
5+
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
6+
https://github.com/decisionpatterns/formula.tools/issues/11. We have added a workaround in [#81](https://github.com/mayer79/missRanger/pull/81).
7+
18
# missRanger 2.6.0
29

310
### Major bug fix

R/missRanger.R

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,20 @@ missRanger <- function(
426426
if (!inherits(formula, "formula")) {
427427
stop("'formula' should be a formula!")
428428
}
429-
formula <- as.character(formula)
430-
if (length(formula) != 3L) {
431-
stop("Formula must have left and right hand side. If it has: Don't load {formula.tools}. It breaks base R's as.character()")
429+
out <- as.character(formula)
430+
if (length(out) == 1L) {
431+
# {formula.tools} seems to be loaded, which breaks base's as.character().
432+
# This is a workaround.
433+
out <- strsplit(out, "~", fixed = TRUE)[[1L]]
434+
if (any(out == "")) {
435+
stop("Formula must have left and right hand side.")
436+
}
437+
out <- c("~", out)
438+
}
439+
if (length(out) != 3L) {
440+
stop("Formula must have left and right hand side.")
432441
}
433-
return(lapply(formula[2:3], FUN = .string_parser, data = data))
442+
return(lapply(out[2:3], FUN = .string_parser, data = data))
434443
}
435444

436445
# Checks if response type can be used in ranger (or easily converted to)

packaging.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ library(usethis)
1515
use_description(
1616
fields = list(
1717
Title = "Fast Imputation of Missing Values",
18-
Version = "2.6.0",
18+
Version = "2.6.1",
1919
Description = "Alternative implementation of the beautiful 'MissForest' algorithm used to impute
2020
mixed-type data sets by chaining random forests, introduced by Stekhoven, D.J. and
2121
Buehlmann, P. (2012) <doi:10.1093/bioinformatics/btr597>. Under the hood, it uses the

tests/testthat/test-helper.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ test_that(".formula_parser() works", {
3131
.formula_parser(. - Species ~ . - Species - Sepal.Length, data = iris),
3232
list(names(iris[1:4]), names(iris[2:4]))
3333
)
34+
35+
# Non-syntactic name
36+
ir <- iris
37+
colnames(ir)[1] <- "a b"
38+
expect_equal(
39+
.formula_parser(Sepal.Width ~ . - Petal.Length - Petal.Width - Species, ir),
40+
list("Sepal.Width", c("a b", "Sepal.Width"))
41+
)
3442
})
3543

3644
test_that(".check_response() works", {

0 commit comments

Comments
 (0)