Skip to content

Commit

Permalink
convert new stat transform functions to standard evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
corybrunson committed Nov 25, 2019
1 parent 2b0ee9f commit 4d59e12
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 37 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export(to_lodes)
export(to_lodes_form)
import(ggplot2)
import(tidyselect)
importFrom(rlang,.data)
7 changes: 5 additions & 2 deletions R/stat-alluvium.r
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#'

#' @import ggplot2
#' @importFrom rlang .data
#' @family alluvial stat layers
#' @seealso [ggplot2::layer()] for additional arguments and [geom_alluvium()],
#' [geom_lode()], and [geom_flow()] for the corresponding geoms.
Expand Down Expand Up @@ -380,9 +381,11 @@ cement_data <- function(data, key, id, fun) {
by_vars <- c(key, id, "binding")
data_agg <- dplyr::group_by(data[, c(by_vars, agg_vars)], .dots = by_vars)
data_agg <- if ("label" %in% agg_vars) {
dplyr::summarize(data_agg, y = sum(y, na.rm = TRUE), label = fun(label))
dplyr::summarize(data_agg,
y = sum(.data$y, na.rm = TRUE), label = fun(.data$label))
} else {
dplyr::summarize(data_agg, y = sum(y, na.rm = TRUE))
#dplyr::summarize(data_agg, y = sum(.data$y, na.rm = TRUE))
dplyr::summarize_at(data_agg, "y", sum, na.rm = TRUE)
}
data_agg <- dplyr::ungroup(data_agg)
# merge into `data`, ensuring that no `key`-`id` pairs are duplicated
Expand Down
44 changes: 9 additions & 35 deletions R/stat-utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -68,48 +68,22 @@ contiguate <- function(x) {
deposit_data <- function(data, decreasing, reverse, absolute) {
if (is.na(decreasing)) {
deposits <- unique(data[, c("x", "yneg", "stratum")])
deposits <- transform(deposits, deposit = order(order(
x, -yneg,
xtfrm(stratum) * (-1) ^ (yneg * absolute + reverse)
)))
deposits$deposit <- order(order(
deposits$x, -deposits$yneg,
xtfrm(deposits$stratum) * (-1) ^ (deposits$yneg * absolute + reverse)
))
} else {
deposits <- stats::aggregate(
x = data$y,
by = data[, c("x", "yneg", "stratum"), drop = FALSE],
FUN = sum
)
names(deposits)[ncol(deposits)] <- "y"
deposits <- transform(deposits, deposit = order(order(
x, -yneg,
xtfrm(y) * (-1) ^ (yneg * absolute + decreasing),
xtfrm(stratum) * (-1) ^ (yneg * absolute + reverse)
)))
deposits$y <- NULL
}
merge(data, deposits, all.x = TRUE, all.y = FALSE)
}

# define 'deposit' variable to rank strata outward from zero (pos then neg)
# -+- originally used to simplify 'ycum' calculation; deprecated -+-
deposit_data_abs <- function(data, decreasing, reverse, absolute) {
if (is.na(decreasing)) {
deposits <- unique(data[, c("x", "yneg", "stratum")])
deposits <- transform(deposits, deposit = order(order(
x, yneg,
xtfrm(stratum) * (-1) ^ (reverse + yneg * ! absolute)
)))
} else {
deposits <- stats::aggregate(
x = data$y,
by = data[, c("x", "yneg", "stratum"), drop = FALSE],
FUN = sum
)
names(deposits)[ncol(deposits)] <- "y"
deposits <- transform(deposits, deposit = order(order(
x, yneg,
xtfrm(y) * (-1) ^ (decreasing + yneg * ! absolute),
xtfrm(stratum) * (-1) ^ (reverse + yneg * ! absolute)
)))
deposits$deposit <- order(order(
deposits$x, -deposits$yneg,
xtfrm(deposits$y) * (-1) ^ (deposits$yneg * absolute + decreasing),
xtfrm(deposits$stratum) * (-1) ^ (deposits$yneg * absolute + reverse)
))
deposits$y <- NULL
}
merge(data, deposits, all.x = TRUE, all.y = FALSE)
Expand Down

0 comments on commit 4d59e12

Please sign in to comment.