Q:

R function to inspect number of arguments in another function?

Is there a built-in R function, or a way to write an R function, that can check how many inputs another function takes, and also lists names of optional arguments.
Let's call this desired function, f, then the following command:
f(dnorm)

should output
4

and
mean, sd, log

Since there are 4 arguments associated with 'dnorm' and and 3 optional arguments: mean, sd, log.
Or maybe this is not possible? Any insight is appreciated!

A:

You can try:
length(formals(dnorm))
# [1] 4
names(Filter(function(x) !is.symbol(x) || nchar(as.character(x)), formals(dnorm)))
# [1] "mean" "sd"   "log" 

Two functions technically, but gets the job done.  For the second one, you may need to play around a bit if the default arguments are complex.

