From cebf5e95d58ea47c1c8c8c92b3e8af83ef26808a Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Fri, 29 Apr 2022 06:47:39 +0000 Subject: [PATCH 1/4] Make `uncount()` generic --- NAMESPACE | 1 + R/uncount.R | 9 ++++++++- man/uncount.Rd | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 8733cbfd4..86adee5e0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,6 +32,7 @@ S3method(separate_rows,data.frame) S3method(separate_rows_,data.frame) S3method(spread,data.frame) S3method(spread_,data.frame) +S3method(uncount,data.frame) S3method(unite,data.frame) S3method(unite_,data.frame) S3method(unnest,data.frame) diff --git a/R/uncount.R b/R/uncount.R index bda3d72b8..568582ef0 100644 --- a/R/uncount.R +++ b/R/uncount.R @@ -21,7 +21,14 @@ #' #' # Or expressions #' uncount(df, 2 / n) -uncount <- function(data, weights, .remove = TRUE, .id = NULL) { +uncount <- function(data, weights, ..., .remove = TRUE, .id = NULL) { + UseMethod("uncount") +} + +#' @export +uncount.data.frame <- function(data, weights, ..., .remove = TRUE, .id = NULL) { + check_dots_used() + weights_quo <- enquo(weights) w <- dplyr::pull(dplyr::mutate(data, `_weight` = !!weights_quo)) # NOTE `vec_cast()` and check for positive weights can be removed diff --git a/man/uncount.Rd b/man/uncount.Rd index 4ea97eb93..c3d4afd60 100644 --- a/man/uncount.Rd +++ b/man/uncount.Rd @@ -4,7 +4,7 @@ \alias{uncount} \title{"Uncount" a data frame} \usage{ -uncount(data, weights, .remove = TRUE, .id = NULL) +uncount(data, weights, ..., .remove = TRUE, .id = NULL) } \arguments{ \item{data}{A data frame, tibble, or grouped tibble.} From 4bbd6580a8743472863f2d1c03fcbfbb46469600 Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Fri, 29 Apr 2022 06:52:07 +0000 Subject: [PATCH 2/4] Document dots for `uncount()` --- R/uncount.R | 1 + man/uncount.Rd | 2 ++ 2 files changed, 3 insertions(+) diff --git a/R/uncount.R b/R/uncount.R index 568582ef0..dc778823e 100644 --- a/R/uncount.R +++ b/R/uncount.R @@ -6,6 +6,7 @@ #' @param data A data frame, tibble, or grouped tibble. #' @param weights A vector of weights. Evaluated in the context of `data`; #' supports quasiquotation. +#' @param ... Additional arguments passed on to methods. #' @param .id Supply a string to create a new variable which gives a unique #' identifier for each created row. #' @param .remove If `TRUE`, and `weights` is the name of a column in `data`, diff --git a/man/uncount.Rd b/man/uncount.Rd index c3d4afd60..459d9f1bf 100644 --- a/man/uncount.Rd +++ b/man/uncount.Rd @@ -12,6 +12,8 @@ uncount(data, weights, ..., .remove = TRUE, .id = NULL) \item{weights}{A vector of weights. Evaluated in the context of \code{data}; supports quasiquotation.} +\item{...}{Additional arguments passed on to methods.} + \item{.remove}{If \code{TRUE}, and \code{weights} is the name of a column in \code{data}, then this column is removed.} From 5f1f99868a9828b76793aac0db19446bac2e9d3f Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Fri, 6 May 2022 06:03:08 +0000 Subject: [PATCH 3/4] NEWS bullet --- NEWS.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS.md b/NEWS.md index d25dbd16d..b132a94eb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,11 @@ # tidyr (development version) +* `uncount()` is now generic so implementations can be provided for objects + other than data frames (@mgirlich, #1358). + +* `uncount()` gained the `...` argument. It comes between the required and the + optional arguments (@mgirlich, #1358). + * `pivot_longer()` gained a new `cols_vary` argument for controlling the ordering of the output rows relative to their original row number (#1312). From 3818382fa048ecb706ed311cd1ad1871088004c3 Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Mon, 9 May 2022 05:57:40 +0000 Subject: [PATCH 4/4] Move `check_dots_used()` into `uncount()` generic. --- R/uncount.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/uncount.R b/R/uncount.R index dc778823e..ce4be985c 100644 --- a/R/uncount.R +++ b/R/uncount.R @@ -23,13 +23,12 @@ #' # Or expressions #' uncount(df, 2 / n) uncount <- function(data, weights, ..., .remove = TRUE, .id = NULL) { + check_dots_used() UseMethod("uncount") } #' @export uncount.data.frame <- function(data, weights, ..., .remove = TRUE, .id = NULL) { - check_dots_used() - weights_quo <- enquo(weights) w <- dplyr::pull(dplyr::mutate(data, `_weight` = !!weights_quo)) # NOTE `vec_cast()` and check for positive weights can be removed