Last active
September 10, 2024 05:01
-
-
Save johnbaums/c7711c2c55cb9f21fcaae30e025ffb2f to your computer and use it in GitHub Desktop.
Get the unique set of namespaces specified in an R script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get_namespaces <- function(file, as_imports=FALSE) { | |
require(stringi) | |
require(dplyr) | |
nm <- readLines(file) %>% | |
stringi::stri_extract_all(regex='[\\w_.]+::[\\w_.]+') %>% | |
unlist %>% | |
setdiff(NA) %>% | |
sort | |
if(as_imports) { | |
imports <- strsplit(nm, '::') | |
if(!length(imports)) return(NULL) | |
imports %>% | |
do.call(rbind.data.frame, .) %>% | |
setNames(c('ns', 'fun')) %>% | |
group_by(ns) %>% | |
summarise( | |
string = sprintf("#' @importFrom %s %s", | |
cur_group(), paste(fun, collapse=' ')), | |
.groups='drop' | |
) %>% | |
pull(string) %>% | |
cat(sep='\n') | |
} else { | |
nm | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment