Skip to content

Commit

Permalink
Special handling for zero row dfs in do. #625
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Sep 26, 2014
1 parent 69c2d3e commit d7e9522
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions R/do.r
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,37 @@ do.grouped_df <- function(.data, ..., env = parent.frame()) {

args <- dots(...)
named <- named_args(args)
labels <- attr(.data, "labels")

index <- attr(.data, "indices")
n <- length(index)
m <- length(args)

# Special case for zero-group input
if (n == 0) {
env <- new.env(parent = parent.frame())
env$. <- group_data

out <- vector("list", m)
for (j in seq_len(m)) {
out[[j]] <- eval(args[[j]], envir = env)
}

if (!named) {
return(label_output_dataframe(labels, list(out), groups(.data)))
} else {
return(label_output_list(labels, list(out), groups(.data)))
}
}

# Create new environment, inheriting from parent, with an active binding
# for . that resolves to the current subset. `_i` is found in environment
# of this function because of usual scoping rules.
index <- attr(.data, "indices")
env <- new.env(parent = parent.frame())
makeActiveBinding(".", function() {
group_data[index[[`_i`]] + 1L, , drop = FALSE]
}, env)

n <- length(index)
m <- length(args)

out <- replicate(m, vector("list", n), simplify = FALSE)
names(out) <- names(args)
p <- progress_estimated(n * m, min_time = 2)
Expand All @@ -145,7 +163,6 @@ do.grouped_df <- function(.data, ..., env = parent.frame()) {
}
}

labels <- attr(.data, "labels")
if (!named) {
label_output_dataframe(labels, out, groups(.data))
} else {
Expand Down

0 comments on commit d7e9522

Please sign in to comment.