Ensure no duplicate terms appear in
validate - asserts the following:
formulamust not have duplicates terms on the left and right hand side of the formula.
check - returns the following:
okA logical. Does the check pass?
duplicatesA character vector. The duplicate terms.
validate_no_formula_duplication(formula, original = FALSE) check_no_formula_duplication(formula, original = FALSE)
A formula to check.
A logical. Should the original names be checked, or should the names after processing be used? If
y ~ log(y)is allowed because the names are
y ~ log(y)is not allowed because the original names are both
check_no_formula_duplication() returns a named list of two components,
hardhat provides validation functions at two levels.
check_*(): check a condition, and return a list. The list always contains at least one element,
ok, a logical that specifies if the check passed. Each check also has check specific elements in the returned list that can be used to construct meaningful error messages.
validate_*(): check a condition, and error if it does not pass. These functions call their corresponding check function, and then provide a default error message. If you, as a developer, want a different error message, then call the
check_*()function yourself, and provide your own validation function.
# All good check_no_formula_duplication(y ~ x) #> $ok #>  TRUE #> #> $duplicates #> character(0) #> # Not good! check_no_formula_duplication(y ~ y) #> $ok #>  FALSE #> #> $duplicates #>  "y" #> # This is generally okay check_no_formula_duplication(y ~ log(y)) #> $ok #>  TRUE #> #> $duplicates #> character(0) #> # But you can be more strict check_no_formula_duplication(y ~ log(y), original = TRUE) #> $ok #>  FALSE #> #> $duplicates #>  "y" #> # This would throw an error try(validate_no_formula_duplication(log(y) ~ log(y))) #> Error in validate_no_formula_duplication(log(y) ~ log(y)) : #> The following terms are duplicated on the left and right hand side of the `formula`: 'log(y)'.