2017-03-06 28 views
2

我想列出一個函數在一個或多個R腳本文件中的使用頻率。我已經找到了功能NCmisc::list.functions.in.file,這是非常接近我想要的東西:計算我的R腳本文件中寫入的函數的頻率?

library(stringr) 
cat("median(iris$Sepal.Length)\n median(iris$Sepal.Width)\n library(stringr); str_length(iris$Species) \n", file = "script.R") 
list.functions.in.file("script.R") 

    package:base package:stats package:stringr 
     "library"  "median" "str_length" 

注意median在腳本中使用了兩次,但list.functions.in.file不使用此信息,並且只列出了每一個獨特的功能。有沒有可以產生這種頻率的軟件包?並且能夠分析多個R腳本的語料庫,而不僅僅是一個文件。

(注意,這是不是在遞歸計數函數調用,例如,我想避免執行腳本)

回答

2

NCmisc function是隻是一個包裝圓utils::parseutils::getParseData,所以你可以使自己的功能(然後你不需要依賴於NCmisc

count.function.names <- function(file) { 
    data <- getParseData(parse(file=file)) 
    function_names <- data$text[which(data$token=="SYMBOL_FUNCTION_CALL")] 
    occurrences <- data.frame(table(function_names)) 
    result <- occurrences$Freq 
    names(result) <- occurrences$function_names 
    result 
} 

應該做你想要什麼......

+0

哎呦感謝 –

+0

感謝tim_yates工作GRE !!!在。 :d – aflindst