From f23a8b0ff6ff38b8daabfa77d070c5fbdc0a4543 Mon Sep 17 00:00:00 2001 From: fweber144 Date: Wed, 22 Mar 2023 13:17:01 +0100 Subject: [PATCH] Add an error message for issue #319. --- R/formula.R | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/R/formula.R b/R/formula.R index 0081b3f8a..6956b37cd 100644 --- a/R/formula.R +++ b/R/formula.R @@ -29,6 +29,7 @@ extract_terms_response <- function(formula) { hier <- grepl("\\|", terms_) int <- grepl(":", terms_) group_terms <- terms_[hier] + group_terms <- parse_group_terms(group_terms) interaction_terms <- terms_[int & !hier] individual_terms <- terms_[!hier & !int] additive_terms <- parse_additive_terms(individual_terms) @@ -92,6 +93,28 @@ extract_response <- function(response) { return(response_name_ch) } +# Parse group terms +# +# @param group_terms Character vector of group terms, but as extracted by +# labels(terms()), i.e., lacking the surrounding parentheses. +# +# @return Character vector of parsed group terms, but as extracted by +# labels(terms()), i.e., lacking the surrounding parentheses. +parse_group_terms <- function(group_terms) { + has_call <- sapply(strsplit(group_terms, "\\|"), function(grp_trm_split) { + if (length(grp_trm_split) != 2) { + stop("Unexpected number of `|` characters in group terms. Please ", + "contact the package maintainer.") + } + grepl("\\(", grp_trm_split[2]) + }) + if (any(has_call)) { + stop("Function calls on the right-hand side of a group-term `|` ", + "character are not allowed.") + } + return(group_terms) +} + ## Parse additive terms (smooth terms) from a list of individual terms. See ## `?init_refmodel` for allowed smooth terms. ## @param terms list of terms to parse