diff --git a/NAMESPACE b/NAMESPACE index 46e849b..837a35a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -43,6 +43,9 @@ export(select_all) export(select_at) export(select_if) export(semi_join) +export(separate_wider_delim) +export(separate_wider_position) +export(separate_wider_regex) export(slice) export(slice_head) export(slice_max) diff --git a/NEWS.md b/NEWS.md index bf90fa4..37dc96c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,7 @@ - mutate now reports dropped columns (#53) - filter reports the number of groups (#52) - fix bug with new dplyr::join_by syntax (#58) +- tidyr: support for separate_wider_* functions (#62) # 1.0.2 diff --git a/R/longer_wider.R b/R/longer_wider.R index a7e4427..845d508 100644 --- a/R/longer_wider.R +++ b/R/longer_wider.R @@ -19,6 +19,22 @@ spread <- function(data, ...) { log_longer_wider(data, .fun = tidyr::spread, .funname = "spread", ...) } +#' @export +separate_wider_position <- function(data, ...) { + log_longer_wider(data, .fun = tidyr::separate_wider_position, .funname = "separate_wider_position", ...) +} + +#' @export +separate_wider_delim <- function(data, ...) { + log_longer_wider(data, .fun = tidyr::separate_wider_delim, .funname = "separate_wider_delim", ...) +} + +#' @export +separate_wider_regex <- function(data, ...) { + log_longer_wider(data, .fun = tidyr::separate_wider_regex, .funname = "separate_wider_regex", ...) +} + + log_longer_wider <- function(.data, .fun, .funname, ...) { newdata <- .fun(.data, ...) diff --git a/man/separate_wider_delim.Rd b/man/separate_wider_delim.Rd new file mode 100644 index 0000000..00a2248 --- /dev/null +++ b/man/separate_wider_delim.Rd @@ -0,0 +1,19 @@ +\name{separate_wider_delim} +\alias{separate_wider_delim} +\title{Wrapper around tidyr::separate_wider_delim +that prints information about the operation} +\usage{ +separate_wider_delim(data, ...) +} +\arguments{ +\item{data}{see \link[tidyr:separate_wider_delim]{separate_wider_delim}} + +\item{...}{see \link[tidyr:separate_wider_delim]{separate_wider_delim}} +} +\value{ +see \link[tidyr:separate_wider_delim]{separate_wider_delim} +} +\description{ +Wrapper around tidyr::separate_wider_delim +that prints information about the operation +} diff --git a/man/separate_wider_position.Rd b/man/separate_wider_position.Rd new file mode 100644 index 0000000..b4a9a0e --- /dev/null +++ b/man/separate_wider_position.Rd @@ -0,0 +1,19 @@ +\name{separate_wider_position} +\alias{separate_wider_position} +\title{Wrapper around tidyr::separate_wider_position +that prints information about the operation} +\usage{ +separate_wider_position(data, ...) +} +\arguments{ +\item{data}{see \link[tidyr:separate_wider_delim]{separate_wider_position}} + +\item{...}{see \link[tidyr:separate_wider_delim]{separate_wider_position}} +} +\value{ +see \link[tidyr:separate_wider_delim]{separate_wider_position} +} +\description{ +Wrapper around tidyr::separate_wider_position +that prints information about the operation +} diff --git a/man/separate_wider_regex.Rd b/man/separate_wider_regex.Rd new file mode 100644 index 0000000..5a3109a --- /dev/null +++ b/man/separate_wider_regex.Rd @@ -0,0 +1,19 @@ +\name{separate_wider_regex} +\alias{separate_wider_regex} +\title{Wrapper around tidyr::separate_wider_regex +that prints information about the operation} +\usage{ +separate_wider_regex(data, ...) +} +\arguments{ +\item{data}{see \link[tidyr:separate_wider_delim]{separate_wider_regex}} + +\item{...}{see \link[tidyr:separate_wider_delim]{separate_wider_regex}} +} +\value{ +see \link[tidyr:separate_wider_delim]{separate_wider_regex} +} +\description{ +Wrapper around tidyr::separate_wider_regex +that prints information about the operation +} diff --git a/tests/testthat/test_longer_wider.R b/tests/testthat/test_longer_wider.R index 4785366..e54ace4 100644 --- a/tests/testthat/test_longer_wider.R +++ b/tests/testthat/test_longer_wider.R @@ -3,115 +3,82 @@ library("tidyr") library("tidylog") context("test_longer_wider") -test_that("pivot_longer", { - mtcars$id <- seq_len(nrow(mtcars)) - - expect_message({ - outlog <- tidylog::pivot_longer(mtcars, -id, names_to = "var", values_to = "value") - }) - expect_equal(nrow(outlog), 352) - expect_equal(ncol(outlog), 3) - - expect_silent({ - outtidy <- tidyr::pivot_longer(mtcars, -id, names_to = "var", values_to = "value") - }) - - expect_equal(outlog, outtidy) - -}) - -test_that("pivot_longer: argument order", { - mtcars$id <- seq_len(nrow(mtcars)) - - expect_message({ - out_ab <- tidylog::pivot_longer(names_to = "var", values_to = "value", - data = mtcars, cols = -id) - }) - - expect_message({ - out_ba <- tidylog::pivot_longer(data = mtcars, cols = -id, - names_to = "var", values_to = "value") - }) - - expect_equal(out_ab, out_ba) -}) - -test_that("pivot_wider", { - expect_message({ - outlog <- tidylog::pivot_wider(mtcars, names_from = vs, values_from = cyl) - }) - expect_equal(nrow(outlog), 32) - expect_equal(ncol(outlog), 11) - - expect_silent({ - outtidyr <- tidyr::pivot_wider(mtcars, names_from = vs, values_from = cyl) - }) - - expect_equal(outlog, outtidyr) -}) - -test_that("pivot_wider: argument order", { - expect_message({ - out_ab <- tidylog::pivot_wider(names_from = vs, values_from = cyl, data = mtcars) - }) - - expect_message({ - out_ba <- tidylog::pivot_wider(mtcars, names_from = vs, values_from = cyl) - }) - - expect_equal(out_ab, out_ba) -}) - -test_that("gather", { - expect_message({ - outlog <- tidylog::gather(mtcars) - }) - expect_equal(nrow(outlog), 352) - expect_equal(ncol(outlog), 2) - - expect_silent({ - outtidy <- tidyr::gather(mtcars) - }) - - expect_equal(outlog, outtidy) - -}) - -test_that("gather: argument order", { - expect_message({ - out_ab <- tidylog::gather(data = mtcars, carb) - }) - - expect_message({ - out_ba <- tidylog::gather(carb, data = mtcars) - }) - - expect_equal(out_ab, out_ba) -}) - -test_that("spread", { - expect_message({ - outlog <- tidylog::spread(mtcars, hp, carb) - }) - expect_equal(nrow(outlog), 32) - expect_equal(ncol(outlog), 31) - - expect_silent({ - outtidyr <- tidyr::spread(mtcars, hp, carb) - }) - - expect_equal(outlog, outtidyr) - -}) - -test_that("spread: argument order", { - expect_message({ - out_ab <- tidylog::spread(data = mtcars, hp, carb) - }) - - expect_message({ - out_ba <- tidylog::spread(hp, carb, data = mtcars) - }) - - expect_equal(out_ab, out_ba) -}) +mtcars_with_id <- dplyr::mutate(mtcars, id = seq_len(dplyr::n())) +mtcars_with_name <- tidyr::as_tibble(mtcars, rownames = "name") + +test_tidyr_fun <- function(fun_name, tidylog_fun, tidyr_fun, data, ..., expected_nrow, expected_ncol) { + # Test that tidylog output matches tidyr. + test_that(fun_name, { + expect_message({ + outlog <- tidylog_fun(data, ...) + }) + expect_equal(nrow(outlog), expected_nrow) + expect_equal(ncol(outlog), expected_ncol) + + expect_silent({ + outtidy <- tidyr_fun(data, ...) + }) + + expect_equal(outlog, outtidy) + }) + + # Test that argument order doesn't matter. + test_that(glue::glue("{fun_name}: argument order"), { + expect_message({ + out_ab <- tidylog_fun(data = data, ...) + }) + + expect_message({ + out_ba <- tidylog_fun(..., data = data) + }) + + expect_equal(out_ab, out_ba) + }) +} + + +test_tidyr_fun( + "pivot_longer", tidylog::pivot_longer, tidyr::pivot_longer, + mtcars_with_id, -id, names_to = "var", values_to = "value", + expected_nrow = 352, expected_ncol = 3 +) + + +test_tidyr_fun( + "pivot_wider", tidylog::pivot_wider, tidyr::pivot_wider, + mtcars, names_from = vs, values_from = cyl, + expected_nrow = 32, expected_ncol = 11 +) + +test_tidyr_fun( + "gather", tidylog::gather, tidyr::gather, + mtcars, + expected_nrow = 352, expected_ncol = 2 +) + +test_tidyr_fun( + "spread", tidylog::spread, tidyr::spread, + mtcars, hp, carb, + expected_nrow = 32, expected_ncol = 31 +) + +test_tidyr_fun( + "separate_wider_delim", tidylog::separate_wider_delim, tidyr::separate_wider_delim, + mtcars_with_name, name, " ", names=c("A", "B"), + too_few = "align_start", too_many = "merge", + expected_nrow = 32, expected_ncol = 13 +) + +test_tidyr_fun( + "separate_wider_position", tidylog::separate_wider_position, tidyr::separate_wider_position, + mtcars_with_name, name, c("A" = 3, "B" = 4), + too_few = "align_start", too_many = "drop", + expected_nrow = 32, expected_ncol = 13 +) + +test_tidyr_fun( + "separate_wider_regex", tidylog::separate_wider_regex, tidyr::separate_wider_regex, + mtcars_with_name, name, c("A" = "\\w+", "\\s", "B" = ".*"), + too_few = "align_start", + expected_nrow = 32, expected_ncol = 13 +)