Skip to content

Commit 4daadf7

Browse files
authored
Peformance: vec_unique_count (#5158)
* Use `is_unique()` * Use `vec_unique_count()`
1 parent 8100859 commit 4daadf7

14 files changed

+21
-18
lines changed

R/axis-secondary.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
193193
full_range <- self$transform_range(old_range)
194194

195195
# Test for monotonicity
196-
if (length(unique0(sign(diff(full_range)))) != 1)
196+
if (!is_unique(sign(diff(full_range))))
197197
cli::cli_abort("Transformation for secondary axes must be monotonic")
198198
},
199199

R/facet-.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ Facet <- ggproto("Facet", NULL,
119119
}
120120
},
121121
draw_back = function(data, layout, x_scales, y_scales, theme, params) {
122-
rep(list(zeroGrob()), length(unique0(layout$PANEL)))
122+
rep(list(zeroGrob()), vec_unique_count(layout$PANEL))
123123
},
124124
draw_front = function(data, layout, x_scales, y_scales, theme, params) {
125-
rep(list(zeroGrob()), length(unique0(layout$PANEL)))
125+
rep(list(zeroGrob()), vec_unique_count(layout$PANEL))
126126
},
127127
draw_panels = function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, params) {
128128
cli::cli_abort("Not implemented")

R/guides-axis.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ warn_for_guide_position <- function(guide) {
440440
return()
441441
}
442442

443-
if (length(unique0(guide$key[[position_aes]][breaks_are_unique])) == 1) {
443+
if (is_unique(guide$key[[position_aes]][breaks_are_unique])) {
444444
cli::cli_warn(c(
445445
"Position guide is perpendicular to the intended axis",
446446
"i" = "Did you mean to specify a different guide {.arg position}?"

R/position-collide.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ collide <- function(data, width = NULL, name, strategy,
4848
intervals <- as.numeric(t(unique0(data[c("xmin", "xmax")])))
4949
intervals <- intervals[!is.na(intervals)]
5050

51-
if (length(unique0(intervals)) > 1 & any(diff(scale(intervals)) < -1e-6)) {
51+
if (vec_unique_count(intervals) > 1 & any(diff(scale(intervals)) < -1e-6)) {
5252
cli::cli_warn("{.fn {name}} requires non-overlapping {.field x} intervals")
5353
# This is where the algorithm from [L. Wilkinson. Dot plots.
5454
# The American Statistician, 1999.] should be used

R/position-dodge.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ PositionDodge <- ggproto("PositionDodge", Position,
144144
# Assumes that each set has the same horizontal position.
145145
pos_dodge <- function(df, width, n = NULL) {
146146
if (is.null(n)) {
147-
n <- length(unique0(df$group))
147+
n <- vec_unique_count(df$group)
148148
}
149149

150150
if (n == 1)

R/position-jitterdodge.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
5353
cli::cli_abort("{.fn position_jitterdodge} requires at least one aesthetic to dodge by")
5454
}
5555
ndodge <- lapply(data[dodgecols], levels) # returns NULL for numeric, i.e. non-dodge layers
56-
ndodge <- length(unique0(unlist(ndodge)))
56+
ndodge <- vec_unique_count(unlist(ndodge))
5757

5858
list(
5959
dodge.width = self$dodge.width,

R/stat-.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Stat <- ggproto("Stat",
143143

144144
# Then, check whether the rest of the columns have constant values (type 2)
145145
# or not (type 3).
146-
non_constant <- vapply(old, function(x) length(unique0(x)) > 1, logical(1L))
146+
non_constant <- vapply(old, vec_unique_count, integer(1)) > 1L
147147

148148
# Record the non-constant columns.
149149
non_constant_columns <<- c(non_constant_columns, names(old)[non_constant])

R/stat-align.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ StatAlign <- ggproto("StatAlign", Stat,
5656

5757
compute_group = function(data, scales, flipped_aes = NA, unique_loc = NULL, adjust = 0) {
5858
data <- flip_data(data, flipped_aes)
59-
if (length(unique(data$x)) == 1) {
59+
if (is_unique(data$x)) {
6060
# Not enough data to align
6161
return(new_data_frame())
6262
}

R/stat-boxplot.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ StatBoxplot <- ggproto("StatBoxplot", Stat,
108108
stats[c(1, 5)] <- range(c(stats[2:4], data$y[!outliers]), na.rm = TRUE)
109109
}
110110

111-
if (length(unique0(data$x)) > 1)
111+
if (vec_unique_count(data$x) > 1)
112112
width <- diff(range(data$x)) * 0.9
113113

114114
df <- data_frame0(!!!as.list(stats))

R/stat-smooth.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ StatSmooth <- ggproto("StatSmooth", Stat,
130130
xseq = NULL, level = 0.95, method.args = list(),
131131
na.rm = FALSE, flipped_aes = NA) {
132132
data <- flip_data(data, flipped_aes)
133-
if (length(unique0(data$x)) < 2) {
133+
if (vec_unique_count(data$x) < 2) {
134134
# Not enough data to perform fit
135135
return(data_frame0())
136136
}

0 commit comments

Comments
 (0)