0

For example,

myfunc <- function (arg1, u=1, ...){
  print(arg1)
  print(u)
}

myfunc(3,arg=4)
# [1] 4
# [1] 3

I didn't want to match arg to arg1, and the first argument appears to be pushed to u. Are there any ways to avoid this behavior?

Max Candocia
  • 4,294
  • 35
  • 58
  • There's no real way around this. R will do partial matching on parameter names as long as it isn't ambiguous. I'm not sure exactly what result you expected here was. Knowing that we might be able to suggest alternatives. – MrFlick Mar 06 '20 at 20:22

1 Answers1

0

This (brief) text explains the order of argument evaluation in R functions: https://cran.r-project.org/doc/manuals/R-lang.html#Argument-matching

Also see @Richie Cotton's answer to Why does R use partial matching?

To your question, if you want no partial matching (e.g. to return an error if arg1 does not exist), you can create conditions where there are two partial matches. Take for example this edit to your function:

myfunc <- function (arg1, arg2=1, ...){
      print(arg1)
      print(arg2)
    }

#returns error "argument 2 matches multiple formal arguments"   
myfunc(3,arg=4)
SEAnalyst
  • 1,077
  • 8
  • 15