@@ -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}
0 commit comments