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) {
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
31 changes: 30 additions & 1 deletion 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 @@ -265,13 +279,28 @@ pivot_longer_spec <- function(data,
#' @rdname pivot_longer_spec
#' @export
build_longer_spec <- function(data, cols,
...,
names_to = "name",
values_to = "value",
names_prefix = NULL,
names_sep = NULL,
names_pattern = NULL,
names_ptypes = 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
45 changes: 37 additions & 8 deletions R/pivot-wide.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pivot_wider <- function(data,

#' @export
pivot_wider.data.frame <- function(data,
...,
id_cols = NULL,
names_from = name,
names_prefix = "",
Expand All @@ -118,8 +119,7 @@ pivot_wider.data.frame <- function(data,
names_repair = "check_unique",
values_from = value,
values_fill = NULL,
values_fn = NULL,
...
values_fn = NULL
) {
names_from <- enquo(names_from)
values_from <- enquo(values_from)
Expand All @@ -133,7 +133,8 @@ pivot_wider.data.frame <- function(data,
)

id_cols <- enquo(id_cols)
pivot_wider_spec(data, spec, !!id_cols,
pivot_wider_spec(data, spec,
id_cols = !!id_cols,
names_repair = names_repair,
values_fill = values_fill,
values_fn = values_fn
Expand Down Expand Up @@ -186,11 +187,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 @@ -286,11 +300,26 @@ pivot_wider_spec <- function(data,
build_wider_spec <- function(data,
names_from = name,
values_from = value,
...,
names_prefix = "",
names_sep = "_",
names_glue = NULL,
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
35 changes: 35 additions & 0 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 @@ -195,6 +200,7 @@ check_pluckers <- function(...) {
#' has inner names.
#' @inheritParams unnest
unnest_longer <- function(data, col,
...,
values_to = NULL,
indices_to = NULL,
indices_include = NULL,
Expand All @@ -203,6 +209,21 @@ unnest_longer <- function(data, col,
ptype = 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 @@ -243,12 +264,26 @@ unnest_longer <- function(data, col,
#' as is. If a string, the inner and outer names will be paste together using
#' `names_sep` as a separator.
unnest_wider <- function(data, col,
...,
names_sep = NULL,
simplify = TRUE,
names_repair = "check_unique",
ptype = 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.

2 changes: 2 additions & 0 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.

Loading