Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More generic functions #1101

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method(build_longer_spec,data.frame)
S3method(build_wider_spec,data.frame)
S3method(chop,data.frame)
S3method(complete,data.frame)
S3method(complete_,data.frame)
S3method(drop_na,data.frame)
Expand All @@ -16,13 +19,17 @@ S3method(full_seq,POSIXct)
S3method(full_seq,numeric)
S3method(gather,data.frame)
S3method(gather_,data.frame)
S3method(hoist,data.frame)
S3method(nest,data.frame)
S3method(nest,grouped_df)
S3method(nest,tbl_df)
S3method(nest_legacy,data.frame)
S3method(nest_legacy,tbl_df)
S3method(pack,data.frame)
S3method(pivot_longer,data.frame)
S3method(pivot_longer_spec,data.frame)
S3method(pivot_wider,data.frame)
S3method(pivot_wider_spec,data.frame)
S3method(replace_na,data.frame)
S3method(replace_na,default)
S3method(separate,data.frame)
Expand All @@ -31,11 +38,16 @@ S3method(separate_rows,data.frame)
S3method(separate_rows_,data.frame)
S3method(spread,data.frame)
S3method(spread_,data.frame)
S3method(unchop,data.frame)
S3method(uncount,data.frame)
S3method(unite,data.frame)
S3method(unite_,data.frame)
S3method(unnest,data.frame)
S3method(unnest,rowwise_df)
S3method(unnest_legacy,data.frame)
S3method(unnest_longer,data.frame)
S3method(unnest_wider,data.frame)
S3method(unpack,data.frame)
export("%>%")
export(as_tibble)
export(build_longer_spec)
Expand Down
17 changes: 15 additions & 2 deletions R/chop.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#' @param ptype Optionally, supply a data frame prototype for the output `cols`,
#' overriding the default that will be guessed from the combination of
#' individual values.
#' @param ... Additional arguments passed on to methods.
#' @export
#' @examples
#' # Chop ==============================================================
Expand All @@ -66,7 +67,13 @@
#' df <- tibble(x = 1:3, y = list(NULL, tibble(x = 1), tibble(y = 1:2)))
#' df %>% unchop(y)
#' df %>% unchop(y, keep_empty = TRUE)
chop <- function(data, cols) {
chop <- function(data, cols, ...) {
ellipsis::check_dots_used()
UseMethod("chop")
}

#' @export
chop.data.frame <- function(data, cols, ...) {
if (missing(cols)) {
return(data)
}
Expand All @@ -89,7 +96,13 @@ chop <- function(data, cols) {

#' @export
#' @rdname chop
unchop <- function(data, cols, keep_empty = FALSE, ptype = NULL) {
unchop <- function(data, cols, keep_empty = FALSE, ptype = NULL, ...) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I think it's better to put the dots before the first optional argument. It's possible that a few people might have relied on partial or position based matching, but check_dots_used() will catch that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In build_wider_spec() I put the dots after names_from and values_from as they felt like main arguments and that's also how they were used in the test names_glue affects output names

ellipsis::check_dots_used()
UseMethod("unchop")
}

#' @export
unchop.data.frame <- function(data, cols, keep_empty = FALSE, ptype = NULL, ...) {
cols <- tidyselect::eval_select(enquo(cols), data)
if (length(cols) == 0) {
return(data)
Expand Down
13 changes: 12 additions & 1 deletion R/pack.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
#' df %>% unpack(c(y, z))
#' df %>% unpack(c(y, z), names_sep = "_")
pack <- function(.data, ..., .names_sep = NULL) {
UseMethod("pack")
}

#' @export
pack.data.frame <- function(.data, ..., .names_sep = NULL) {
cols <- enquos(...)
if (any(names2(cols) == "")) {
abort("All elements of `...` must be named")
Expand Down Expand Up @@ -94,7 +99,13 @@ pack <- function(.data, ..., .names_sep = NULL) {
#'
#' See [vctrs::vec_as_names()] for more details on these terms and the
#' strategies used to enforce them.
unpack <- function(data, cols, names_sep = NULL, names_repair = "check_unique") {
unpack <- function(data, cols, names_sep = NULL, names_repair = "check_unique", ...) {
ellipsis::check_dots_used()
UseMethod("unpack")
}

#' @export
unpack.data.frame <- function(data, cols, names_sep = NULL, names_repair = "check_unique", ...) {
check_present(cols)
cols <- tidyselect::eval_select(enquo(cols), data)

Expand Down
33 changes: 31 additions & 2 deletions R/pivot-long.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,22 @@ pivot_longer_spec <- function(data,
names_repair = "check_unique",
values_drop_na = FALSE,
values_ptypes = list(),
values_transform = list()
values_transform = list(),
...
) {
ellipsis::check_dots_used()
UseMethod("pivot_longer_spec")
}

#' @export
pivot_longer_spec.data.frame <- function(data,
spec,
names_repair = "check_unique",
values_drop_na = FALSE,
values_ptypes = list(),
values_transform = list(),
...
) {
spec <- check_spec(spec)
spec <- deduplicate_spec(spec, data)

Expand Down Expand Up @@ -271,7 +285,22 @@ build_longer_spec <- function(data, cols,
names_sep = NULL,
names_pattern = NULL,
names_ptypes = NULL,
names_transform = NULL) {
names_transform = NULL,
...) {
ellipsis::check_dots_used()
UseMethod("build_longer_spec")
}

#' @export
build_longer_spec.data.frame <- function(data, cols,
names_to = "name",
values_to = "value",
names_prefix = NULL,
names_sep = NULL,
names_pattern = NULL,
names_ptypes = NULL,
names_transform = NULL,
...) {
cols <- tidyselect::eval_select(enquo(cols), data[unique(names(data))])

if (length(cols) == 0) {
Expand Down
40 changes: 34 additions & 6 deletions R/pivot-wide.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,24 @@ pivot_wider.data.frame <- function(data,
#' us_rent_income %>%
#' pivot_wider_spec(spec2)
pivot_wider_spec <- function(data,
spec,
names_repair = "check_unique",
id_cols = NULL,
values_fill = NULL,
values_fn = NULL) {
spec,
names_repair = "check_unique",
id_cols = NULL,
values_fill = NULL,
values_fn = NULL,
...) {
ellipsis::check_dots_used()
UseMethod("pivot_wider_spec")
}

#' @export
pivot_wider_spec.data.frame <- function(data,
spec,
names_repair = "check_unique",
id_cols = NULL,
values_fill = NULL,
values_fn = NULL,
...) {
spec <- check_spec(spec)

if (is.function(values_fn)) {
Expand Down Expand Up @@ -289,8 +302,23 @@ build_wider_spec <- function(data,
names_prefix = "",
names_sep = "_",
names_glue = NULL,
names_sort = FALSE
names_sort = FALSE,
...
) {
ellipsis::check_dots_used()
UseMethod("build_wider_spec")
}

#' @export
build_wider_spec.data.frame <- function(data,
names_from = name,
values_from = value,
names_prefix = "",
names_sep = "_",
names_glue = NULL,
names_sort = FALSE,
...
) {
names_from <- tidyselect::eval_select(enquo(names_from), data)
values_from <- tidyselect::eval_select(enquo(values_from), data)

Expand Down
39 changes: 37 additions & 2 deletions R/rectangle.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@
#'
#' @export hoist
hoist <- function(.data, .col, ..., .remove = TRUE, .simplify = TRUE, .ptype = list(), .transform = list()) {
UseMethod("hoist")
}

#' @export
hoist.data.frame <- function(.data, .col, ..., .remove = TRUE, .simplify = TRUE, .ptype = list(), .transform = list()) {
check_present(.col)
.col <- tidyselect::vars_pull(names(.data), !!enquo(.col))
x <- .data[[.col]]
Expand Down Expand Up @@ -201,8 +206,24 @@ unnest_longer <- function(data, col,
names_repair = "check_unique",
simplify = TRUE,
ptype = list(),
transform = list()
transform = list(),
...
) {
ellipsis::check_dots_used()
UseMethod("unnest_longer")
}

#' @export
unnest_longer.data.frame <- function(data, col,
values_to = NULL,
indices_to = NULL,
indices_include = NULL,
names_repair = "check_unique",
simplify = TRUE,
ptype = list(),
transform = list(),
...
) {

check_present(col)
col <- tidyselect::vars_pull(names(data), !!enquo(col))
Expand Down Expand Up @@ -247,8 +268,22 @@ unnest_wider <- function(data, col,
simplify = TRUE,
names_repair = "check_unique",
ptype = list(),
transform = list()
transform = list(),
...
) {
ellipsis::check_dots_used()
UseMethod("unnest_wider")
}

#' @export
unnest_wider.data.frame <- function(data, col,
names_sep = NULL,
simplify = TRUE,
names_repair = "check_unique",
ptype = list(),
transform = list(),
...
) {
check_present(col)
col <- tidyselect::vars_pull(tbl_vars(data), !!enquo(col))

Expand Down
9 changes: 8 additions & 1 deletion R/uncount.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' identifier for each created row.
#' @param .remove If `TRUE`, and `weights` is the name of a column in `data`,
#' then this column is removed.
#' @param ... Additional arguments passed on to methods.
#' @export
#' @examples
#' df <- tibble(x = c("a", "b"), n = c(1, 2))
Expand All @@ -21,7 +22,13 @@
#'
#' # Or expressions
#' uncount(df, 2 / n)
uncount <- function(data, weights, .remove = TRUE, .id = NULL) {
uncount <- function(data, weights, .remove = TRUE, .id = NULL, ...) {
ellipsis::check_dots_used()
UseMethod("uncount")
}

#' @export
uncount.data.frame <- function(data, weights, .remove = TRUE, .id = NULL, ...) {
weights_quo <- enquo(weights)
w <- dplyr::pull(dplyr::mutate(data, `_weight` = !! weights_quo))
# NOTE `vec_cast()` and check for positive weights can be removed
Expand Down
6 changes: 4 additions & 2 deletions man/chop.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/hoist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/pack.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions man/pivot_longer_spec.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading