Skip to content

Instantly share code, notes, and snippets.

@jennybc
Last active December 26, 2019 18:15
Show Gist options
  • Save jennybc/0298232e4cddc58b7445469fa4398bed to your computer and use it in GitHub Desktop.
Save jennybc/0298232e4cddc58b7445469fa4398bed to your computer and use it in GitHub Desktop.
List all function signatures in a package
library(tidyverse)

## attach the package whose function signatures you want to examine
pkg <- "usethis"
library(pkg, character.only = TRUE)
x <- lsf.str(pos = paste0("package:", pkg))

## inspired by examining print.ls_str and head.function
get_signatures <- function(x) {
  x %>%
    set_names() %>%
    map(~ get(.x, envir = attr(x, "envir"), mode = attr(x, "mode"))) %>%
    map(~ noquote(head(.x)[ , 1, drop = TRUE])) %>%
    map(unname) %>%
    map(str_trim) %>%
    map(str_replace_all, "\"", "'") %>%
    map_chr(~ str_c(.x, collapse = " ")) %>%
    # recursive regex found in bonus section here
    # http://www.drregex.com/2017/11/match-nested-brackets-with-regex-new.html
    map(~ regmatches(.x, regexpr("\\((?:[^()]+|(?R))*+\\)", .x, perl = TRUE))) %>% 
    imap_chr(~ str_glue("{.y}{.x}")) %>%
    unname()
}

y <- get_signatures(x)
head(y)
#> [1] "browse_circleci(package = NULL)"                                                                                       
#> [2] "browse_cran(package = NULL)"                                                                                           
#> [3] "browse_github(package = NULL)"                                                                                         
#> [4] "browse_github_issues(package = NULL, number = NULL)"                                                                   
#> [5] "browse_github_pat(scopes = c('repo', 'gist', 'user:email'), description = 'R:GITHUB_PAT', host = 'https://github.com')"
#> [6] "browse_github_pulls(package = NULL, number = NULL)"

## example task: find exported usethis functions that call gihub_token()
## in the signature
z <- str_subset(y, "\\(.*github_token.*\\)")
z
#> [1] "create_from_github(repo_spec, destdir = NULL, fork = NA, rstudio = NULL, open = interactive(), protocol = git_protocol(), credentials = NULL, auth_token = github_token(), host = NULL)"                           
#> [2] "git_credentials(protocol = git_protocol(), auth_token = github_token())"                                                                                                                                           
#> [3] "use_github(organisation = NULL, private = FALSE, protocol = git_protocol(), credentials = NULL, auth_token = github_token(), host = NULL)"                                                                         
#> [4] "use_github_labels(repo_spec = github_repo_spec(), labels = character(), rename = character(), colours = character(), descriptions = character(), delete_default = FALSE, auth_token = github_token(), host = NULL)"
#> [5] "use_github_links(auth_token = github_token(), host = 'https://api.github.com', overwrite = FALSE)"                                                                                                                 
#> [6] "use_github_release(host = NULL, auth_token = github_token())"                                                                                                                                                      
#> [7] "use_tidy_labels(repo_spec = github_repo_spec(), auth_token = github_token(), host = NULL)"

## for pasting into GitHub issue
#clipr::write_clip(noquote(paste0("* ", encodeString(z, quote = "`")))) 

Created on 2019-12-26 by the reprex package (v0.3.0.9001)

library(tidyverse)
## attach the package whose function signatures you want to examine
pkg <- "usethis"
library(pkg, character.only = TRUE)
x <- lsf.str(pos = paste0("package:", pkg))
## inspired by examining print.ls_str and head.function
get_signatures <- function(x) {
x %>%
set_names() %>%
map(~ get(.x, envir = attr(x, "envir"), mode = attr(x, "mode"))) %>%
map(~ noquote(head(.x)[ , 1, drop = TRUE])) %>%
map(unname) %>%
map(str_trim) %>%
map(str_replace_all, "\"", "'") %>%
map_chr(~ str_c(.x, collapse = " ")) %>%
# recursive regex found in bonus section here
# http://www.drregex.com/2017/11/match-nested-brackets-with-regex-new.html
map(~ regmatches(.x, regexpr("\\((?:[^()]+|(?R))*+\\)", .x, perl = TRUE))) %>%
imap_chr(~ str_glue("{.y}{.x}")) %>%
unname()
}
y <- get_signatures(x)
head(y)
## example task: find exported usethis functions that call gihub_token()
## in the signature
z <- str_subset(y, "\\(.*github_token.*\\)")
z
## for pasting into GitHub issue
#clipr::write_clip(noquote(paste0("* ", encodeString(z, quote = "`"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment