/
utils-approved.R
62 lines (60 loc) · 1.59 KB
/
utils-approved.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#' Build approved packages and functions tibble
#'
#' A utility function to help you build your approved packages and functions
#' list. This can be used by logrx to log unapproved use of packages and
#' functions.
#'
#' For more details see the vignette:
#' \code{vignette("approved", package = "logrx")}
#'
#' @param pkg_list Named list of character vectors where the name is the
#' package name with a character vector of approved functions or 'All'
#' @param pkg_list Named list of character vectors:
#' * Name is the package name
#' * Value is a character vector of approved functions or 'All'
#' @param file String. Name of file where the approved tibble will be written
#' to. If not specified, the tibble is returned
#'
#' Default: NULL
#'
#' Permitted Files: .RDS
#'
#' @return Tibble with two columns (library, function) and one row per function
#' @importFrom purrr map2_dfr
#' @export
#'
#' @examples
#' approved_pkgs <- list(
#' base = c("library", "mean"),
#' dplyr = "All"
#' )
#'
#' # build and return
#' build_approved(approved_pkgs)
#'
#' # build and save
#' dir <- tempdir()
#' build_approved(approved_pkgs, file.path(dir, "approved.rds"))
#'
build_approved <- function(pkg_list, file = NULL) {
approved <- purrr::map2_dfr(
names(pkg_list),
pkg_list,
~ {
all <- tibble::tibble(
function_name = getNamespaceExports(.x),
library = paste0("package:", .x)
)
if (.y[1] %in% c("All", "all")) {
all
} else {
all[all$function_name %in% .y, ]
}
}
)
if (is.null(file)) {
approved
} else {
saveRDS(approved, file)
}
}