formalArgs and formals are two functions that would be useful in this case. If you just need parameter names, then formalArgs will be more useful since it just gives names and ignores any default values. formals gives the list as output and provides the parameter name as the name of the element in the list, and the default value 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 formals to simply suggest formals , and if you just wanted parameter names, you could use names like names(formals(f)) . The formalArgs function formalArgs just a wrapper that does this for you anyway.
Edit: Note that technically primitive functions do not have βformals,β so this method returns NULL if used for primitives. To do this, you first need to transfer the function to args before moving on to formalArgs . This works whether 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"
Dason source share