Lista de funciones en una función [duplicado]

Is there a way in R to list the functions that are contained in a given function?

For instance in the code below:

myFun <- function(x) { 
  res <- list(m1=mean(x), s1=sd(x), mi=min(x))
  return(res)
}

How to extract from the function myFun the names of the functions used. In this case I would like to have a vector with mean, sd and min.

I would like to do this without having to call the function (otherwise Rprof() would do the job).

preguntado el 12 de febrero de 14 a las 06:02

This is quite a nontrivial task. The only idea I have is to parse body(myFun) de alguna manera, con lsf.str("package:base") maybe? Probably an overkill. -

There's another problem: how would you choose between, say, mean y return? Both are functions from base: grep(ls("package:base"), pattern='return'), grep(ls("package:base"), pattern='mean'). -

1 Respuestas

Instale pryr via devtools and github:

require(devtools)
install_github("hadley/pryr")

Then simply walk into Mordor:

fun_calls(myFun)
[1] "{"      "<-"     "list"   "mean"   "sd"     "min"    "return"

Note how there are more functions than you expected, because so much in R is a function. Feel free to apply extra logic to remove common things you aren't interested in, like { y <- (which you get when you do an assign with =) y [ (which you get if you ever subset).

Respondido 12 Feb 14, 07:02

Another neat tool I didn't know about! I guess I'll have to learn how to browse the R-section of github. - Carlos Witthoft

pryr is used in Hadley's advanced R book, online. - Hombre espacial

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.