Finding Function Calls in R Code | /en/2023/01/func-call/ #1467
Replies: 2 comments 6 replies
-
This is super helpful--thanks! Do you know of anything similar for extracting functions from R Markdown/Quarto files (both in the text and code chunks)? For example, if I wanted to find all of the functions referenced in a chapter of R4DS. |
Beta Was this translation helpful? Give feedback.
5 replies
-
Thanks for your help. Everything seems to have worked fine. I had to tweak the functions a little bit. Here's what I ended up with: extract_code <- function(file) {
x <- xfun::read_utf8(file)
res <- litedown::crack(x)
unlist(lapply(res, function(el) {
if (el$type == "code_chunk") el$source
}))
}
extract_functions <- function(code) {
d <- getParseData(x = parse(text = code, keep.source = TRUE))
f <- d[d$token == 'SYMBOL_FUNCTION_CALL', 'text']
for (s in d[d$token == 'SYMBOL', 'text']) {
tryCatch({
ev <- eval(as.symbol(s), parent.frame())
if (is.function(ev)) f = c(f, s)
}, error = function(e) NULL)
}
f
} Are you OK with me adding these functions to my flashr package under an MIT License (with a link to this page in the documentation)? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Finding Function Calls in R Code
Yesterday I learned an unexpected but interesting use of the highr package from a Github issue. This package is intended for syntax highlighting R code, but the user wanted to identify function calls …
https://yihui.org/en/2023/01/func-call/
Beta Was this translation helpful? Give feedback.
All reactions