我想從我的R源代碼腳本中提取註釋(與模式匹配),以保留它們發生的功能。從R源文件中提取註釋,保留它們發生的功能
的目標是編寫使用經典的降價複選框- [ ]
或- [x]
內部函數體代碼文檔註釋,並提取作進一步處理這些意見作爲特徵向量的名單 - 我可以輕鬆編寫新.md
文件。
下面是可重複使用的示例和預期輸出。
# 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"
jangorecki - 你真的接受這個答案,所以我可以刪除我(康拉德的是一個更好的解決方案) – hrbrmstr
@hrbrmstr恭敬地不同意。 –
@hrbrmstr我會接受這個但不刪除你的,它回答問題,它是好的,它教人,我也看到它的價值! – jangorecki