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

Make pack() and unpack() generic #1372

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ S3method(nest,grouped_df)
S3method(nest,tbl_df)
S3method(nest_legacy,data.frame)
S3method(nest_legacy,tbl_df)
S3method(pack,data.frame)
S3method(pack,tbl_df)
S3method(pivot_longer,data.frame)
S3method(pivot_wider,data.frame)
S3method(replace_na,data.frame)
Expand All @@ -38,6 +40,8 @@ S3method(unite_,data.frame)
S3method(unnest,data.frame)
S3method(unnest,rowwise_df)
S3method(unnest_legacy,data.frame)
S3method(unpack,data.frame)
S3method(unpack,tbl_df)
export("%>%")
export(all_of)
export(any_of)
Expand Down
36 changes: 35 additions & 1 deletion R/pack.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#' @param ... <[`tidy-select`][tidyr_tidy_select]> Columns to pack, specified
#' using name-variable pairs of the form `new_col = c(col1, col2, col3)`.
#' The right hand side can be any valid tidy select expression.
#' Must be empty for `unpack()`, reserved for future extensions.
#' @export
#' @examples
#' # Packing =============================================================
Expand Down Expand Up @@ -58,6 +59,22 @@
#' 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) {
# The data frame print handles packed data frames poorly, so we want to
# convert data frames (but not subclasses) to tibbles
if (identical(class(.data), "data.frame")) {
.data <- as_tibble(.data)
}

pack.tbl_df(.data, ..., .names_sep = .names_sep)
}

#' @export
pack.tbl_df <- function(.data, ..., .names_sep = NULL) {
cols <- enquos(...)
if (any(names2(cols) == "")) {
abort("All elements of `...` must be named")
Expand Down Expand Up @@ -96,7 +113,24 @@ 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") {
check_dots_empty()
UseMethod("unpack")
}

#' @export
unpack.data.frame <- function(data, cols, ..., names_sep = NULL, names_repair = "check_unique") {
# The data frame print handles packed data frames poorly, so we want to
# convert data frames (but not subclasses) to tibbles
if (identical(class(.data), "data.frame")) {
.data <- as_tibble(.data)
}

unpack.tbl_df(data, cols, ..., names_sep = names_sep, names_repair = names_repair)
}

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

Expand Down
5 changes: 3 additions & 2 deletions man/pack.Rd

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