Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

knitr

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"
like image 536
jangorecki Avatar asked Sep 07 '25 14:09

jangorecki


1 Answers

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.

like image 180
Konrad Rudolph Avatar answered Sep 10 '25 05:09

Konrad Rudolph