[R-pkg-devel] R feature suggestion: Duplicated function arguments check

Vincent van Hees v|ncentv@nhee@ @end|ng |rom gm@||@com
Mon Nov 8 16:29:55 CET 2021


Not sure if this is the best place to post this message, as it is more of a
suggestion than a question.

When an R function accepts more than a handful of arguments there is the
risk that users accidentally provide arguments twice, e.g myfun(A=1, B=2,
C=4, D=5, A=7), and if those two values are not the same it can have
frustrating side-effects. To catch this I am planning to add a check for
duplicated arguments, as shown below, in one of my own functions. I am now
wondering whether this would be a useful feature for R itself to operate in
the background when running any R function that has more than a certain
number of input arguments.

Cheers, Vincent

myfun = function(...) {
  #check input arguments for duplicate assignments
  input = list(...)
  if (length(input) > 0) {
    argNames = names(input)
    dupArgNames = duplicated(argNames)
    if (any(dupArgNames)) {
      for (dupi in unique(argNames[dupArgNames])) {
        dupArgValues = input[which(argNames %in% dupi)]
        if (all(dupArgValues == dupArgValues[[1]])) { # double arguments,
but no confusion about what value should be
          warning(paste0("\nArgument ", dupi, " has been provided more than
once in the same call, which is ambiguous. Please fix."))
        } else { # double arguments, and confusion about what value should
be,
          stop(paste0("\nArgument ", dupi, " has been provided more than
once in the same call, which is ambiguous. Please fix."))
        }
      }
    }
  }
  # rest of code...
}

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list