Skip to content

v0.3.4

Choose a tag to compare

@tsostarics tsostarics released this 17 Sep 23:07
· 57 commits to main since this release

User-facing changes

Bug fixes:

  • contr.poly(n), throws an error if n is 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_contrasts no longer shows rownames
  • get_reference_levels works correctly for various types of inputs now
  • various issues with namespacing defused inputs work fine now

New functions:

  • as.unordered is now available, with the implementation largely being the same as base R's as.factor with the added conditional to remove the ordered class from the factor if the factor is ordered. is.unordered was already available.

QOL changes;

  • glimpse_contrasts should be faster now

Backend changes

  • glimpse_contrasts and related functions were refactored and now work much better
  • utility tryMatch is now available, which makes augmenting specific error messages with new messages much simpler. Compare in .get_dimnames (1) below. There is also a related helper stopWithMatch which takes a simpleError and a string (default deparse1(sys.call(1))) and augments the resulting error message. tryMatch can be used in place of tryCatch iff you do not need to run additional code when handling the error and only want to format an error message. stopWithMatch can be used as part of tryCatch, 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!