Get the argument names of an R function
Asked Answered
B

1

33

For an arbitrary function

f <- function(x, y = 3){
  z <- x + y
  z^2
}

I want to be able take the argument names of f

> argument_names(f)
[1] "x" "y"

Is this possible?

Ber answered 16/11, 2016 at 19:7 Comment(5)
Check out ?formalsBerny
Awesome! Both work. Feel free to answer. I will accept the first.Ber
help("args") or one of the functions linked there.Cherisecherish
@Ber Oh hey I know you. Small world.Berny
Yeah @Dason, I remember you from grad school. How about that? Zheyuan Li declined his chance to claim the best answer. Want some easy points?Ber
B
39

formalArgs and formals are two functions that would be useful in this case. If you just want the parameter names then formalArgs will be more useful as it just gives the names and ignores any defaults. formals gives a list as the output and provides the parameter name as the name of the element in the list and the default as the value of the element.

f <- function(x, y = 3){
  z <- x + y
  z^2
}

> formalArgs(f)
[1] "x" "y"
> formals(f)
$x


$y
[1] 3

My first inclination was to just suggest formals and if you just wanted the names of the parameters you could use names like names(formals(f)). The formalArgs function just is a wrapper that does that for you so either way works.

Edit: Note that technically primitive functions don't have "formals" so this method will return NULL if used on primitives. A way around that is to first wrap the function in args before passing to formalArgs. This works regardless of it the function is primitive or not.

> # formalArgs will work for non-primitives but not primitives
> formalArgs(f)
[1] "x" "y"
> formalArgs(sum)
NULL
> # But wrapping the function in args first will work in either case
> formalArgs(args(f))
[1] "x" "y"
> formalArgs(args(sum))
[1] "..."   "na.rm"
Berny answered 16/11, 2016 at 19:19 Comment(4)
@MichaelChirico Hmmm. Looks like anything that is just a wrapper to a .primitive call has the same issue.Berny
yea I was trying to find all the one-argument functions in base and ran into this issue -- all the good ones are just primitive wrappersPantelegraph
@Pantelegraph I think I found a workaround. Technically formalArgs providing NULL for primitives isn't a bug. The help page for formals says "Only closures have formals, not primitive functions." which explains why this happens. args itself returns a closure which is why the 'trick' gives us what we want.Berny
Note that formals() also works for f(x=1, y = a) (a being an object like a <- 3). Then (for my use case) you can even do: formals(f)$y == 'a'.Denson

© 2022 - 2024 — McMap. All rights reserved.