Skip to content

Commit

Permalink
update README, fix examples for gather/spread
Browse files Browse the repository at this point in the history
  • Loading branch information
elbersb committed Jul 25, 2019
1 parent 42cfd3f commit 2fabdc4
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 92 deletions.
6 changes: 4 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# 0.1.0.9000
- wrap new functions for tidyr::gather() and tidyr::spread()
- do not report negative NA (#18)
- wrap new functions for tidyr::gather() and tidyr::spread() (thanks @WilDoane)
- use clisymbols for ellipsis
- add number of remaining rows to filter (#23)
- bugfix: do not report negative NA (#18)
- bugfix: avoid partial matching (closes #26)
38 changes: 0 additions & 38 deletions R/gather.R

This file was deleted.

78 changes: 78 additions & 0 deletions R/gather_spread.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#' Wrapper around tidyr::gather
#' that prints information about the operation
#'
#' @param .data a tbl; see \link[tidyr]{gather}
#' @param ... see \link[tidyr]{gather}
#' @return see \link[tidyr]{gather}
#' @examples
#' # create id
#' mtcars$id <- 1:nrow(mtcars)
#' gathered <- gather(mtcars, "col", "data", -id)
#' #> gather: reorganized (mpg, cyl, disp, hp, drat, …) into (col, data) [was 32x12, now 352x3]
#' @import tidyr
#' @export
gather <- function(.data, ...) {
log_gather(.data, .fun = tidyr::gather, .funname = "gather", ...)
}

log_gather <- function(.data, .fun, .funname, ...) {
newdata <- .fun(.data, ...)

if (!"data.frame" %in% class(.data) | !should_display()) {
return(newdata)
}

newcols <- setdiff(names(newdata), names(.data))
oldcols <- setdiff(names(.data), names(newdata))

display(glue::glue(
"{.funname}: ",
"reorganized ({format_list(oldcols)}) ",
"into ({format_list(newcols)}) ",
"[was {nrow(.data)}x{ncol(.data)}, ",
"now {nrow(newdata)}x{ncol(newdata)}]"
))

newdata
}

#' Wrapper around tidyr::spread
#' that prints information about the operation
#'
#' @param .data a tbl; see \link[tidyr]{spread}
#' @param ... see \link[tidyr]{spread}
#' @return see \link[tidyr]{spread}
#' @examples
#' # create id
#' mtcars$id <- 1:nrow(mtcars)
#' gathered <- gather(mtcars, "col", "data", -id)
#' #> gather: reorganized (mpg, cyl, disp, hp, drat, …) into (col, data) [was 32x12, now 352x3]
#' spread(gathered, col, data)
#' #> spread: reorganized (col, data) into (am, carb, cyl, disp, drat, …) [was 352x3, now 32x12]
#' @import tidyr
#' @export
spread <- function(.data, ...) {
log_spread(.data, .fun = tidyr::spread, .funname = "spread", ...)
}

log_spread <- function(.data, .fun, .funname, ...) {
newdata <- .fun(.data, ...)

if (!"data.frame" %in% class(.data) | !should_display()) {
return(newdata)
}

newcols <- setdiff(names(newdata), names(.data))
oldcols <- setdiff(names(.data), names(newdata))

display(glue::glue(
"{.funname}: ",
"reorganized ({format_list(oldcols)}) ",
"into ({format_list(newcols)}) ",
"[was {nrow(.data)}x{ncol(.data)}, ",
"now {nrow(newdata)}x{ncol(newdata)}]"
))

newdata
}

39 changes: 0 additions & 39 deletions R/spread.R
Original file line number Diff line number Diff line change
@@ -1,39 +0,0 @@
#' Wrapper around tidyr::spread
#' that prints information about the operation
#'
#' @param .data a tbl; see \link[tidyr]{spread}
#' @param ... see \link[tidyr]{spread}
#' @return see \link[tidyr]{spread}
#' @examples
#' mtcars %>%
#' mutate(id = 1:n()) %>%
#' gather("col", "data", -id) %>%
#' spread(col, data)
#' #> spread: was 352 rows and 3 columns, now 32 rows and 12 columns;
#' #> reorganized data from (col, data) into (am, carb, cyl, disp, drat, …)
#' @import tidyr
#' @export
spread <- function(.data, ...) {
log_spread(.data, .fun = tidyr::spread, .funname = "spread", ...)
}

log_spread <- function(.data, .fun, .funname, ...) {
newdata <- .fun(.data, ...)

if (!"data.frame" %in% class(.data) | !should_display()) {
return(newdata)
}

newcols <- setdiff(names(newdata), names(.data))
oldcols <- setdiff(names(.data), names(newdata))

display(glue::glue(
"{.funname}: ",
"reorganized ({format_list(oldcols)}) ",
"into ({format_list(newcols)}) ",
"[was {nrow(.data)}x{ncol(.data)}, ",
"now {nrow(newdata)}x{ncol(newdata)}]"
))

newdata
}
10 changes: 10 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ c <- mtcars %>% count(gear, carb)
d <- mtcars %>% add_count(gear, carb, name = "count")
```

### gather, spread (tidyr)

```{r}
long <- mtcars %>%
mutate(id = 1:n()) %>%
gather("col", "data", -id)
wide <- long %>%
spread(col, data)
```

## Turning logging off, registering additional loggers

To turn off the output for just a particular function call, you can simply call the dplyr functions
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ d <- mtcars %>% add_count(gear, carb, name = "count")
#> add_count: new variable 'count' with 5 unique values and 0% NA
```

### gather, spread (tidyr)

``` r
long <- mtcars %>%
mutate(id = 1:n()) %>%
gather("col", "data", -id)
#> mutate: new variable 'id' with 32 unique values and 0% NA
#> gather: reorganized (mpg, cyl, disp, hp, drat, …) into (col, data) [was 32x12, now 352x3]
wide <- long %>%
spread(col, data)
#> spread: reorganized (col, data) into (am, carb, cyl, disp, drat, …) [was 352x3, now 32x12]
```

## Turning logging off, registering additional loggers

To turn off the output for just a particular function call, you can
Expand Down
11 changes: 5 additions & 6 deletions man/gather.Rd

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

14 changes: 7 additions & 7 deletions man/spread.Rd

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

0 comments on commit 2fabdc4

Please sign in to comment.