Skip to content

Commit

Permalink
refactor: Rename grouped_data S3 methods to grouped_df
Browse files Browse the repository at this point in the history
Closes #70
  • Loading branch information
nathaneastwood committed Mar 28, 2021
1 parent f192979 commit 6171974
Show file tree
Hide file tree
Showing 23 changed files with 58 additions and 54 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: poorman
Type: Package
Title: A Poor Man's Dependency Free Recreation of 'dplyr'
Version: 0.2.4.20
Version: 0.2.4.21
Authors@R: person("Nathan", "Eastwood", "", "[email protected]",
role = c("aut", "cre"))
Maintainer: Nathan Eastwood <[email protected]>
Expand Down
32 changes: 16 additions & 16 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ S3method(arrange,data.frame)
S3method(default_missing,data.frame)
S3method(default_missing,default)
S3method(distinct,data.frame)
S3method(distinct,grouped_data)
S3method(distinct,grouped_df)
S3method(filter,data.frame)
S3method(filter,grouped_data)
S3method(filter,grouped_df)
S3method(glimpse,data.frame)
S3method(glimpse,default)
S3method(group_by,data.frame)
S3method(group_by_drop_default,default)
S3method(group_by_drop_default,grouped_data)
S3method(group_by_drop_default,grouped_df)
S3method(group_data,data.frame)
S3method(group_data,grouped_data)
S3method(group_data,grouped_df)
S3method(mutate,data.frame)
S3method(mutate,grouped_data)
S3method(mutate,grouped_df)
S3method(nest_by,data.frame)
S3method(nest_by,grouped_data)
S3method(nest_by,grouped_df)
S3method(recode,character)
S3method(recode,factor)
S3method(recode,numeric)
Expand All @@ -27,25 +27,25 @@ S3method(rename_with,data.frame)
S3method(replace_na,data.frame)
S3method(replace_na,default)
S3method(slice,data.frame)
S3method(slice,grouped_data)
S3method(slice,grouped_df)
S3method(slice_head,data.frame)
S3method(slice_head,grouped_data)
S3method(slice_head,grouped_df)
S3method(slice_max,data.frame)
S3method(slice_max,grouped_data)
S3method(slice_max,grouped_df)
S3method(slice_min,data.frame)
S3method(slice_min,grouped_data)
S3method(slice_min,grouped_df)
S3method(slice_sample,data.frame)
S3method(slice_sample,grouped_data)
S3method(slice_sample,grouped_df)
S3method(slice_tail,data.frame)
S3method(slice_tail,grouped_data)
S3method(slice_tail,grouped_df)
S3method(summarise,data.frame)
S3method(summarise,grouped_data)
S3method(summarise,grouped_df)
S3method(summarize,data.frame)
S3method(summarize,grouped_data)
S3method(summarize,grouped_df)
S3method(transmute,data.frame)
S3method(transmute,grouped_data)
S3method(transmute,grouped_df)
S3method(ungroup,data.frame)
S3method(ungroup,grouped_data)
S3method(ungroup,grouped_df)
export("%>%")
export(across)
export(add_count)
Expand Down
2 changes: 1 addition & 1 deletion R/distinct.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ distinct.data.frame <- function(.data, ..., .keep_all = FALSE) {
}

#' @export
distinct.grouped_data <- function(.data, ..., .keep_all = FALSE) {
distinct.grouped_df <- function(.data, ..., .keep_all = FALSE) {
apply_grouped_function("distinct", .data, drop = TRUE, ..., .keep_all = .keep_all)
}
2 changes: 1 addition & 1 deletion R/filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ filter.data.frame <- function(.data, ..., .preserve = FALSE) {
}

#' @export
filter.grouped_data <- function(.data, ..., .preserve = FALSE) {
filter.grouped_df <- function(.data, ..., .preserve = FALSE) {
rows <- rownames(.data)
res <- apply_grouped_function("filter", .data, drop = TRUE, ...)
res <- res[rows[rows %in% rownames(res)], ]
Expand Down
6 changes: 3 additions & 3 deletions R/group_by.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ group_by.data.frame <- function(.data, ..., .add = FALSE, .drop = group_by_drop_
vars <- dotdotdot(..., .impute_names = TRUE)
if (all(vapply(vars, is.null, FALSE))) {
res <- groups_set(.data, NULL)
class(res) <- class(res)[!(class(res) %in% "grouped_data")]
class(res) <- class(res)[!(class(res) %in% "grouped_df")]
return(res)
}
new_cols <- add_group_columns(.data, vars)
Expand All @@ -49,7 +49,7 @@ group_by.data.frame <- function(.data, ..., .add = FALSE, .drop = group_by_drop_
if (any(unknown)) stop("Invalid groups: ", groups[unknown])
if (length(groups) > 0L) {
res <- groups_set(res, groups, .drop)
class(res) <- union("grouped_data", class(res))
class(res) <- union("grouped_df", class(res))
}
res
}
Expand Down Expand Up @@ -82,7 +82,7 @@ group_by_drop_default.default <- function(.tbl) {
}

#' @export
group_by_drop_default.grouped_data <- function(.tbl) {
group_by_drop_default.grouped_df <- function(.tbl) {
tryCatch({
!identical(attr(group_data(.tbl), ".drop"), FALSE)
}, error = function(e) {
Expand Down
2 changes: 1 addition & 1 deletion R/group_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ group_data.data.frame <- function(.data) {
}

#' @export
group_data.grouped_data <- function(.data) {
group_data.grouped_df <- function(.data) {
attr(.data, "groups")
}

Expand Down
6 changes: 5 additions & 1 deletion R/group_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ apply_grouped_function <- function(fn, .data, drop = FALSE, ...) {
grouped <- split_into_groups(.data, groups, drop)
res <- do.call(rbind, unname(lapply(grouped, fn, ...)))
if (any(groups %in% colnames(res))) {
class(res) <- c("grouped_data", class(res))
class(res) <- c("grouped_df", class(res))
res <- groups_set(res, groups[groups %in% colnames(res)])
}
res
Expand Down Expand Up @@ -84,3 +84,7 @@ calculate_groups <- function(data, groups, drop = group_by_drop_default(data)) {
rownames(unique_groups) <- NULL
structure(unique_groups, .drop = drop)
}

is.grouped_df <- function(x) {
inherits(x, "grouped_df")
}
2 changes: 1 addition & 1 deletion R/mutate.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mutate.data.frame <- function(
}

#' @export
mutate.grouped_data <- function(.data, ...) {
mutate.grouped_df <- function(.data, ...) {
context$group_env <- parent.frame(n = 1)
on.exit(rm(list = c("group_env"), envir = context), add = TRUE)
rows <- rownames(.data)
Expand Down
2 changes: 1 addition & 1 deletion R/nest_by.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ nest_by.data.frame <- function(.data, ..., .key = "data", .keep = FALSE) {
}

#' @export
nest_by.grouped_data <- function(.data, ..., .key = "data", .keep = FALSE) {
nest_by.grouped_df <- function(.data, ..., .key = "data", .keep = FALSE) {
if (!missing(...)) {
stop("Can't re-group while nesting. Either `ungroup()` first or don't supply arguments to `nest_by()`")
}
Expand Down
4 changes: 2 additions & 2 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#'
#' A print method for grouped `data.frame`s. Uses the standard `print.data.frame()` method but also reports the groups.
#'
#' @param x An object of class `grouped_data`.
#' @param x An object of class `grouped_df`.
#' @param ... Additional arguments to [print()].
#' @inheritParams base::print.data.frame
#'
#' @examples
#' mtcars %>% group_by(cyl, am) %>% print()
#'
#' @noRd
print.grouped_data <- function(x, ..., digits = NULL, quote = FALSE, right = TRUE, row.names = TRUE, max = NULL) {
print.grouped_df <- function(x, ..., digits = NULL, quote = FALSE, right = TRUE, row.names = TRUE, max = NULL) {
class(x) <- "data.frame"
print(x, ..., digits = digits, quote = quote, right = right, row.names = row.names, max = max)
cat("\nGroups: ", paste(group_vars(x), collapse = ", "), "\n\n")
Expand Down
4 changes: 2 additions & 2 deletions R/reconstruct.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ reconstruct_attrs.data.frame <- function(data, template) {
data
}

reconstruct_attrs.grouped_data <- function(data, template) {
reconstruct_attrs.grouped_df <- function(data, template) {
group_vars <- intersect(group_vars(template), colnames(data))
structure(
groups_set(data, group_vars, drop = group_by_drop_default(template)),
class = c("grouped_data", "data.frame")
class = c("grouped_df", "data.frame")
)
}

Expand Down
2 changes: 1 addition & 1 deletion R/rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ rename_with <- function(.data, .fn, .cols = everything(), ...) {
#' @export
rename_with.data.frame <- function(.data, .fn, .cols = everything(), ...) {
if (!is.function(.fn)) stop("`", .fn, "` is not a valid function")
grouped <- inherits(.data, "grouped_data")
grouped <- is.grouped_df(.data)
if (grouped) grp_pos <- which(colnames(.data) %in% group_vars(.data))
col_pos <- eval_select_pos(.data = .data, .group_pos = TRUE, .cols = substitute(.cols))
cols <- colnames(.data)[col_pos]
Expand Down
12 changes: 6 additions & 6 deletions R/slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ slice.data.frame <- function(.data, ...) {
}

#' @export
slice.grouped_data <- function(.data, ...) {
slice.grouped_df <- function(.data, ...) {
apply_grouped_function("slice", .data, drop = TRUE, ...)
}

Expand All @@ -83,7 +83,7 @@ slice_head.data.frame <- function(.data, ..., n, prop) {
}

#' @export
slice_head.grouped_data <- function(.data, ..., n, prop) {
slice_head.grouped_df <- function(.data, ..., n, prop) {
apply_grouped_function("slice_head", .data, drop = TRUE, n = n, prop = prop, ...)
}

Expand All @@ -105,7 +105,7 @@ slice_tail.data.frame <- function(.data, ..., n, prop) {
}

#' @export
slice_tail.grouped_data <- function(.data, ..., n, prop) {
slice_tail.grouped_df <- function(.data, ..., n, prop) {
apply_grouped_function("slice_tail", .data, drop = TRUE, n = n, prop = prop, ...)
}

Expand Down Expand Up @@ -142,7 +142,7 @@ slice_min.data.frame <- function(.data, order_by, ..., n, prop, with_ties = TRUE
}

#' @export
slice_min.grouped_data <- function(.data, order_by, ..., n, prop, with_ties = TRUE) {
slice_min.grouped_df <- function(.data, order_by, ..., n, prop, with_ties = TRUE) {
eval_env$env <- environment()
on.exit(rm(list = "env", envir = eval_env), add = TRUE)
apply_grouped_function(
Expand Down Expand Up @@ -179,7 +179,7 @@ slice_max.data.frame <- function(.data, order_by, ..., n, prop, with_ties = TRUE
}

#' @export
slice_max.grouped_data <- function(.data, order_by, ..., n, prop, with_ties = TRUE) {
slice_max.grouped_df <- function(.data, order_by, ..., n, prop, with_ties = TRUE) {
eval_env$env <- environment()
on.exit(rm(list = "env", envir = eval_env), add = TRUE)
apply_grouped_function(
Expand Down Expand Up @@ -212,7 +212,7 @@ slice_sample.data.frame <- function(.data, ..., n, prop, weight_by = NULL, repla
}

#' @export
slice_sample.grouped_data <- function(.data, ..., n, prop, weight_by = NULL, replace = FALSE) {
slice_sample.grouped_df <- function(.data, ..., n, prop, weight_by = NULL, replace = FALSE) {
eval_env$env <- environment()
on.exit(rm(list = "env", envir = eval_env), add = TRUE)
apply_grouped_function(
Expand Down
4 changes: 2 additions & 2 deletions R/summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ summarise.data.frame <- function(.data, ..., .groups = NULL) {
}

#' @export
summarise.grouped_data <- function(.data, ..., .groups = NULL) {
summarise.grouped_df <- function(.data, ..., .groups = NULL) {
if (!is.null(.groups)) {
.groups <- match.arg(arg = .groups, choices = c("drop", "drop_last", "keep"), several.ok = FALSE)
}
Expand Down Expand Up @@ -132,7 +132,7 @@ summarize <- summarise
#' @export
summarize.data.frame <- summarise.data.frame
#' @export
summarize.grouped_data <- summarise.grouped_data
summarize.grouped_df <- summarise.grouped_df

# -- Helpers -------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion R/transmute.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ transmute.data.frame <- function(.data, ...) {
}

#' @export
transmute.grouped_data <- function(.data, ...) {
transmute.grouped_df <- function(.data, ...) {
rows <- rownames(.data)
res <- apply_grouped_function("transmute", .data, drop = TRUE, ...)
res[rows, ]
Expand Down
4 changes: 2 additions & 2 deletions R/ungroup.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ ungroup.data.frame <- function(x, ...) {
x <- groups_set(x, groups[!(groups %in% rm_groups)])
if (length(attr(x, "groups")) == 0L) {
attr(x, "groups") <- NULL
class(x) <- class(x)[!(class(x) %in% "grouped_data")]
class(x) <- class(x)[!(class(x) %in% "grouped_df")]
}
x
}

#' @export
ungroup.grouped_data <- function(x, ...) {
ungroup.grouped_df <- function(x, ...) {
NextMethod("ungroup")
}
2 changes: 1 addition & 1 deletion R/unite.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ unite <- function(data, col, ..., sep = "_", remove = TRUE, na.rm = FALSE) {
}
if (isTRUE(remove)) {
to_rm <- setdiff(colnames(to_unite), col)
if (inherits(data, "grouped_data") && length(to_rm) > 0L) {
if (is.grouped_df(data) && length(to_rm) > 0L) {
rm_groups <- as_symbols(intersect(group_vars(data), to_rm))
data <- do.call(ungroup, squash(list(x = data, rm_groups)))
}
Expand Down
2 changes: 1 addition & 1 deletion inst/tinytest/test_across.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ expect_equal(
row.names = 1:2,
class = "data.frame",
.drop = TRUE
), row.names = 1:2, class = c("grouped_data", "data.frame")
), row.names = 1:2, class = c("grouped_df", "data.frame")
),
info = "across() usage can depend on the group id"
)
Expand Down
6 changes: 3 additions & 3 deletions inst/tinytest/test_arrange.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ expect_equal(arrange(df, y), df[3:1, ], info = "arrange handles complex columns"
df <- data.frame(g = c(2, 2, 1, 1), x = c(1, 3, 2, 4))
res <- df %>% group_by(g) %>% arrange(x)
expect_true(
inherits(res, "grouped_data"),
inherits(res, "grouped_df"),
info = "arrange keeps the grouping class"
)

Expand All @@ -69,7 +69,7 @@ expect_equal(
list(g = c(1, 2), .rows = list(c(1L, 3L), c(2L, 4L))), row.names = 1:2, class = "data.frame", .drop = TRUE
),
row.names = 4:1,
class = c("grouped_data", "data.frame")
class = c("grouped_df", "data.frame")
),
info = "grouped arrange ignores group_by groups"
)
Expand All @@ -78,7 +78,7 @@ expect_equal(
structure(
list(g = c(1, 1, 2, 2), x = c(1L, 3L, 2L, 4L)),
groups = structure(list(g = c(1, 2), .rows = list(1:2, 3:4)), row.names = 1:2, class = "data.frame", .drop = TRUE),
row.names = c(4L, 2L, 3L, 1L), class = c("grouped_data", "data.frame")
row.names = c(4L, 2L, 3L, 1L), class = c("grouped_df", "data.frame")
),
info = "grouped arrange ignores group, unless requested with .by_group"
)
2 changes: 1 addition & 1 deletion inst/tinytest/test_group_by.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ expect_equal(n_groups(res), 1L, info = "group_by(add = TRUE) sets .drop if the o
expect_true(group_by_drop_default(res), info = "group_by(add = TRUE) sets .drop if the origonal data was .drop = TRUE")

df <- data.frame(x = 1:2, y = 1:2) %>%
structure(class = c("grouped_data", "data.frame"))
structure(class = c("grouped_df", "data.frame"))
expect_true(group_by_drop_default(df), info = "group_by_drop_default() is forgiving about corrupt grouped df")

res <- data.frame(x = c("apple", NA, "banana"), y = 1:3, stringsAsFactors = FALSE) %>%
Expand Down
6 changes: 3 additions & 3 deletions inst/tinytest/test_rownames.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ res <- rowid_to_column(mtcars)
expect_false(has_rownames(res), info = "rownames are correctly removed")
expect_equal(
mtcars %>% group_by(am) %>% rowid_to_column() %>% class(),
c("grouped_data", "data.frame"),
c("grouped_df", "data.frame"),
info = "rowid_to_column() retains classes"
)
expect_equal(res$rowid, seq_len(nrow(mtcars)), info = "rowid is correctly set")
Expand All @@ -26,7 +26,7 @@ expect_false(has_rownames(res), info = "rownames have indeed been removed")
expect_equal(res$rowname, rownames(mtcars), info = "rownames have been correctly converted to a column")
expect_equal(
mtcars %>% group_by(am) %>% rownames_to_column() %>% class(),
c("grouped_data", "data.frame"),
c("grouped_df", "data.frame"),
info = "rownames_to_column() retains class"
)

Expand All @@ -38,7 +38,7 @@ expect_equal(res, mtcars)
expect_false("rowname" %in% colnames(res), info = "The old column is removed")
expect_equal(
mtcars %>% group_by(am) %>% rownames_to_column() %>% column_to_rownames("rowname") %>% class(),
c("grouped_data", "data.frame"),
c("grouped_df", "data.frame"),
info = "column_to_rownames() retains class"
)

Expand Down
4 changes: 2 additions & 2 deletions inst/tinytest/test_summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ expect_equal(
sumDisp = c(120.1, 287.5, 483, 335.2, 4291.4, 533.5, 215.4, 320, 145, 652)
),
row.names = c(NA, 10L),
class = c("grouped_data", "data.frame")
class = c("grouped_df", "data.frame")
),
info = "Test multiple groups and multiple summary functions"
)
Expand All @@ -81,7 +81,7 @@ expect_equal(
res <- mtcars %>% group_by(am) %>% summarise()
expect_equal(
class(res),
c("grouped_data", "data.frame"),
c("grouped_df", "data.frame"),
info = "empty grouped summarise() returns groups #1"
)
expect_equal(
Expand Down
Loading

0 comments on commit 6171974

Please sign in to comment.