From 3cfc34d7b40f322e863503e3148939647a896a8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bent=C3=A9geac?= Date: Tue, 26 Mar 2024 15:54:48 +0100 Subject: [PATCH 1/2] fix: handle variables with complex names --- R/formula.R | 8 ++++---- R/internal.R | 4 ++++ R/sampler.R | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/R/formula.R b/R/formula.R index c9a3facd9..cd8a41d24 100644 --- a/R/formula.R +++ b/R/formula.R @@ -43,8 +43,8 @@ make.formulas <- function(data, blocks = make.blocks(data), x <- "0" } formulas[[h]] <- paste( - paste(y, collapse = "+"), "~", - paste(x, collapse = "+") + paste(backticks(y), collapse = "+"), "~", + paste(backticks(x), collapse = "+") ) } @@ -190,12 +190,12 @@ extend.formula <- function(formula = ~0, # handle dot in RHS if (hasdot(formula)) { if (length(predictors) > 1) { - fr <- as.formula(c("~", paste(predictors, collapse = "+"))) + fr <- as.formula(c("~", paste(backticks(predictors), collapse = "+"))) } else { fr <- ~0 } } else { - fr <- reformulate(c(".", predictors)) + fr <- reformulate(c(".", backticks(predictors))) } if (auxiliary) formula <- update(formula, fr, ...) diff --git a/R/internal.R b/R/internal.R index 66f4689f2..98cdfa138 100644 --- a/R/internal.R +++ b/R/internal.R @@ -149,3 +149,7 @@ ma_exists <- function(x, pos, n_index = 1:8) { res <- list(is_there = is_there, obj = obj, pos = pos) return(res) } + +backticks <- function(varname) { + sprintf("`%s`", varname) +} diff --git a/R/sampler.R b/R/sampler.R index 595b9c110..cf747a408 100644 --- a/R/sampler.R +++ b/R/sampler.R @@ -187,7 +187,7 @@ sampler.univ <- function(data, r, where, pred, formula, method, yname, k, vars <- colnames(data)[pred != 0] xnames <- setdiff(vars, j) if (length(xnames) > 0L) { - formula <- reformulate(xnames, response = j) + formula <- reformulate(backticks(xnames), response = j) formula <- update(formula, ". ~ . ") } else { formula <- as.formula(paste0(j, " ~ 1")) @@ -199,7 +199,7 @@ sampler.univ <- function(data, r, where, pred, formula, method, yname, k, ymove <- setdiff(lhs(formula), j) formula <- update(formula, paste(j, " ~ . ")) if (length(ymove) > 0L) { - formula <- update(formula, paste("~ . + ", paste(ymove, collapse = "+"))) + formula <- update(formula, paste("~ . + ", paste(backticks(ymove), collapse = "+"))) } } From e357679b82a74c4624d880e499c1059c2cffc401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bent=C3=A9geac?= Date: Tue, 26 Mar 2024 16:05:39 +0100 Subject: [PATCH 2/2] fix: added more backticks --- R/sampler.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/sampler.R b/R/sampler.R index cf747a408..95362c330 100644 --- a/R/sampler.R +++ b/R/sampler.R @@ -187,7 +187,7 @@ sampler.univ <- function(data, r, where, pred, formula, method, yname, k, vars <- colnames(data)[pred != 0] xnames <- setdiff(vars, j) if (length(xnames) > 0L) { - formula <- reformulate(backticks(xnames), response = j) + formula <- reformulate(backticks(xnames), response = backticks(j)) formula <- update(formula, ". ~ . ") } else { formula <- as.formula(paste0(j, " ~ 1"))