-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmake_function_lists.R
269 lines (233 loc) · 8.36 KB
/
make_function_lists.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# ------------------------------------------------------------------------------
# Make data sets for function reference searches. Run this offline to refresh
# data objects.
library(tidymodels)
library(glue)
library(utils)
library(revdepcheck)
library(fs)
library(pkgdown)
library(urlchecker)
library(stringr)
# ------------------------------------------------------------------------------
tidymodels_prefer()
theme_set(theme_bw())
options(pillar.advice = FALSE, pillar.min_title_chars = Inf)
# ------------------------------------------------------------------------------
# Use the pkgdown package to parse the source files and put them into a usable format
# TODO find a better way to figure out how to find the true "check_" recipe operations
# from just the source files
get_pkg_info <- function(pkg, pth = tempdir(), keep_internal = FALSE, pattern = NULL) {
src_file <-
download.packages(pkg,
destdir = pth,
repos = "https://cran.rstudio.com/",
quiet = TRUE)
if (nrow(src_file) != length(pkg)) {
return(NULL)
rlang::warn(glue::glue("package {pkg} was not downloaded"))
}
pkg_path <- fs::path(pth, pkg)
on.exit(fs::dir_delete(pkg_path))
untar_res <- purrr::map_int(src_file[, 2], untar, exdir = pth)
fs::file_delete(src_file[, 2])
if (any(untar_res != 0)) {
rlang::abort(glue::glue("package {pkg} did not unpack correctly"))
}
pkg_info <- pkgdown::as_pkgdown(pkg_path)
res <- pkg_info$topics
if (!keep_internal) {
res <- dplyr::filter(res, !internal)
}
res <-
res %>%
dplyr::select(file_out, functions = alias, title) %>%
tidyr::unnest(functions) %>%
mutate(package = pkg, all_urls = list(pkg_info$desc$get_urls())) %>%
relocate(package, all_urls)
if (!is.null(pattern)) {
res <- dplyr::filter(res, grepl(pattern, functions))
}
res
}
# See if any of the urls appear to correspond to the _standard_ pkgdown structure.
# Is so, link to the specific pkgdown html package, otherwise link to the first
# url or, if there are none listed, the canonical CRAN page link.
# We use an internal function in urlchecker to essentially ping the potential url
sort_out_urls <- function(x) {
test_urls <-
x %>%
group_by(package) %>%
slice(1) %>%
ungroup() %>%
unnest(all_urls) %>%
mutate(
URL = map_chr(all_urls, ~ glue("{.x[[1]]}/reference/index.html")),
URL = gsub("//", "/", URL, fixed = TRUE)
) %>%
select(URL, Parent = functions, package, all_urls)
url_check_fails <-
urlchecker:::tools$check_url_db(test_urls) %>%
dplyr::select(URL)
pkgdown_urls <-
test_urls %>%
anti_join(url_check_fails, by = "URL") %>%
select(package, pkgdown_url = all_urls) %>%
group_by(package) %>%
slice(1) %>%
ungroup()
x %>%
left_join(pkgdown_urls, by = "package") %>%
mutate(
first_url = map_chr(all_urls, ~ .x[1]),
first_url = ifelse(is.na(first_url),
glue("https://cran.r-project.org/package={package}"),
first_url),
base_url = ifelse(is.na(pkgdown_url),
first_url,
pkgdown_url),
url = ifelse(!is.na(pkgdown_url),
glue("{pkgdown_url}/reference/{file_out}"),
base_url),
topic = glue("<a href='{url}' target='_blank'><tt>{functions}</tt></a>")
) %>%
dplyr::select(title, functions, topic, package) %>%
mutate(package = as.factor(package)) %>%
filter(!grepl("deprecated", tolower(title))) %>%
arrange(tolower(gsub("[[:punct:]]", "", title)))
}
# ------------------------------------------------------------------------------
broom_pkgs <- revdepcheck::cran_revdeps("broom", dependencies = c("Depends", "Imports"))
generics_pkgs <- revdepcheck::cran_revdeps("generics", dependencies = "Imports")
broom_pkgs <- sort(unique(c(broom_pkgs, generics_pkgs)))
excl <- c("hydrorecipes", "healthcareai")
broom_pkgs <- broom_pkgs[!(broom_pkgs %in% excl)]
broom_functions <-
map_dfr(
broom_pkgs,
get_pkg_info,
pattern = "(^tidy\\.)|(^glance\\.)|(^augment\\.)",
.progress = TRUE
) %>%
sort_out_urls() %>%
select(-functions)
save(
broom_functions,
file = "find/broom/broom_functions.RData",
compress = TRUE)
# ------------------------------------------------------------------------------
recipe_pkgs <- revdepcheck::cran_revdeps("recipes", dependencies = c("Depends", "Imports"))
recipe_pkgs <- c(recipe_pkgs, "recipes")
recipe_pkgs <- sort(unique(c(recipe_pkgs)))
excl <- c("hydrorecipes", "healthcareai")
recipe_pkgs <- recipe_pkgs[!(recipe_pkgs %in% excl)]
recipe_functions <-
map_dfr(
recipe_pkgs,
get_pkg_info,
pattern = "^step_",
.progress = TRUE
) %>%
sort_out_urls() %>%
select(-functions)
save(
recipe_functions,
file = "find/recipes/recipe_functions.RData",
compress = TRUE)
# ------------------------------------------------------------------------------
all_tm <-
c("agua", "applicable", "baguette", "brulee", "broom", "butcher",
"censored", "corrr", "dials", "discrim", "embed", "finetune",
"hardhat", "infer", "modeldata", "modeldb",
"modelenv", "multilevelmod", "parsnip", "plsmod", "poissonreg",
"probably", "recipes", "rsample", "rules", "shinymodels", "spatialsample",
"stacks", "textrecipes", "themis", "tidyclust", "tidymodels",
"tidyposterior", "tidypredict", "tune", "usemodels", "workflows",
"workflowsets", "yardstick")
tidymodels_functions <-
map_dfr(
all_tm,
get_pkg_info,
.progress = TRUE
) %>%
sort_out_urls() %>%
filter(!grepl("^\\.", functions)) %>%
select(-functions)
save(
tidymodels_functions,
file = "find/all/tidymodels_functions.RData",
compress = TRUE)
# ------------------------------------------------------------------------------
parsnip_pkgs <- revdepcheck::cran_revdeps("parsnip", dependencies = c("Depends", "Imports"))
parsnip_pkgs <- c(parsnip_pkgs, "parsnip")
# These ignore the tidymodels design principles and/or don't work with the broader ecosystem
# or we don't don't have any models in them
excl <- c("additive", "bayesian", "cuda.ml", "SSLR", "workflowsets", "workflows",
"tune", "tidymodels", "shinymodels", "stacks")
parsnip_pkgs <- parsnip_pkgs[!(parsnip_pkgs %in% excl)]
# Load them then get the model data base
loaded <- map_lgl(parsnip_pkgs, ~ suppressPackageStartupMessages(require(.x, character.only = TRUE)))
table(loaded)
# h2o overwrites soooo many functions; this may take a few minutes
conflicted::conflict_prefer_all("base", loser = "h2o", quiet = TRUE)
origin_pkg <- rlang::env_get_list(
env = parsnip::get_model_env(),
nms = ls(parsnip::get_model_env(), pattern = "_pkg")
) %>%
purrr::list_rbind(names_to = "model") %>%
mutate(pkg = map_chr(pkg, ~ {
pkg <- intersect(.x, parsnip_pkgs)
if (length(pkg) == 0) {
pkg <- "parsnip"
}
pkg
})) %>%
mutate(model = str_remove(model, "_pkgs$"))
model_list <-
map_dfr(get_from_env("models"), ~ get_from_env(.x) %>% mutate(model = .x)) %>%
mutate(
mode = factor(mode, levels = c("classification", "regression", "censored regression"))
) %>%
left_join(origin_pkg, by = c("engine", "mode", "model")) %>%
mutate(
functions = glue("details_{model}_{engine}")
)
parsnip_model_info <-
map_dfr(
parsnip_pkgs,
get_pkg_info,
keep_internal = TRUE,
.progress = TRUE
) %>%
sort_out_urls()
# Split model/engine combinations by whether they have "details" pages. Link to
# the details pages whenever possible.
has_details <-
parsnip_model_info %>%
filter(grepl("^details_", functions)) %>%
inner_join(model_list, by = "functions") %>%
mutate(topic = gsub("<tt>details_", "<tt>", topic))
no_details <-
model_list %>%
anti_join(has_details %>% select(model, engine), by = c("model", "engine")) %>%
mutate(functions = model) %>%
inner_join(parsnip_model_info, by = "functions")
parsnip_models <-
no_details %>%
select(title, model, engine, topic, mode, package = pkg) %>%
bind_rows(
has_details %>%
select(title, model, engine, topic, mode, package = pkg)
) %>%
mutate(
model = paste0("<code>", model, "</code>"),
engine = paste0("<code>", engine, "</code>"),
title = gsub("General Interface for ", "", title)
) %>%
arrange(model, engine) %>%
select(title, model, engine, topic, mode, package)
save(
parsnip_models,
file = "find/parsnip/parsnip_models.RData",
compress = TRUE
)