Skip to content

Commit ff268a5

Browse files
authored
Fix ordering in plyr compats... (#3327)
1 parent 723223e commit ff268a5

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

R/compat-plyr.R

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,26 @@ rbind_dfs <- function(dfs) {
292292
allocated[new_columns] <- TRUE
293293
if (all(allocated)) break
294294
}
295+
is_date <- lapply(out, inherits, 'Date')
296+
is_time <- lapply(out, inherits, 'POSIXct')
295297
pos <- c(cumsum(nrows) - nrows + 1)
296298
for (i in seq_along(dfs)) {
297299
df <- dfs[[i]]
298300
rng <- seq(pos[i], length.out = nrows[i])
299301
for (col in names(df)) {
300-
if (inherits(df[[col]], 'factor')) {
302+
date_col <- inherits(df[[col]], 'Date')
303+
time_col <- inherits(df[[col]], 'POSIXct')
304+
if (is_date[[col]] && !date_col) {
305+
out[[col]][rng] <- as.Date(
306+
unclass(df[[col]]),
307+
origin = ggplot_global$date_origin
308+
)
309+
} else if (is_time[[col]] && !time_col) {
310+
out[[col]][rng] <- as.POSIXct(
311+
unclass(df[[col]]),
312+
origin = ggplot_global$time_origin
313+
)
314+
} else if (date_col || time_col || inherits(df[[col]], 'factor')) {
301315
out[[col]][rng] <- as.character(df[[col]])
302316
} else {
303317
out[[col]][rng] <- df[[col]]
@@ -307,7 +321,11 @@ rbind_dfs <- function(dfs) {
307321
for (col in names(col_levels)) {
308322
out[[col]] <- factor(out[[col]], levels = col_levels[[col]])
309323
}
310-
attributes(out) <- list(class = "data.frame", names = names(out), row.names = .set_row_names(total))
324+
attributes(out) <- list(
325+
class = "data.frame",
326+
names = names(out),
327+
row.names = .set_row_names(total)
328+
)
311329
out
312330
}
313331
#' Apply function to unique subsets of a data.frame
@@ -333,6 +351,7 @@ dapply <- function(df, by, fun, ..., drop = TRUE) {
333351
grouping_cols <- .subset(df, by)
334352
ids <- id(grouping_cols, drop = drop)
335353
group_rows <- split(seq_len(nrow(df)), ids)
354+
fallback_order <- unique(c(by, names(df)))
336355
rbind_dfs(lapply(seq_along(group_rows), function(i) {
337356
cur_data <- df_rows(df, group_rows[[i]])
338357
res <- fun(cur_data, ...)
@@ -341,6 +360,8 @@ dapply <- function(df, by, fun, ..., drop = TRUE) {
341360
vars <- lapply(setNames(by, by), function(col) .subset2(cur_data, col)[1])
342361
if (is.matrix(res)) res <- split_matrix(res)
343362
if (is.null(names(res))) names(res) <- paste0("V", seq_along(res))
344-
new_data_frame(modify_list(unclass(vars), unclass(res)))
363+
if (all(by %in% names(res))) return(new_data_frame(unclass(res)))
364+
res <- modify_list(unclass(vars), unclass(res))
365+
new_data_frame(res[intersect(c(fallback_order, names(res)), names(res))])
345366
}))
346367
}

R/zzz.r

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ pathGrob <- NULL
3535

3636
ggplot_global$theme_current <- theme_gray()
3737

38+
# Used by rbind_dfs
39+
date <- Sys.Date()
40+
ggplot_global$date_origin <- date - unclass(date)
41+
time <- Sys.time()
42+
ggplot_global$time_origin <- time - unclass(time)
43+
3844
# To avoid namespace clash with dplyr.
3945
# It seems surprising that this hack works
4046
if (requireNamespace("dplyr", quietly = TRUE)) {

tests/testthat/test-layer.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ test_that("if an aes is mapped to a function that returns NULL, it is removed",
6060
df <- data_frame(x = 1:10)
6161
null <- function(...) NULL
6262
p <- cdata(ggplot(df, aes(x, null())))
63-
expect_identical(names(p[[1]]), c("PANEL", "x", "group"))
63+
expect_identical(names(p[[1]]), c("x", "PANEL", "group"))
6464
})
6565

6666
# Data extraction ---------------------------------------------------------

0 commit comments

Comments
 (0)