Issue
What would be a proper way to programmatically test whether a given R function accepts its main input as dot-dot-dot or via named argument?
For example, consider the difference between max()
and log()
. Whereas max()
is defined to take ...
, log()
expects a numeric vector x
.
How can I test a function for that difference?
For example:
# ddd stands for dot-dot-dot
is_main_input_ddd(max) # true
is_main_input_ddd(log) # false
Solution
From the comments of @rawr and @camille:
is_main_input_ddd <- function(.f) {
names(formals(args(.f)))[1] == "..."
}
is_main_input_ddd(max)
#> [1] TRUE
is_main_input_ddd(log)
#> [1] FALSE
Created on 2022-01-28 by the reprex package (v2.0.1.9000)
Answered By – Emman
Answer Checked By – Terry (BugsFixing Volunteer)