Extract comments from R source files, keep function in which they occurs

jangorecki

I would like to extract comments (matching to patterns) from my R source script keeping the functions in which they occurs.

The goal is to write documentation comments inside function body code using classic markdown checkboxes - [ ] or - [x] and extract those comments for further processing as list of character vectors - which I can easily write to new .md file.

Reproducible example and expected output below.

# preview the 'data'
script_body = c('# some init comment - not matching pattern','g = function(){','# - [x] comment_g1','# - [ ] comment_g2','1','}','f = function(){','# - [ ] comment_f1','# another non match to pattern','g()+1','}')
cat(script_body, sep = "\n")
# # some init comment - not matching pattern
# g = function(){
#     # - [x] comment_g1
#     # - [ ] comment_g2
#     1
# }
# f = function(){
#     # - [ ] comment_f1
#     # another non match to pattern
#     g()+1
# }

# populate R souce file
writeLines(script_body, "test.R")

# test it 
source("test.R")
f()
# [1] 2

# expected output
r = magic_function_get_comments("test.R", starts.with = c(" - [x] "," - [ ] "))
# r = list("g" = c(" - [x] comment_g1"," - [ ] comment_g2"), "f" = " - [ ] comment_f1")
str(r)
# List of 2
#  $ g: chr [1:2] " - [x] comment_g1" " - [ ] comment_g2"
#  $ f: chr " - [ ] comment_f1"
Konrad Rudolph

Here’s a stripped-down, unevaluated variant of what hrbmstr has done:

get_comments = function (filename) {
    is_assign = function (expr)
        as.character(expr) %in% c('<-', '<<-', '=', 'assign')

    is_function = function (expr)
        is.call(expr) && is_assign(expr[[1]]) && is.call(expr[[3]]) && expr[[3]][[1]] == quote(`function`)

    source = parse(filename, keep.source = TRUE)
    functions = Filter(is_function, source)
    fun_names = as.character(lapply(functions, `[[`, 2))
    setNames(lapply(attr(functions, 'srcref'), grep,
                    pattern = '^\\s*#', value = TRUE), fun_names)
}

This comes with a caveat: since we don’t evaluate the source, we may miss function definitions (for instance, we wouldn’t find f = local(function (x) x)). The above function uses a simple heuristic to find function definitions (it looks at all simple assignments of a function expression to a variable).

This can only be fixed using eval (or source), which comes with its own caveats — for instance, it’s a security risk to execute files from an unknown source.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How does cocoapods determine which files from source to keep in installation?

Extract values from comments (#) in R

Dynamically extract comments from files using cat

Extract package name and comments from package files

Which files to keep under source control for google-chrome-app?

How to extract the source files from a git repository?

Read multiple files but keep track of which file is which dataframe in R

which files/folders to exclude from source control

How to be able to extract comments from inside a function in doxygen?

When warnings occur in a loop in R, is it possible to report and extract which iteration it occurs in?

Extract Python function source text from the source code string

Strip // Comments From Files

Function to extract date from jpg files in a directory

Extract the line which are in comments with // aa well as /*...*/

Extract content from HTML comments

Is there a simple example of using antlr4 to create an AST from java source code and extract methods, variables and comments?

R, knitr, and source function: How to preserve source file comments for html report

Extract data from pdf files with R

Extract rows from csv files in R

R loop to extract CSV files from FTP

Select which rows to keep when using the distinct function in R

Add automatic header comments for all source files

How to put comments in device tree source files

Issue with ctags and indent (extract function definitions/declarations from source)

Using VBA to extract data from within a Javascript function source

How to extract data from complex function in R

Extract data from column aggregate function in R

Extract argument of function from string with R

Remove comments from file and keep integers

TOP Ranking

HotTag

Archive