Lista de funciones en una función [duplicado]
Frecuentes
Visto 85 equipos
2
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).
1 Respuestas
8
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 r function or haz tu propia pregunta.
This is quite a nontrivial task. The only idea I have is to parse
body(myFun)
de alguna manera, conlsf.str("package:base")
maybe? Probably an overkill. - tonytonovThere's another problem: how would you choose between, say,
mean
yreturn
? Both are functions frombase
:grep(ls("package:base"), pattern='return')
,grep(ls("package:base"), pattern='mean')
. - tonytonov