R - formalArgs() and args(): functions retrieve the formal arguments of a function

13 Aug 2018

Aim: retrieving formal arguments of a function

If the function of is not a primitive, use formalArgs:

formalArgs(ls)

[1] “name” “pos” “envir” “all.names” “pattern” “sorted”


But formalArgs will return NULL if the function is a primitive (primitives don’t have formals):

formalArgs(abs) NULL —

Check if the function is a primitive:

is.primitive(abs)

[1] TRUE


If the function is a primitive, use args, and pass the output of args to formals or formalArgs:


args(abs)

function (x)

NULL



formalArgs(args(abs))

[1] “x”


formalArgs(args()) works for both primitives and non primitives:


formalArgs(args(“ls”))

[1] “name” “pos” “envir” “all.names” “pattern” “sorted”


[ R  function  ]