Debian Codegraph @

R/aaa-documentation-helper.R

raw
1 # nocov start
2
3 # TODO:
4 # - catch errors and throw a warning visible to the user
5 # - warn when custom arg is taking precedence over something well-defined
6
7 # starter boiler plate like: returns a one-row data.frame with the columns
8 # .pre = before items, .post = after items
9
10 return_evalrd <- function(..., .method, .pre = NULL, .post = NULL) {
11 glos_env <- new.env()
12 data("column_glossary", package = "modeltests", envir = glos_env)
13 cols <- list(...)
14
15 # figure out which arguments are custom documentation and what should
16 # get pulled straight from the model glossary
17
18 custom_cols <- tibble()
19 use_custom_doc <- !is.null(names(cols))
20 pull_from_modeltests <- as.character(cols)
21
22 if (use_custom_doc) {
23 idx <- purrr::map_lgl(names(cols), \(x) x != "")
24 custom_doc <- cols[idx]
25 pull_from_modeltests <- pull_from_modeltests[!idx]
26 pull_from_modeltests <- setdiff(pull_from_modeltests, names(custom_doc))
27
28 not_found <- setdiff(pull_from_modeltests, glos_env$column_glossary$column)
29
30 if (length(not_found) > 0) {
31 cli::cli_abort(
32 c(
33 "Tried to use modeltests documentation for: {not_found} column{?s}.",
34 "i" = "No documentation could be found."
35 )
36 )
37 }
38
39 custom_cols <- tibble(
40 column = names(custom_doc),
41 description = as.character(custom_doc)
42 )
43 }
44
45 glos <- glos_env$column_glossary |>
46 filter(purrr::map_lgl(column, \(x) x %in% pull_from_modeltests)) |>
47 filter(method == !!.method) |>
48 bind_rows(custom_cols)
49
50 row_to_item <- function(column, description) {
51 glue(" \\item{{{column}}}{{{description}}}")
52 }
53
54 items <- with(glos, purrr::map2_chr(column, description, row_to_item))
55 items <- paste(items, collapse = "\n")
56
57 result <- paste(
58 "\\value{",
59 .pre,
60 items,
61 .post,
62 "}",
63 sep = "\n",
64 collapse = "\n"
65 )
66
67 # check that all arguments resulted in some form of documentation
68 standard_cols <- if (is.null(names(cols))) {
69 unlist(cols)
70 } else {
71 unlist(cols[names(cols) == ""])
72 }
73 cols_exps <- paste0("\\item\\{", standard_cols)
74 written <- purrr::map_lgl(cols_exps, stringr::str_detect, string = result)
75 missing_cols <- standard_cols[!written]
76 if (length(missing_cols) != 0) {
77 cli::cli_inform(
78 "The {.code return_{.method}} input {.val {missing_cols}} did not
79 result in any documentation being written."
80 )
81 }
82
83 result
84 }
85
86 return_glance <- function(..., .pre = NULL, .post = NULL) {
87 if (is.null(.pre)) {
88 .pre <- paste(
89 "A \\code{\\link[tibble:tibble]{tibble::tibble()}} with exactly",
90 "one row and columns:"
91 )
92 }
93
94 return_evalrd(..., .pre = .pre, .post = .post, .method = "glance")
95 }
96
97 return_tidy <- function(..., .pre = NULL, .post = NULL, regression = FALSE) {
98 if (is.null(.pre)) {
99 .pre <- "A \\code{\\link[tibble:tibble]{tibble::tibble()}} with columns:"
100 }
101
102 args <- list(
103 ...,
104 .pre = .pre,
105 .post = .post,
106 .method = "tidy"
107 )
108
109 if (regression) {
110 args <- c(
111 args,
112 "term",
113 "estimate",
114 "std.error",
115 "statistic",
116 "p.value",
117 "conf.low",
118 "conf.high"
119 )
120 }
121
122 do.call("return_evalrd", args)
123 }
124
125 return_augment <- function(
126 ...,
127 .pre = NULL,
128 .post = NULL,
129 .fitted = TRUE,
130 .resid = TRUE
131 ) {
132 if (is.null(.pre)) {
133 .pre <- "A \\code{\\link[tibble:tibble]{tibble::tibble()}} with columns:"
134 }
135
136 args <- list(
137 ...,
138 .pre = .pre,
139 .post = .post,
140 .method = "augment"
141 )
142
143 if (.fitted) {
144 args <- c(args, ".fitted")
145 }
146
147 if (.resid) {
148 args <- c(args, ".resid")
149 }
150
151 do.call("return_evalrd", args)
152 }
153
154 # nocov end