Skip to content

Commit 9c8a15d

Browse files
committed
capture bin argument fixup in function
1 parent e57d6b8 commit 9c8a15d

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

R/bin.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,42 @@ bin_out <- function(count = integer(0), x = numeric(0), width = numeric(0),
186186
.size = length(count)
187187
)
188188
}
189+
190+
fix_bin_params = function(params, fun, version) {
191+
192+
if (!is.null(params$drop)) {
193+
args <- paste0(fun, c("(drop)", "(pad)"))
194+
deprecate_warn0(version, args[1], args[2])
195+
params$drop <- NULL
196+
}
197+
198+
if (!is.null(params$origin)) {
199+
args <- paste0(fun, c("(origin)", "(boundary)"))
200+
deprecate_warn0(version, args[1], args[2])
201+
params$boudnary <- params$origin
202+
params$origin <- NULL
203+
}
204+
205+
if (!is.null(params$right)) {
206+
args <- paste0(fun, c("(right)", "(closed)"))
207+
deprecate_warn0(version, args[1], args[2])
208+
params$closed <- if (isTRUE(params$right)) "right" else "left"
209+
params$right <- NULL
210+
}
211+
212+
if (!is.null(params$boundary) && !is.null(params$center)) {
213+
cli::cli_abort(
214+
"Only one of {.arg boundary} and {.arg center} may be specified \\
215+
in {.fn {fun}}."
216+
)
217+
}
218+
219+
if (is.null(params$breaks %||% params$binwidth %||% params$bins)) {
220+
cli::cli_inform(
221+
"{.fn {fun}} using {.code bins = 30}. Pick better value {.arg binwidth}."
222+
)
223+
params$bins <- 30
224+
}
225+
226+
params
227+
}

R/stat-bin.R

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -118,29 +118,7 @@ StatBin <- ggproto("StatBin", Stat,
118118
))
119119
}
120120

121-
if (!is.null(params$drop)) {
122-
deprecate_warn0("2.1.0", "stat_bin(drop)", "stat_bin(pad)")
123-
params$drop <- NULL
124-
}
125-
if (!is.null(params$origin)) {
126-
deprecate_warn0("2.1.0", "stat_bin(origin)", "stat_bin(boundary)")
127-
params$boundary <- params$origin
128-
params$origin <- NULL
129-
}
130-
if (!is.null(params$right)) {
131-
deprecate_warn0("2.1.0", "stat_bin(right)", "stat_bin(closed)")
132-
params$closed <- if (params$right) "right" else "left"
133-
params$right <- NULL
134-
}
135-
if (!is.null(params$boundary) && !is.null(params$center)) {
136-
cli::cli_abort("Only one of {.arg boundary} and {.arg center} may be specified in {.fn {snake_class(self)}}.")
137-
}
138-
139-
if (is.null(params$breaks) && is.null(params$binwidth) && is.null(params$bins)) {
140-
cli::cli_inform("{.fn {snake_class(self)}} using {.code bins = 30}. Pick better value with {.arg binwidth}.")
141-
params$bins <- 30
142-
}
143-
121+
params <- fix_bin_params(params, fun = snake_class(self), version = "2.1.0")
144122
params
145123
},
146124

tests/testthat/_snaps/stat-bin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
Error in `stat_bin()`:
6262
! Problem while computing stat.
6363
i Error occurred in the 1st layer.
64-
Caused by error in `setup_params()`:
64+
Caused by error in `fix_bin_params()`:
6565
! Only one of `boundary` and `center` may be specified in `stat_bin()`.
6666

6767
# bin errors at high bin counts

0 commit comments

Comments
 (0)