Skip to content

Commit 89099c2

Browse files
committed
Convert stat$inform_defaults to stat$compute_defaults
1 parent 23e95e1 commit 89099c2

10 files changed

+43
-53
lines changed

R/layer.r

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ Layer <- ggproto("Layer", NULL,
105105
},
106106

107107

108-
calc_statistic = function(self, data, scales) {
108+
calc_statistic = function(self, data, scales, params) {
109109
if (empty(data))
110110
return(data.frame())
111111

112112
check_required_aesthetics(
113113
self$stat$required_aes,
114-
c(names(data), names(self$stat_params)),
114+
c(names(data), names(params)),
115115
snake_class(self$stat)
116116
)
117117

118-
args <- c(list(data = quote(data), scales = quote(scales)), self$stat_params)
118+
args <- c(list(data = quote(data), scales = quote(scales)), params)
119119
tryCatch(do.call(self$stat$calculate_groups, args), error = function(e) {
120120
warning("Computation failed in `", snake_class(self$stat), "()`:\n",
121121
e$message, call. = FALSE)
@@ -160,7 +160,9 @@ Layer <- ggproto("Layer", NULL,
160160

161161

162162
adjust_position = function(self, data) {
163+
if (empty(data)) return(data.frame())
163164
params <- self$position$compute_defaults(data)
165+
164166
plyr::ddply(data, "PANEL", function(data) {
165167
if (empty(data)) return(data.frame())
166168

R/panel.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ calculate_stats <- function(panel, data, layers) {
188188
d <- data[[i]]
189189
l <- layers[[i]]
190190

191-
l$stat$inform_defaults(d, l$stat_params)
191+
params <- l$stat$compute_defaults(d, l$stat_params)
192192

193193
plyr::ddply(d, "PANEL", function(panel_data) {
194194
scales <- panel_scales(panel, panel_data$PANEL[1])
195-
l$calc_statistic(panel_data, scales)
195+
l$calc_statistic(panel_data, scales, params)
196196
})
197197
})
198198
}

R/stat-.r

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#' \item \code{calculate}: Calculates a stat for a single group of data.
1414
#' \item \code{calculate_groups}: Calculates stat for all groups. The method
1515
#' typically calls \code{calculate} for each group.
16-
#' \item \code{inform_defaults(data, params)}: called once for each layer.
17-
#' Used to inform the user how important parameter choices are made
18-
#' by default.
16+
#' \item \code{compute_defaults(data, params)}: called once for each layer.
17+
#' Used to compute defaults that need to complete dataset, and to inform
18+
#' the user of important choices.
1919
#' \item \code{required_aes}: A character vector of aesthetics needed to
2020
#' render the geom.
2121
#' \item \code{default_aes}: A list (generated by \code{\link{aes}()} of
@@ -39,7 +39,8 @@ Stat <- ggproto("Stat",
3939
data
4040
},
4141

42-
inform_defaults = function(data, params) {
42+
compute_defaults = function(data, params) {
43+
params
4344
},
4445

4546
calculate_groups = function(self, data, scales, ...) {

R/stat-bin.r

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ stat_bin <- function(mapping = NULL, data = NULL, geom = "bar",
4848
#' @usage NULL
4949
#' @export
5050
StatBin <- ggproto("StatBin", Stat,
51-
inform_defaults = function(data, params) {
51+
compute_defaults = function(data, params) {
5252
if (!is.null(data$y) || !is.null(params$y)) {
5353
warning("stat_bin() ignores y aesthetic.", call. = FALSE)
5454
}
5555

5656
if (is.null(params$breaks) && is.null(params$binwidth) && is.null(params$bins)) {
5757
message("`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.")
5858
}
59+
60+
params
5961
},
6062

6163
calculate = function(self, data, scales, binwidth = NULL, bins = NULL,

R/stat-bindot.r

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
#' @usage NULL
44
#' @export
55
StatBindot <- ggproto("StatBindot", Stat,
6-
inform_defaults = function(data, params) {
6+
compute_defaults = function(data, params) {
77
if (is.null(params$breaks) && is.null(params$binwidth)) {
88
message("`stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.")
99
}
10+
params
1011
},
1112

1213
calculate_groups = function(self, data, na.rm = FALSE, binwidth = NULL,

R/stat-boxplot.r

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,18 @@ stat_boxplot <- function(mapping = NULL, data = NULL, geom = "boxplot",
4343
StatBoxplot <- ggproto("StatBoxplot", Stat,
4444
required_aes = c("x", "y"),
4545

46-
calculate_groups = function(self, data, na.rm = FALSE, width = NULL, ...)
47-
{
48-
data <- remove_missing(data, na.rm, c("x", "y", "weight"), name = "stat_boxplot",
49-
finite = TRUE)
50-
data$weight <- data$weight %||% 1
51-
width <- width %||% resolution(data$x) * 0.75
52-
53-
ggproto_parent(Stat, self)$calculate_groups(data, na.rm = na.rm,
54-
width = width, ...)
46+
compute_defaults = function(data, params) {
47+
params$width <- params$width %||% resolution(data$x) * 0.75
48+
params
5549
},
5650

5751
calculate = function(data, scales, width = NULL, na.rm = FALSE, coef = 1.5, ...) {
5852
qs <- c(0, 0.25, 0.5, 0.75, 1)
59-
if (length(unique(data$weight)) != 1) {
53+
54+
data <- remove_missing(data, na.rm, c("x", "y", "weight"), name = "stat_boxplot",
55+
finite = TRUE)
56+
57+
if (!is.null(data$weight)) {
6058
mod <- quantreg::rq(y ~ 1, weights = weight, data = data, tau = qs)
6159
stats <- as.numeric(stats::coef(mod))
6260
} else {

R/stat-smooth.r

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,45 +56,31 @@ stat_smooth <- function(mapping = NULL, data = NULL, geom = "smooth",
5656
#' @export
5757
StatSmooth <- ggproto("StatSmooth", Stat,
5858

59-
inform_defaults = function(data, params) {
59+
compute_defaults = function(data, params) {
60+
# Figure out what type of smoothing to do: loess for small datasets,
61+
# gam with a cubic regression basis for large data
62+
# This is based on the size of the _largest_ group.
6063
if (identical(params$method, "auto")) {
6164
message(
6265
'`geom_smooth()`: For groups with <1000 observations, using ',
6366
'`method = "loess"` with `span = 0.75`,\n otherwise ',
6467
'`method = "gam"` with `formula = y ~ s(x, bs = "cs")`.'
6568
)
66-
}
67-
},
68-
69-
calculate_groups = function(self, data, scales, method = "auto",
70-
formula = y ~ x, ...) {
71-
rows <- plyr::daply(data, "group", function(df) length(unique(df$x)))
7269

73-
if (all(rows == 1) && length(rows) > 1) {
74-
message("geom_smooth: Only one unique x value each group.",
75-
"Maybe you want aes(group = 1)?")
76-
return(data.frame())
77-
}
78-
79-
# Figure out what type of smoothing to do: loess for small datasets,
80-
# gam with a cubic regression basis for large data
81-
# This is based on the size of the _largest_ group.
82-
if (identical(method, "auto")) {
83-
groups <- plyr::count(data, "group")
70+
max_group <- max(table(data$group))
8471

85-
if (max(groups$freq) < 1000) {
86-
method <- "loess"
72+
if (max_group < 1000) {
73+
params$method <- "loess"
8774
} else {
88-
method <- "gam"
89-
formula <- y ~ s(x, bs = "cs")
75+
params$method <- "gam"
76+
params$formula <- y ~ s(x, bs = "cs")
9077
}
9178
}
92-
if (identical(method, "gam")) {
93-
method <- mgcv::gam
79+
if (identical(params$method, "gam")) {
80+
params$method <- mgcv::gam
9481
}
9582

96-
ggproto_parent(Stat, self)$calculate_groups(data, scales, method = method,
97-
formula = formula, ...)
83+
params
9884
},
9985

10086
calculate = function(data, scales, method = "auto", formula = y~x,

man/ggplot2-ggproto.Rd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ implement one or more of the following:
196196
\item \code{calculate}: Calculates a stat for a single group of data.
197197
\item \code{calculate_groups}: Calculates stat for all groups. The method
198198
typically calls \code{calculate} for each group.
199-
\item \code{inform_defaults(data, params)}: called once for each layer.
200-
Used to inform the user how important parameter choices are made
201-
by default.
199+
\item \code{compute_defaults(data, params)}: called once for each layer.
200+
Used to compute defaults that need to complete dataset, and to inform
201+
the user of important choices.
202202
\item \code{required_aes}: A character vector of aesthetics needed to
203203
render the geom.
204204
\item \code{default_aes}: A list (generated by \code{\link{aes}()} of

tests/testthat/test-stats-function.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test_stat_scale <- function(stat, scale) {
55
stat$data <- transform(stat$data, PANEL = 1)
66
dat <- stat$compute_aesthetics(stat$data, ggplot())
77
dat <- add_group(dat)
8-
stat$calc_statistic(dat, scale)
8+
stat$calc_statistic(dat, scale, stat$stat_params)
99
}
1010

1111
test_that("stat-function", {

tests/testthat/test-stats.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test_stat <- function(stat) {
1818
stat$data <- transform(stat$data, PANEL = 1)
1919
dat <- stat$compute_aesthetics(stat$data, ggplot())
2020
dat <- add_group(dat)
21-
stat$calc_statistic(dat, NULL)
21+
stat$calc_statistic(dat, NULL, stat$stat_params)
2222
}
2323

2424
context("stat-bin")
@@ -93,7 +93,7 @@ test_stat_scale <- function(stat, scale) {
9393
stat$data <- transform(stat$data, PANEL = 1)
9494
dat <- stat$compute_aesthetics(stat$data, ggplot())
9595
dat <- add_group(dat)
96-
stat$calc_statistic(dat, scale)
96+
stat$calc_statistic(dat, scale, stat$stat_params)
9797
}
9898

9999
context("stat-bin2d")

0 commit comments

Comments
 (0)