Skip to content

Commit

Permalink
Merge pull request #631 from Ralayax/master
Browse files Browse the repository at this point in the history
Handle complex variable names with backticks
  • Loading branch information
stefvanbuuren authored Apr 17, 2024
2 parents d193383 + e357679 commit 3063d35
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
8 changes: 4 additions & 4 deletions R/formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "+")
)
}

Expand Down Expand Up @@ -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, ...)
Expand Down
4 changes: 4 additions & 0 deletions R/internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions R/sampler.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = backticks(j))
formula <- update(formula, ". ~ . ")
} else {
formula <- as.formula(paste0(j, " ~ 1"))
Expand All @@ -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 = "+")))
}
}

Expand Down

0 comments on commit 3063d35

Please sign in to comment.