Retrieving information about using functions and packages in R scripts

What would be the correct way to retrieve function calls and their resulting string in one or more R scripts? Is there a function or package of the parsing base that allows me to do this, or should I build a solution with regular expressions?

For instance:

function_calls("project1/exploratory_analysis.R") 

should output the data file as:

 ## function line filename ## 1 tapply 35 exploratory_analysis.R ## 2 qplot 80 exploratory_analysis.R 

Ultimately, I want to create a reverse index of function calls and loaded packages used in one or more R scripts for educational and reference purposes. (for example, used as a repository with usage examples). For instance:

 -------------------------------------------------------- | function | source_file | line | package | |:--------:|:----------------------:|:-----:|:--------:| | tapply | exploratory_analysis.R | 35 | base | | qplot | exploratory_analysis.R | 80 | ggplot2 | | cor | regression.R | 15 | stats | | cor | regression.R | 27 | stats | | tapply | regression.R | 12 | base | | fromJSON | load_dataset.R | 5 | jsonlite | | %>% | transformation.R | 10 | magrittr | -------------------------------------------------------- 

You can use some regular expressions to extract function calls, but I was wondering if there was parsing, static code analysis, or a reflection function supporting this task.

I believe that combining parse() , substitute() , getParseData() , deParse() , srcfile() (or maybe functions in mvbutils , codetools ) would allow, but I'm not familiar with using them to figure it out on your own.

+6
source share

All Articles