Skip to content

Add log to dplyr function ungroup() #33

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

Merged
merged 21 commits into from
Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from 11 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
10 changes: 7 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
Package: tidylog
Type: Package
Title: Logging for 'dplyr' and 'tidyr' Functions
Title: Logging for 'dplyr' and 'tidyr' functions
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a typo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: 3e70eea

Version: 0.2.0.9000
Authors@R: person("Benjamin", "Elbers", email = "[email protected]",
role = c("aut", "cre"), comment = c(ORCID = "0000-0001-5392-3448"))
Authors@R: c(
person("Benjamin", "Elbers", email = "[email protected]",
role = c("aut", "cre"), comment = c(ORCID = "0000-0001-5392-3448")),
person("Damiano", "Oldoni", email = "[email protected]",
role = c("aut", "ctb"), comment = c(ORCID = "0000-0003-3445-7562"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use just "ctb" here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am very sorry: I am definitely not the author! A copy paste error.
Solved here: baae929

)
Description: Provides feedback about 'dplyr' and 'tidyr' operations.
License: MIT + file LICENSE
Imports: dplyr, tidyr, glue, clisymbols
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export(transmute)
export(transmute_all)
export(transmute_at)
export(transmute_if)
export(ungroup)
import(clisymbols)
import(dplyr)
import(tidyr)
1 change: 0 additions & 1 deletion R/filter.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' Wrapper around dplyr::filter and related functions
#' that prints information about the operation
#'
#'
#' @param .data a tbl; see \link[dplyr]{filter}
#' @param ... see \link[dplyr]{filter}
#' @return see \link[dplyr]{filter}
Expand Down
26 changes: 26 additions & 0 deletions R/ungroup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#' Wrapper around dplyr::ungroup that prints information about the operation
#'
#' @param .data a tbl; see \link[dplyr]{group_by}
#' @param ... see \link[dplyr]{group_by}
#' @return see \link[dplyr]{group_by}
#' @examples
#' mtcars <- group_by(mtcars, am, cyl)
#' #> group_by: 2 grouping variables (am, cyl)
#' mtcars <- ungroup(mtcars)
#' #> ungroup: no grouping variables left
#' @import dplyr
#' @export
ungroup <- function(.data, ...) {
log_ungroup(.data, .fun = dplyr::ungroup, .funname = "ungroup", ...)
}


log_ungroup <- function(.data, .fun, .funname, ...) {
newdata <- .fun(.data, ...)
if (!"data.frame" %in% class(.data) | !should_display()) {
return(newdata)
}
display(glue::glue(
"{.funname}: no grouping variables left"))
newdata
}
7 changes: 4 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ summary <- mtcars %>%
mutate(mpg_round = round(mpg)) %>%
group_by(cyl, mpg_round, am) %>%
tally() %>%
filter(n >= 1)
filter(n >= 1) %>%
ungroup()
```
Here, it might have been accidental that the last `filter` command had no effect.
Here, it might have been accidental that the `filter` command had no effect.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please leave as is, or change to "second"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right: it refers to the fact that there are two filters, indeed.
Solved here: 39c624f


## Installation

Expand Down Expand Up @@ -154,7 +155,7 @@ a <- mtcars %>%

b <- iris %>%
group_by(Species) %>%
summarize_all(list(~min, ~max))
summarize_all(list(min, max))
```

### tally, count, add_tally, add_count
Expand Down
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ summary <- mtcars %>%
mutate(mpg_round = round(mpg)) %>%
group_by(cyl, mpg_round, am) %>%
tally() %>%
filter(n >= 1)
filter(n >= 1) %>%
ungroup()
#> select: dropped 7 variables (disp, drat, wt, qsec, vs, …)
#> filter: removed 6 rows (19%), 26 rows remaining
#> mutate: new variable 'mpg_round' with 15 unique values and 0% NA
#> group_by: 3 grouping variables (cyl, mpg_round, am)
#> tally: now 20 rows and 4 columns, 2 group variables remaining (cyl, mpg_round)
#> filter (grouped): no rows removed
#> ungroup: no grouping variables left
```

Here, it might have been accidental that the last `filter` command had
no effect.
Here, it might have been accidental that the `filter` command had no
effect.

## Installation

Expand Down Expand Up @@ -221,7 +223,7 @@ a <- mtcars %>%

b <- iris %>%
group_by(Species) %>%
summarize_all(list(~min, ~max))
summarize_all(list(min, max))
#> group_by: one grouping variable (Species)
#> summarize_all: now 3 rows and 9 columns, ungrouped
```
Expand Down Expand Up @@ -258,7 +260,7 @@ wide <- long %>%
## Turning logging off, registering additional loggers

To turn off the output for just a particular function call, you can
simply call the dplyr and tidyr functions directly, e.g. `dplyr::filter`
simply call the dplyr and tidyr functions directly, e.g. `dplyr::filter`
or `tidyr::drop_na`.

To turn off the output more permanently, set the global option
Expand Down Expand Up @@ -287,7 +289,8 @@ a <- filter(mtcars, mpg > 20)
#> filter: removed 18 rows (56%), 14 rows remaining
```

To print the output both to the screen and to a file, you could use:
To print the output both to the screen and to a file, you could
use:

``` r
log_to_file <- function(text) cat(text, file = "log.txt", sep = "\n", append = TRUE)
Expand Down
25 changes: 25 additions & 0 deletions man/ungroup.Rd

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

3 changes: 0 additions & 3 deletions tests/testthat/test_filter.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library("dplyr")
library("tidyr")
library("tidylog")
context("test_filter")

test_that("filter", {
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test_gather_spread.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library("dplyr")
library("tidyr")
library("tidylog")
context("test_gather")

test_that("gather", {
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_group_by.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
library("dplyr")
library("tidylog")
context("test_group_by")

test_that("group_by", {
Expand Down
91 changes: 66 additions & 25 deletions tests/testthat/test_join.R
Original file line number Diff line number Diff line change
@@ -1,60 +1,101 @@
library("dplyr")
library("tidylog")
context("test_group_by")

test_that("join", {

expect_message({
out <- tidylog::inner_join(band_members, band_instruments, by = "name")
out <- tidylog::inner_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
})
expect_equal(out, dplyr::inner_join(band_members, band_instruments, by = "name"))
expect_equal(out, dplyr::inner_join(dplyr::band_members,
dplyr::band_instruments,
by = "name"))

expect_message({
out <- tidylog::full_join(band_members, band_instruments, by = "name")
out <- tidylog::full_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
})
expect_equal(out, dplyr::full_join(band_members, band_instruments, by = "name"))
expect_equal(out, dplyr::full_join(dplyr::band_members,
dplyr::band_instruments,
by = "name"))

expect_message({
out <- tidylog::left_join(band_members, band_instruments, by = "name")
out <- tidylog::left_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
})
expect_equal(out, dplyr::left_join(band_members, band_instruments, by = "name"))
expect_equal(out, dplyr::left_join(dplyr::band_members,
dplyr::band_instruments,
by = "name"))

expect_message({
out <- tidylog::right_join(band_members, band_instruments, by = "name")
out <- tidylog::right_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
})
expect_equal(out, dplyr::right_join(band_members, band_instruments, by = "name"))
expect_equal(out, dplyr::right_join(dplyr::band_members,
dplyr::band_instruments,
by = "name"))

expect_message({
out <- tidylog::anti_join(band_members, band_instruments, by = "name")
out <- tidylog::anti_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
})
expect_equal(out, dplyr::anti_join(band_members, band_instruments, by = "name"))
expect_equal(out, dplyr::anti_join(dplyr::band_members,
dplyr::band_instruments,
by = "name"))

expect_message({
out <- tidylog::semi_join(band_members, band_instruments, by = "name")
out <- tidylog::semi_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
})
expect_equal(out, dplyr::semi_join(band_members, band_instruments, by = "name"))
expect_equal(out, dplyr::semi_join(dplyr::band_members,
dplyr::band_instruments,
by = "name"))

expect_silent({
out <- dplyr::inner_join(band_members, band_instruments, by = "name")
out <- dplyr::full_join(band_members, band_instruments, by = "name")
out <- dplyr::left_join(band_members, band_instruments, by = "name")
out <- dplyr::right_join(band_members, band_instruments, by = "name")
out <- dplyr::anti_join(band_members, band_instruments, by = "name")
out <- dplyr::semi_join(band_members, band_instruments, by = "name")
out <- dplyr::inner_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
out <- dplyr::full_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
out <- dplyr::left_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
out <- dplyr::right_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
out <- dplyr::anti_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
out <- dplyr::semi_join(dplyr::band_members,
dplyr::band_instruments,
by = "name")
})
})

test_that("join: argument order", {
expect_message({
out <- tidylog::inner_join(y = band_instruments, x = band_members)
out <- tidylog::inner_join(y = dplyr::band_instruments,
x = dplyr::band_members)
})
expect_equal(out, dplyr::inner_join(y = band_instruments, x = band_members))
expect_equal(out, dplyr::inner_join(x = band_members, y = band_instruments))
expect_equal(out, dplyr::inner_join(y = dplyr::band_instruments,
x = dplyr::band_members))
expect_equal(out, dplyr::inner_join(x = dplyr::band_members,
y = dplyr::band_instruments))

expect_message({
out <- tidylog::inner_join(by = "name", x = band_members, y = band_instruments)
out <- tidylog::inner_join(by = "name",
x = dplyr::band_members,
y = dplyr::band_instruments)
})
expect_equal(out, dplyr::inner_join(by = "name", x = band_members, y = band_instruments))
expect_equal(out, dplyr::inner_join(by = "name",
x = dplyr::band_members,
y = dplyr::band_instruments))
})

# adapted from dplyr tests
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_mutate.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
library("dplyr")
library("tidylog")
context("test_mutate")

test_that("mutate", {
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_select.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
library("dplyr")
library("tidylog")
context("test_select")

test_that("select", {
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_summarize.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
library("dplyr")
library("tidylog")
context("test_summarize")

test_that("summarize", {
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test_tidylog.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
library("dplyr")
library("tidylog")
context("test_tidylog")

test_that("tidylog", {
Expand All @@ -18,7 +16,7 @@ test_that("logging on/off", {
expect_silent(tidylog::filter(mtcars, mpg > 20))
expect_silent(tidylog::select(mtcars, mpg))
expect_silent(tidylog::group_by(mtcars, mpg))
expect_silent(tidylog::left_join(band_members, band_instruments, by = "name"))
expect_silent(tidylog::left_join(dplyr::band_members, dplyr::band_instruments, by = "name"))
expect_silent(tidylog::mutate(mtcars, test = TRUE))
expect_silent(tidylog::summarize(mtcars, test = TRUE))
expect_silent(tidylog::gather(mtcars))
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test_ungroup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
context("test_ungroup")

test_that("ungroup", {
expect_message({
out <- tidylog::ungroup(mtcars, mpg)
})
expect_equal(dplyr::is.grouped_df(out), FALSE)

expect_silent({
out <- dplyr::ungroup(mtcars, mpg)
})
})