Skip to content

Commit 5d78b45

Browse files
authored
Use lengths() where appropriate (tidyverse#5210)
1 parent 5c79bc7 commit 5d78b45

File tree

11 files changed

+12
-12
lines changed

11 files changed

+12
-12
lines changed

R/annotation.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
5656
aesthetics <- c(position, list(...))
5757

5858
# Check that all aesthetic have compatible lengths
59-
lengths <- vapply(aesthetics, length, integer(1))
59+
lengths <- lengths(aesthetics)
6060
n <- unique0(lengths)
6161

6262
# if there is more than one unique length, ignore constants

R/compat-plyr.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ id <- function(.variables, drop = FALSE) {
9191
nrows <- nrow(.variables)
9292
.variables <- unclass(.variables)
9393
}
94-
lengths <- vapply(.variables, length, integer(1))
94+
lengths <- lengths(.variables)
9595
.variables <- .variables[lengths != 0]
9696
if (length(.variables) == 0) {
9797
n <- nrows %||% 0L

R/geom-.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ NULL
230230
.stroke <- 96 / 25.4
231231

232232
check_aesthetics <- function(x, n) {
233-
ns <- vapply(x, length, integer(1))
233+
ns <- lengths(x)
234234
good <- ns == 1L | ns == n
235235

236236
if (all(good)) {

R/guide-bins.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ guide_geom.bins <- function(guide, layers, default_mapping) {
240240

241241
if (length(matched) > 0) {
242242
# Filter out set aesthetics that can't be applied to the legend
243-
n <- vapply(layer$aes_params, length, integer(1))
243+
n <- lengths(layer$aes_params)
244244
params <- layer$aes_params[n == 1]
245245

246246
aesthetics <- layer$computed_mapping

R/guide-legend.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ guide_geom.legend <- function(guide, layers, default_mapping) {
259259

260260
if (length(matched) > 0) {
261261
# Filter out set aesthetics that can't be applied to the legend
262-
n <- vapply(layer$aes_params, length, integer(1))
262+
n <- lengths(layer$aes_params)
263263
params <- layer$aes_params[n == 1]
264264

265265
aesthetics <- layer$computed_mapping

R/layer.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ Layer <- ggproto("Layer", NULL,
297297
if (length(evaled) == 0) {
298298
n <- 0
299299
} else {
300-
aes_n <- vapply(evaled, length, integer(1))
300+
aes_n <- lengths(evaled)
301301
n <- if (min(aes_n) == 0) 0L else max(aes_n)
302302
}
303303
}

R/limits.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ expand_limits <- function(...) {
189189
data <- unlist(c(list(data[!data_dfs]), data[data_dfs]), recursive = FALSE)
190190

191191
# Repeat vectors up to max length and collect to data frame
192-
n_rows <- max(vapply(data, length, integer(1)))
192+
n_rows <- max(lengths(data))
193193
data <- lapply(data, rep, length.out = n_rows)
194194
data <- data_frame0(!!!data)
195195

R/scale-.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
747747
}
748748
if (is.list(labels)) {
749749
# Guard against list with empty elements
750-
labels[vapply(labels, length, integer(1)) == 0] <- ""
750+
labels[lengths(labels) == 0] <- ""
751751
# Make sure each element is scalar
752752
labels <- lapply(labels, `[`, 1)
753753

R/scale-hue.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ qualitative_pal <- function(type, h, c, l, h.start, direction) {
176176
if (!all(vapply(type_list, is.character, logical(1)))) {
177177
cli::cli_abort("{.arg type} must be a character vector or a list of character vectors")
178178
}
179-
type_lengths <- vapply(type_list, length, integer(1))
179+
type_lengths <- lengths(type_list)
180180
# If there are more levels than color codes default to hue_pal()
181181
if (max(type_lengths) < n) {
182182
return(scales::hue_pal(h, c, l, h.start, direction)(n))

R/stat-summary-2d.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ tapply_df <- function(x, index, fun, ..., drop = TRUE) {
134134
out$value <- unlist(lapply(grps, fun, ...))
135135

136136
if (drop) {
137-
n <- vapply(grps, length, integer(1))
137+
n <- lengths(grps)
138138
out <- out[n > 0, , drop = FALSE]
139139
}
140140

R/utilities.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ check_required_aesthetics <- function(required, present, name, call = caller_env
2727
if (is.null(required)) return()
2828

2929
required <- strsplit(required, "|", fixed = TRUE)
30-
if (any(vapply(required, length, integer(1)) > 1)) {
30+
if (any(lengths(required) > 1)) {
3131
required <- lapply(required, rep_len, 2)
3232
required <- list(
3333
vapply(required, `[`, character(1), 1),
@@ -37,7 +37,7 @@ check_required_aesthetics <- function(required, present, name, call = caller_env
3737
required <- list(unlist(required))
3838
}
3939
missing_aes <- lapply(required, setdiff, present)
40-
if (any(vapply(missing_aes, length, integer(1)) == 0)) return()
40+
if (any(lengths(missing_aes) == 0)) return()
4141
message <- "{.fn {name}} requires the following missing aesthetics: {.field {missing_aes[[1]]}}"
4242
if (length(missing_aes) > 1) {
4343
message <- paste0(message, " {.strong or} {.field {missing_aes[[2]]}}")

0 commit comments

Comments
 (0)