Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/elbersb/tidylog
Browse files Browse the repository at this point in the history
  • Loading branch information
elbersb committed May 8, 2024
2 parents e8cc348 + cc72f77 commit c38fe96
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 112 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions R/longer_wider.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)

Expand Down
19 changes: 19 additions & 0 deletions man/separate_wider_delim.Rd
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions man/separate_wider_position.Rd
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions man/separate_wider_regex.Rd
Original file line number Diff line number Diff line change
@@ -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
}
191 changes: 79 additions & 112 deletions tests/testthat/test_longer_wider.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit c38fe96

Please sign in to comment.