Skip to content

Commit dd6bb00

Browse files
committed
Use standalone purrr file
1 parent 5430f3d commit dd6bb00

File tree

11 files changed

+264
-32
lines changed

11 files changed

+264
-32
lines changed

R/check-devtools.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ check_dev_versions <- function(pkg = ".") {
2323
pkg <- as.package(pkg)
2424

2525
dep_list <- pkg[tolower(remotes::standardise_dep(TRUE))]
26-
deps <- do.call("rbind", unname(compact(lapply(dep_list, parse_deps))))
26+
deps <- do.call("rbind", unname(compact(map(dep_list, parse_deps))))
2727
deps <- deps[!is.na(deps$version), , drop = FALSE]
2828

29-
parsed <- lapply(deps$version, function(x) unlist(numeric_version(x)))
29+
parsed <- map(deps$version, function(x) unlist(numeric_version(x)))
3030

3131
lens <- lengths(parsed)
32-
last_ver <- vapply(parsed, function(x) x[[length(x)]], integer(1))
32+
last_ver <- map_int(parsed, function(x) x[[length(x)]])
3333

3434
is_dev <- lens == 4 & last_ver >= 9000
3535

@@ -68,7 +68,7 @@ check_vignette_titles <- function(pkg = ".") {
6868
any(grepl("Vignette Title", h))
6969
}
7070
v <- stats::setNames(vigns$docs, path_file(vigns$docs))
71-
has_vt <- vapply(v, has_vignette_title, logical(1), n = 30)
71+
has_vt <- map_lgl(v, has_vignette_title, n = 30)
7272

7373
check_status(
7474
!any(has_vt),

R/check-doc.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,18 @@ check_doc_fields <- function(pkg = ".", fields = c("value", "examples")) {
8484

8585
paths <- dir_ls(path(pkg$path, "man"), regexp = "\\.Rd$")
8686
names(paths) <- path_rel(paths, pkg$path)
87-
rd <- lapply(paths, tools::parse_Rd, permissive = TRUE)
88-
rd_tags <- lapply(rd, \(x) unlist(lapply(x, attr, "Rd_tag")))
87+
rd <- map(paths, tools::parse_Rd, permissive = TRUE)
88+
rd_tags <- map(rd, \(x) unlist(map(x, attr, "Rd_tag")))
8989

9090
has_tag <- function(tags, this) {
9191
any(paste0("\\", this) %in% tags)
9292
}
9393

94-
has_usage <- vapply(rd_tags, has_tag, logical(1), this = "usage")
94+
has_usage <- map_lgl(rd_tags, has_tag, this = "usage")
9595
rd_tags <- rd_tags[has_usage]
9696

97-
results <- lapply(fields, function(field) {
98-
missing <- !vapply(rd_tags, has_tag, logical(1), this = field)
97+
results <- map(fields, function(field) {
98+
missing <- !map_lgl(rd_tags, has_tag, this = field)
9999
names(rd_tags)[missing]
100100
})
101101

R/check-mac.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ check_mac <- function(
107107
)
108108

109109
if (length(dep_built_paths) > 0) {
110-
uploads <- lapply(dep_built_paths, httr::upload_file)
110+
uploads <- map(dep_built_paths, httr::upload_file)
111111
names(uploads) <- rep("depfiles", length(uploads))
112112
body <- append(body, uploads)
113113
}

R/check-win.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ check_win <- function(
137137
"/",
138138
path_file(built_path)
139139
)
140-
lapply(url, upload_ftp, file = built_path)
140+
walk(url, upload_ftp, file = built_path)
141141

142142
if (!quiet) {
143143
time <- strftime(Sys.time() + 30 * 60, "%I:%M %p")
@@ -192,7 +192,7 @@ change_maintainer_email <- function(path, email, call = parent.frame()) {
192192
if (!is.list(roles)) {
193193
roles <- list(roles)
194194
}
195-
is_maintainer <- vapply(roles, function(r) all("cre" %in% r), logical(1))
195+
is_maintainer <- map_lgl(roles, function(r) all("cre" %in% r))
196196
aut[is_maintainer]$email <- email
197197
desc$set_authors(aut)
198198

R/dev-mode.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ is_library <- function(path) {
8888
dirs <- dir_ls(path, type = "directory")
8989

9090
has_pkg_dir <- function(path) length(dir_ls(path, regexp = "Meta")) > 0
91-
help_dirs <- vapply(dirs, has_pkg_dir, logical(1))
91+
help_dirs <- map_lgl(dirs, has_pkg_dir)
9292

9393
all(help_dirs)
9494
}

R/import-standalone-purrr.R

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
# Standalone file: do not edit by hand
2+
# Source: https://github.com/r-lib/rlang/blob/HEAD/R/standalone-purrr.R
3+
# Generated by: usethis::use_standalone("r-lib/rlang", "purrr")
4+
# ----------------------------------------------------------------------
5+
#
6+
# ---
7+
# repo: r-lib/rlang
8+
# file: standalone-purrr.R
9+
# last-updated: 2023-02-23
10+
# license: https://unlicense.org
11+
# imports: rlang
12+
# ---
13+
#
14+
# This file provides a minimal shim to provide a purrr-like API on top of
15+
# base R functions. They are not drop-in replacements but allow a similar style
16+
# of programming.
17+
#
18+
# ## Changelog
19+
#
20+
# 2023-02-23:
21+
# * Added `list_c()`
22+
#
23+
# 2022-06-07:
24+
# * `transpose()` is now more consistent with purrr when inner names
25+
# are not congruent (#1346).
26+
#
27+
# 2021-12-15:
28+
# * `transpose()` now supports empty lists.
29+
#
30+
# 2021-05-21:
31+
# * Fixed "object `x` not found" error in `imap()` (@mgirlich)
32+
#
33+
# 2020-04-14:
34+
# * Removed `pluck*()` functions
35+
# * Removed `*_cpl()` functions
36+
# * Used `as_function()` to allow use of `~`
37+
# * Used `.` prefix for helpers
38+
#
39+
# nocov start
40+
41+
map <- function(.x, .f, ...) {
42+
.f <- as_function(.f, env = global_env())
43+
lapply(.x, .f, ...)
44+
}
45+
walk <- function(.x, .f, ...) {
46+
map(.x, .f, ...)
47+
invisible(.x)
48+
}
49+
50+
map_lgl <- function(.x, .f, ...) {
51+
.rlang_purrr_map_mold(.x, .f, logical(1), ...)
52+
}
53+
map_int <- function(.x, .f, ...) {
54+
.rlang_purrr_map_mold(.x, .f, integer(1), ...)
55+
}
56+
map_dbl <- function(.x, .f, ...) {
57+
.rlang_purrr_map_mold(.x, .f, double(1), ...)
58+
}
59+
map_chr <- function(.x, .f, ...) {
60+
.rlang_purrr_map_mold(.x, .f, character(1), ...)
61+
}
62+
.rlang_purrr_map_mold <- function(.x, .f, .mold, ...) {
63+
.f <- as_function(.f, env = global_env())
64+
out <- vapply(.x, .f, .mold, ..., USE.NAMES = FALSE)
65+
names(out) <- names(.x)
66+
out
67+
}
68+
69+
map2 <- function(.x, .y, .f, ...) {
70+
.f <- as_function(.f, env = global_env())
71+
out <- mapply(.f, .x, .y, MoreArgs = list(...), SIMPLIFY = FALSE)
72+
if (length(out) == length(.x)) {
73+
set_names(out, names(.x))
74+
} else {
75+
set_names(out, NULL)
76+
}
77+
}
78+
map2_lgl <- function(.x, .y, .f, ...) {
79+
as.vector(map2(.x, .y, .f, ...), "logical")
80+
}
81+
map2_int <- function(.x, .y, .f, ...) {
82+
as.vector(map2(.x, .y, .f, ...), "integer")
83+
}
84+
map2_dbl <- function(.x, .y, .f, ...) {
85+
as.vector(map2(.x, .y, .f, ...), "double")
86+
}
87+
map2_chr <- function(.x, .y, .f, ...) {
88+
as.vector(map2(.x, .y, .f, ...), "character")
89+
}
90+
imap <- function(.x, .f, ...) {
91+
map2(.x, names(.x) %||% seq_along(.x), .f, ...)
92+
}
93+
94+
pmap <- function(.l, .f, ...) {
95+
.f <- as.function(.f)
96+
args <- .rlang_purrr_args_recycle(.l)
97+
do.call(
98+
"mapply",
99+
c(
100+
FUN = list(quote(.f)),
101+
args,
102+
MoreArgs = quote(list(...)),
103+
SIMPLIFY = FALSE,
104+
USE.NAMES = FALSE
105+
)
106+
)
107+
}
108+
.rlang_purrr_args_recycle <- function(args) {
109+
lengths <- map_int(args, length)
110+
n <- max(lengths)
111+
112+
stopifnot(all(lengths == 1L | lengths == n))
113+
to_recycle <- lengths == 1L
114+
args[to_recycle] <- map(args[to_recycle], function(x) rep.int(x, n))
115+
116+
args
117+
}
118+
119+
keep <- function(.x, .f, ...) {
120+
.x[.rlang_purrr_probe(.x, .f, ...)]
121+
}
122+
discard <- function(.x, .p, ...) {
123+
sel <- .rlang_purrr_probe(.x, .p, ...)
124+
.x[is.na(sel) | !sel]
125+
}
126+
map_if <- function(.x, .p, .f, ...) {
127+
matches <- .rlang_purrr_probe(.x, .p)
128+
.x[matches] <- map(.x[matches], .f, ...)
129+
.x
130+
}
131+
.rlang_purrr_probe <- function(.x, .p, ...) {
132+
if (is_logical(.p)) {
133+
stopifnot(length(.p) == length(.x))
134+
.p
135+
} else {
136+
.p <- as_function(.p, env = global_env())
137+
map_lgl(.x, .p, ...)
138+
}
139+
}
140+
141+
compact <- function(.x) {
142+
.x[as.logical(lengths(.x))]
143+
}
144+
145+
transpose <- function(.l) {
146+
if (!length(.l)) {
147+
return(.l)
148+
}
149+
150+
inner_names <- names(.l[[1]])
151+
152+
if (is.null(inner_names)) {
153+
fields <- seq_along(.l[[1]])
154+
} else {
155+
fields <- set_names(inner_names)
156+
.l <- map(.l, function(x) {
157+
if (is.null(names(x))) {
158+
set_names(x, inner_names)
159+
} else {
160+
x
161+
}
162+
})
163+
}
164+
165+
# This way missing fields are subsetted as `NULL` instead of causing
166+
# an error
167+
.l <- map(.l, as.list)
168+
169+
map(fields, function(i) {
170+
map(.l, .subset2, i)
171+
})
172+
}
173+
174+
every <- function(.x, .p, ...) {
175+
.p <- as_function(.p, env = global_env())
176+
177+
for (i in seq_along(.x)) {
178+
if (!rlang::is_true(.p(.x[[i]], ...))) return(FALSE)
179+
}
180+
TRUE
181+
}
182+
some <- function(.x, .p, ...) {
183+
.p <- as_function(.p, env = global_env())
184+
185+
for (i in seq_along(.x)) {
186+
if (rlang::is_true(.p(.x[[i]], ...))) return(TRUE)
187+
}
188+
FALSE
189+
}
190+
negate <- function(.p) {
191+
.p <- as_function(.p, env = global_env())
192+
function(...) !.p(...)
193+
}
194+
195+
reduce <- function(.x, .f, ..., .init) {
196+
f <- function(x, y) .f(x, y, ...)
197+
Reduce(f, .x, init = .init)
198+
}
199+
reduce_right <- function(.x, .f, ..., .init) {
200+
f <- function(x, y) .f(y, x, ...)
201+
Reduce(f, .x, init = .init, right = TRUE)
202+
}
203+
accumulate <- function(.x, .f, ..., .init) {
204+
f <- function(x, y) .f(x, y, ...)
205+
Reduce(f, .x, init = .init, accumulate = TRUE)
206+
}
207+
accumulate_right <- function(.x, .f, ..., .init) {
208+
f <- function(x, y) .f(y, x, ...)
209+
Reduce(f, .x, init = .init, right = TRUE, accumulate = TRUE)
210+
}
211+
212+
detect <- function(.x, .f, ..., .right = FALSE, .p = is_true) {
213+
.p <- as_function(.p, env = global_env())
214+
.f <- as_function(.f, env = global_env())
215+
216+
for (i in .rlang_purrr_index(.x, .right)) {
217+
if (.p(.f(.x[[i]], ...))) {
218+
return(.x[[i]])
219+
}
220+
}
221+
NULL
222+
}
223+
detect_index <- function(.x, .f, ..., .right = FALSE, .p = is_true) {
224+
.p <- as_function(.p, env = global_env())
225+
.f <- as_function(.f, env = global_env())
226+
227+
for (i in .rlang_purrr_index(.x, .right)) {
228+
if (.p(.f(.x[[i]], ...))) {
229+
return(i)
230+
}
231+
}
232+
0L
233+
}
234+
.rlang_purrr_index <- function(x, right = FALSE) {
235+
idx <- seq_along(x)
236+
if (right) {
237+
idx <- rev(idx)
238+
}
239+
idx
240+
}
241+
242+
list_c <- function(x) {
243+
inject(c(!!!x))
244+
}
245+
246+
# nocov end

R/run-examples.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ run_examples <- function(
102102
load_all(pkg$path, reset = TRUE, export_all = FALSE, helpers = FALSE)
103103
on.exit(load_all(pkg$path, reset = TRUE))
104104

105-
lapply(
105+
walk(
106106
files,
107107
pkgload::run_example,
108108
run_donttest = run_donttest,

R/session-info.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ loaded_packages <- function() {
2020
#' @export
2121
#' @keywords internal
2222
dev_packages <- function() {
23-
packages <- vapply(
24-
loadedNamespaces(),
25-
function(x) !is.null(pkgload::dev_meta(x)),
26-
logical(1)
27-
)
23+
packages <- map_lgl(loadedNamespaces(), \(x) !is.null(pkgload::dev_meta(x)))
2824

2925
names(packages)[packages]
3026
}

R/sitrep.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ check_for_rstudio_updates <- function(
5353
return()
5454
}
5555

56-
nms <- vcapply(result, `[[`, 1)
57-
values <- vcapply(result, function(x) utils::URLdecode(x[[2]]))
56+
nms <- map_chr(result, `[[`, 1)
57+
values <- map_chr(result, function(x) utils::URLdecode(x[[2]]))
5858

5959
result <- stats::setNames(values, nms)
6060

R/utils.R

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
compact <- function(x) {
2-
x[lengths(x) > 0]
3-
}
4-
5-
"%||%" <- function(a, b) if (!is.null(a)) a else b
6-
71
"%:::%" <- function(p, f) {
82
get(f, envir = asNamespace(p))
93
}
@@ -26,10 +20,6 @@ is_attached <- function(pkg = ".") {
2620
!is.null(pkgload::pkg_env(pkg$package))
2721
}
2822

29-
vcapply <- function(x, FUN, ...) {
30-
vapply(x, FUN, FUN.VALUE = character(1), ...)
31-
}
32-
3323
release_bullets <- function() {
3424
c(
3525
'`usethis::use_latest_dependencies(TRUE, "CRAN")`',

0 commit comments

Comments
 (0)