问题描述
假设我有一个用这种表示法表示的函数 shell
,或者字符串表示法“shell”。
@MrFlick 提供了一些代码来列出主函数的所有子函数(内部函数调用)。
Regular expression to find function calls in a function body
call.ignore <-c("[[","[","&","&&","|","||","==","!=","-","+","*","/","!",">","<",":")
find.funcs <- function(f,descend=FALSE) {
if( is.function(f)) {
return(find.funcs(body(f),descend=descend))
} else if (is(f,"name") | is.atomic(f)) {
return(character(0))
}
v <- list()
if (is(f,"call") && !(deparse(f[[1]]) %in% call.ignore)) {
v[[1]] <- deparse(f)
if(!descend) return(v[[1]])
}
v <- append(v,lapply(as.list(f),find.funcs,descend=descend))
unname(do.call(c,v))
}
这个功能相当健壮。
find.functions(shell); # works as expected
find.functions("shell"); # breaks
fn = "shell";
find.functions(fn); # breaks
如何扩展它以允许字符串表示法“shell”以便函数是可变参数的?
似乎可以根据 return(character(0))
是否是字符串来更新它,但是通过递归,我没有让它按预期工作。
fstr = "shell";
f <- deparse(body(get(fstr)));
或
fstr = "shell";
f = eval(parse(text=fstr));
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)