Last active
July 25, 2020 08:08
-
-
Save martinctc/a8483d6affbb6a570741d59800fb3217 to your computer and use it in GitHub Desktop.
[Run a summary of functions and the associated description from a loaded package] #R
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
#' Run a summary of functions and the associated description | |
#' for a loaded package | |
#' | |
#' @param package_name String providing the name of the loaded package, e.g. surveytoolbox | |
#' | |
#' @examples | |
#' \dontrun{ | |
#' pkg_summary("surveytoolbox") | |
#' pkg_summary("wpa") | |
#' pkg_summary("rwa") | |
#' pkg_summary("sjlabelled") | |
#' } | |
#' | |
#' @export | |
generate_summary <- function(package_name){ | |
pkg_string <- paste0("package:", package_name) | |
fnc_names <- lsf.str(pkg_string) %>% as.character() | |
generate_fam <- function(fnc_names, package_name = "wpa"){ | |
fnc_names %>% | |
utils::help(eval(package_name)) %>% | |
utils:::.getHelpFile() %>% | |
purrr::keep(~attr(.x, "Rd_tag") == "\\seealso") %>% | |
purrr::map(as.character) %>% | |
purrr::flatten_chr() -> p | |
if(length(p) == 0){ | |
return("") | |
} else { | |
p[[2]] %>% # Extract second element | |
str_remove(., "Other ") %>% | |
str_remove(., ":") %>% | |
str_remove(., "[\n]") %>% | |
str_trim() | |
} | |
} | |
generate_desc <- function(fnc_names, package_name = "wpa"){ | |
fnc_names %>% | |
utils::help(eval(package_name)) %>% | |
utils:::.getHelpFile() %>% | |
purrr::keep(~attr(.x, "Rd_tag") == "\\description") %>% | |
purrr::map(as.character) %>% | |
purrr::flatten_chr() %>% | |
paste0(., collapse="") %>% | |
str_remove(., "[\n]") %>% | |
str_trim() | |
} | |
generate_title <- function(fnc_names, package_name = "wpa"){ | |
fnc_names %>% | |
utils::help(eval(package_name)) %>% | |
utils:::.getHelpFile() %>% | |
purrr::keep(~attr(.x, "Rd_tag") == "\\title") %>% | |
purrr::map(as.character) %>% | |
purrr::flatten_chr() %>% | |
paste0(., collapse="") %>% | |
str_remove(., "[\n]") %>% | |
str_trim() | |
} | |
generate_argument <- function(fnc_names, package_name = "wpa"){ | |
fnc_names %>% | |
utils::help(eval(package_name)) %>% | |
utils:::.getHelpFile() %>% | |
purrr::keep(~attr(.x, "Rd_tag") == "\\arguments") %>% | |
purrr::map(as.character) %>% | |
purrr::flatten_chr() -> p | |
if(length(p) == 0){ | |
return("") | |
} else { | |
p %>% | |
str_remove_all(., "[\n]") %>% | |
.[. != ""] %>% # remove blanks | |
str_trim() %>% | |
str_remove_all(., "list\\(") %>% # clean up | |
str_remove_all(., ",.+") %>% # remove argument description | |
str_remove_all(., "[:punct:]") %>% # remove all non-words | |
paste(collapse = "; ") | |
} | |
} | |
fnc_fam <- | |
fnc_names %>% | |
map_chr(generate_fam) | |
fnc_title <- | |
fnc_names %>% | |
map_chr(generate_title) | |
fnc_desc <- | |
fnc_names %>% | |
map_chr(generate_desc) | |
fnc_arg <- | |
fnc_names %>% | |
map_chr(generate_argument) | |
tibble(Functions = fnc_names, | |
Family = fnc_fam, | |
Title = fnc_title, | |
Description = fnc_desc, | |
Arguments = fnc_arg) %>% | |
return() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment