How can I avoid partial matching in a function that uses ...?

Hi All,

How can I avoid partial matching in a function that uses ...?

I want arguments to match exactly

David

Hi, had a follow up question: can you give a reprex of the use case? From what I can tell if you use the exact name, then it works.

> myFunction = function(...) {
     varArgs <- list(...)
     print(varArgs$firstName)
 }
> myFunction(firstName = "John", firstLetter="J")
[1] "John"

vs

> myFunction = function(...) {
     varArgs <- list(...)
     print(varArgs$first)
 }
> myFunction(firstName = "John", firstLetter="J")
NULL

Thanks @joesho112358

My use case is I want to create a function that avoids partial matching as much as possible.

I think if I move the ... argument to the front of the function, it achieves that??

myFunction = function(arg = "john", ...) {
  list(arg, ...)
}
myFunction(arg = "Frank", firstLetter="J")
#> [[1]]
#> [1] "Frank"
#> 
#> $firstLetter
#> [1] "J"
myFunction(ar = "Frank", firstLetter="J")
#> [[1]]
#> [1] "Frank"
#> 
#> $firstLetter
#> [1] "J"


myFunction2 = function( ..., arg = "john") {
  list(arg, ...)
}
myFunction2(arg = "Frank", firstLetter="J")
#> [[1]]
#> [1] "Frank"
#> 
#> $firstLetter
#> [1] "J"
myFunction2(ar = "Frank", firstLetter="J")
#> [[1]]
#> [1] "john"
#> 
#> $ar
#> [1] "Frank"
#> 
#> $firstLetter
#> [1] "J"

Created on 2023-12-18 with reprex v2.0.2

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.