There is an argument specialsfor terms, which allows you to specify named functions in a formula to retrieve by position.
So you can write
selectmds<-function(form,fn) {
tt<-terms(form,specials=fn);
idx<-attr(tt,"specials");
v<-as.list(attr(tt,"variables"))[-1];
unlist(lapply(idx,function(i) v[i]))
}
Then your test files give
> selectmds(formula(y~myfun(x)+z),"myfun")
$myfun
myfun(x)
> selectmds(formula(y~myfun(x)+z+myfun(zz)),"myfun")
$myfun1
myfun(x)
$myfun2
myfun(zz)
But you can also do
> selectmds(formula(y~myfun(x)+myfun(x2)+z+yourfun(zz)),c("myfun","yourfun"))
$myfun1
myfun(x)
$myfun2
myfun(x2)
$yourfun
yourfun(zz)
Where you can strike unlistso that it is nested using a named function.
source
share