1

Followup to this question about proper checking of parameters against a list of values, is there a way to refer to an argument list of default values without using an external constant/variable?

For example, in the following, is it possible to refer to each argument list of default values without setting it outside the function?

ISO.open_bracket_left <- c("]", ")") # Current workaround
semi_open_intervals <-
  function(a, b, right_half_open=TRUE,
           open_bracket_left= ISO.open_bracket_left,
           open_bracket_right=c("[", "("))
{
  # See https://en.wikipedia.org/wiki/ISO_31-11
  if (missing(open_bracket_left) || !(open_bracket_left %in% ISO.open_bracket_left))
    open_bracket_left <- ISO.open_bracket_left[1]
  if (missing(open_bracket_right) || !(open_bracket_right %in% c("[", "(")))
    open_bracket_right <- "["

  bracket_left <- "["
  bracket_right <- "]"
  if (right_half_open) bracket_right <- open_bracket_right
  else bracket_left <- open_bracket_left
  paste0(bracket_left, a, ", ", b, bracket_right)
}

## Use case
semi_open_intervals(0, 7, right_half_open = FALSE, open_bracket_left = ")") ## ")0, 7]"
semi_open_intervals(0, 7, right_half_open = FALSE, open_bracket_left = "[[") ## "]0, 7]"
green diod
  • 1,399
  • 3
  • 14
  • 29
  • There was a similar discussion here and I have created a small package for that: https://stackoverflow.com/a/42987798/4468078 – R Yoda Jul 21 '17 at 17:29

1 Answers1

1

Not syntactic but still a language feature, the answer is in base::match.arg code with base::formals, see also function components for a description.

So instead of the workaround, if one really needs to refer to the argument list of default values, the way to do it is through base::formals. If you just need simple argument checking as in the toy example above, just use base::match.arg which does the job:

ISO.open_bracket_left <- c("]", ")") # Current workaround
semi_open_intervals <-
  function(a, b, right_half_open=TRUE,
           open_bracket_left= ISO.open_bracket_left,
           open_bracket_right=c("[", "("))
{
  # See https://en.wikipedia.org/wiki/ISO_31-11
  if (missing(open_bracket_left) || !(open_bracket_left %in% ISO.open_bracket_left))
    open_bracket_left <- ISO.open_bracket_left[1]

  ### Use base, duh!
  open_bracket_right <- match.arg(open_bracket_right)

  bracket_left <- "["
  bracket_right <- "]"
  if (right_half_open) bracket_right <- open_bracket_right
  else bracket_left <- open_bracket_left
  paste0(bracket_left, a, ", ", b, bracket_right)
}
green diod
  • 1,399
  • 3
  • 14
  • 29