checkmate icon indicating copy to clipboard operation
checkmate copied to clipboard

let "check_names" report back vname()s, not actual choices

Open maxheld83 opened this issue 9 years ago • 0 comments

check_names() is great, except that sometimes, when the subset.of argument is pretty big, the output can be a bit unwieldy:

[1] "Must be a subset of set {meet_material_needs,live_to_own_values,good_relative_income,stable_environment,knowledge,feel_at_home,feel_represented,open_natural_spaces,meaningful_activity,friendship}"

I worry that my users will be flabbergasted, and not understand that the problem lies with consistency of x and subset.of. Unhelpfully, the error message doesn't even tell us, what subset.of was.

Perhaps, something like the below might be a helpful alternative:

# helper: check whether a is subset of b
check_names_many <- function(x, type = "named", permutation.of = NULL, subset.of = NULL, identical.to = NULL) {
  res <- check_names(x = x, type = type, permutation.of = permutation.of, subset.of = subset.of, identical.to = identical.to)
  if (!isTRUE(res)) {
    if (!is.null(permutation.of)) {
      return(paste(vname(x), "must be permutation of", vname(permutation.of)))
    } else if (!is.null(subset.of)) {
      return(paste(vname(x), "must be subset of", vname(subset.of)))
    } else if (!is.null(identical.to)) {
      return(paste(vname(x), "must be identical to", vname(identical.to)))
    }
  } else {
    return(TRUE)
  }
}

This would give a more succinct error message in terms of vname()s, not in terms of the actual content of whichever sets were passed to, say subset.of:

obj1 <- c("foo", "bar")
obj2 <- c("foo", "baz")
check_names_many(x = obj2, identical.to = obj1)

gives:

 # [1] "obj2 must be identical to obj2"

(Obviously something is wrong with my use of vname().

This is potentially more informative to the user.

My above function is an ugly hackjob, and it doesn't seem like an elegant solution that fits in well with checkmate(), just wanted to add this as an illustration.

maxheld83 avatar Dec 09 '16 16:12 maxheld83