v0.3.4
User-facing changes
Bug fixes:
contr.poly(n), throws an error ifnis greater than 95. This caused various functions to fail even with unordered factors with large numbers of factor levels because of the manual check for polynomial contrasts. Now the comparison is only performed if (1) no coding function is provided and (2) n is greater than 95. In situations where an ordered column's contrasts are queried (as in glimpse_contrasts when show_all_factors is TRUE and the column is not set to a non-polynomial scheme) then the error will still be thrown but will be augmented with a helpful message. (#35)glimpse_contrastsno longer shows rownamesget_reference_levelsworks correctly for various types of inputs now- various issues with namespacing defused inputs work fine now
New functions:
as.unorderedis now available, with the implementation largely being the same as base R'sas.factorwith the added conditional to remove theorderedclass from the factor if the factor is ordered.is.unorderedwas already available.
QOL changes;
glimpse_contrastsshould be faster now
Backend changes
glimpse_contrastsand related functions were refactored and now work much better- utility
tryMatchis now available, which makes augmenting specific error messages with new messages much simpler. Compare in.get_dimnames(1) below. There is also a related helperstopWithMatchwhich takes a simpleError and a string (defaultdeparse1(sys.call(1))) and augments the resulting error message.tryMatchcan be used in place oftryCatchiff you do not need to run additional code when handling the error and only want to format an error message.stopWithMatchcan be used as part oftryCatch, see (2) below.
(1) Much less boilerplate to remember:
tryCatch(dimnames(stats::contrasts(factor_col)),
error = \(e) {
err <- conditionMessage(e)
if (!grepl("cannot be represented accurately", err)) {
stop(c)
}
msg <- paste0("Polynomial contrasts can only be used with <95 levels.\n",
"Convert to unordered with `as.unordered()` or use a non-polynomial scheme")
stop(paste(err, msg, sep = "\n"))
})
tryMatch(dimnames(stats::contrasts(factor_col)),
"cannot be represented accurately" =
c("Polynomial contrasts can only be used with <95 levels.",
"Convert to unordered with `as.unordered` or use a non-polynomial scheme.")
)
(2) foo() runs additional code, foo2() only uses augmented error messages:
foo <- function(a) {
tryCatch(a, error = \(e) {
message("An error! have a random number: ", rnorm(1))
stopWithMatch(e, "not found" = "how sad!")
})
}
foo2 <- function(a) {
tryMatch(a, "not found" = "how sad!")
}
foo(a + 1)
foo2(a + 1)> foo(a + 1)
An error! have a random number: -1.27538246066647
Error: In `foo(a+1)`:
object 'a' not found
ℹ how sad!
> foo2(a + 1)
Error: In `foo2(a+1)`:
object 'a' not found
ℹ how sad!