1
我试图找到哪些函数在某些代码中被使用。确定哪些函数在代码中
- 以为他们会一直跟着开口支架
- 循环正则表达式
grepl("someFunction\\(", code)
在函数名 - 指定一定是有什么比一个字母一样,下划线或点函数名的前面,因此
frame
未在data.frame(...)
中找到。这是可能与以下正则表达式:grepl("[^a-zA-Z_\\.]someFunction\\(", code)
- 在代码开头确保函数名仍然被前面加上空格代码
- 在函数名与
\\.
更换点发现:gsub(".","\\.",theFunctions, fixed=TRUE)
这里的一个最小的可重复性测试:
code <- "mean(pi); head(data.frame(A=1:5)); data_frame(7:9)"
funs <- c("mean", "head", "head.data.frame", "data.frame", "frame", "data_frame")
data.frame(isfound=sapply(paste0("[^a-zA-Z_\\.]",gsub(".","\\.",funs,fixed=TRUE),"\\("),
grepl, x=paste0(" ",code)),
shouldbefound=c(T,T,F,T,F,T))
这似乎工作,但太长,不太可读的人。
有没有一种更优雅的方式来确定哪些功能出现在某些代码中?