Skip to content

Commit

Permalink
Replace magrittr pipe with base R pipe (#23)
Browse files Browse the repository at this point in the history
* Remove the magrittr pipe from the code and documentation

* Remove magrittr from DESCRIPTION and NAMESPACE

* Correct test errors when calling internal function statascii()

* Add a NEWS item

---------

Co-authored-by: Gustavo Velásquez <gvelasq2github@gmail.com>
  • Loading branch information
askawron and gvelasq authored Apr 20, 2024
1 parent 73e8a8e commit 1bc0508
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 117 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Imports:
dplyr,
grDevices,
here,
magrittr,
purrr,
readr,
rlang (>= 0.2.0),
Expand Down
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export("%gin%")
export(br)
export(ftab)
export(ta)
export(tab)
export(tab1)
export(tab2)
importFrom(magrittr,"%>%")
importFrom(rlang,"%|%")
importFrom(rlang,":=")
importFrom(rlang,.data)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# tidytab (development version)

* Replaced magrittr pipe with base R pipe (#18, @askawron).
* Updated usethis helpers to usethis 2.2.3 (#17).
* Added an R-universe badge to README (#14).

Expand Down
16 changes: 8 additions & 8 deletions R/tab.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,31 @@
#'
#' @examples
#' # one-way table of frequencies
#' mtcars %>% tab(cyl)
#' mtcars |> tab(cyl)
#'
#' # two-way table of frequencies (a special 2x2 contingency table)
#' mtcars %>% tab(cyl, gear)
#' mtcars |> tab(cyl, gear)
#'
#' # flat contingency tables of three (or more) variables
#' mtcars %>% tab(cyl, gear, am)
#' mtcars |> tab(cyl, gear, am)
#'
#' # tables wider than the R console are automatically wrapped
#' mtcars %>% tab(cyl, gear, am, vs)
#' mtcars |> tab(cyl, gear, am, vs)
#'
#' # missing values are displayed in red
#' tab(letters[24:27])
#'
#' # ftab() displays only flat contingency tables (here, with two variables)
#' mtcars %>% ftab(cyl, gear)
#' mtcars |> ftab(cyl, gear)
#'
#' # tab1() displays one-way tables for each variable
#' mtcars %>% tab1(cyl, gear)
#' mtcars |> tab1(cyl, gear)
#'
#' # tab2() displays two-way tables for all variable combinations
#' mtcars %>% tab2(cyl, gear, am)
#' mtcars |> tab2(cyl, gear, am)
#'
#' # ta() is a shortened alias for tab(), inspired by Stata
#' mtcars %>% ta(gear)
#' mtcars |> ta(gear)
#'
#' @aliases
#' ta
Expand Down
11 changes: 0 additions & 11 deletions R/utils-pipe.R

This file was deleted.

17 changes: 8 additions & 9 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,34 @@ devtools::install_github("gvelasq/tidytab")
## Usage

```{r example, message = FALSE}
# setup (tidytab automatically exports the magrittr pipe)
library(tidytab)
# one-way table of frequencies
mtcars %>% tab(cyl)
mtcars |> tab(cyl)
# two-way table of frequencies (a special 2x2 contingency table)
mtcars %>% tab(cyl, gear)
mtcars |> tab(cyl, gear)
# flat contingency tables of three (or more) variables
mtcars %>% tab(cyl, gear, am)
mtcars |> tab(cyl, gear, am)
# tables wider than the R console are automatically wrapped
mtcars %>% tab(cyl, gear, am, vs)
mtcars |> tab(cyl, gear, am, vs)
# missing values are displayed in red
tab(letters[24:27])
# ftab() displays only flat contingency tables (here, with two variables)
mtcars %>% ftab(cyl, gear)
mtcars |> ftab(cyl, gear)
# tab1() displays one-way tables for each variable
mtcars %>% tab1(cyl, gear)
mtcars |> tab1(cyl, gear)
# tab2() displays two-way tables for all variable combinations
mtcars %>% tab2(cyl, gear, am)
mtcars |> tab2(cyl, gear, am)
# ta() is a shortened alias for tab(), inspired by Stata
mtcars %>% ta(gear)
mtcars |> ta(gear)
```

## Code of Conduct
Expand Down
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ devtools::install_github("gvelasq/tidytab")
## Usage

``` r
# setup (tidytab automatically exports the magrittr pipe)
library(tidytab)

# one-way table of frequencies
mtcars %>% tab(cyl)
mtcars |> tab(cyl)
#> cyl │ Freq. Percent Cum.
#> ────────────┼───────────────────────────────────
#> 4 │ 11 34.4 34.4
Expand All @@ -44,7 +43,7 @@ mtcars %>% tab(cyl)
#> Total │ 32 100.0  

# two-way table of frequencies (a special 2x2 contingency table)
mtcars %>% tab(cyl, gear)
mtcars |> tab(cyl, gear)
#> │ gear │
#> cyl │ 3 4 5 │ Total
#> ───────────┼─────────────────────────────────┼──────────
Expand All @@ -55,7 +54,7 @@ mtcars %>% tab(cyl, gear)
#> Total │ 15 12 5 │ 32

# flat contingency tables of three (or more) variables
mtcars %>% tab(cyl, gear, am)
mtcars |> tab(cyl, gear, am)
#> cyl │ gear am Freq. Percent Cum.
#> ────────────┼───────────────────────────────────────────────────────────
#> 4 │ 3 0 1 3.1 3.1
Expand All @@ -72,7 +71,7 @@ mtcars %>% tab(cyl, gear, am)
#> 8 │ 5 1 2 6.2 99.7

# tables wider than the R console are automatically wrapped
mtcars %>% tab(cyl, gear, am, vs)
mtcars |> tab(cyl, gear, am, vs)
#> cyl │ gear am vs Freq. Percent
#> ────────────┼───────────────────────────────────────────────────────────
#> 4 │ 3 0 1 1 3.1
Expand Down Expand Up @@ -117,7 +116,7 @@ tab(letters[24:27])
#> Total │ 4 100.0  

# ftab() displays only flat contingency tables (here, with two variables)
mtcars %>% ftab(cyl, gear)
mtcars |> ftab(cyl, gear)
#> cyl │ gear Freq. Percent Cum.
#> ────────────┼───────────────────────────────────────────────
#> 4 │ 3 1 3.1 3.1
Expand All @@ -132,7 +131,7 @@ mtcars %>% ftab(cyl, gear)
#> 8 │ 5 2 6.2 99.8

# tab1() displays one-way tables for each variable
mtcars %>% tab1(cyl, gear)
mtcars |> tab1(cyl, gear)
#> cyl │ Freq. Percent Cum.
#> ────────────┼───────────────────────────────────
#> 4 │ 11 34.4 34.4
Expand All @@ -150,7 +149,7 @@ mtcars %>% tab1(cyl, gear)
#> Total │ 32 100.0  

# tab2() displays two-way tables for all variable combinations
mtcars %>% tab2(cyl, gear, am)
mtcars |> tab2(cyl, gear, am)
#> │ gear │
#> cyl │ 3 4 5 │ Total
#> ───────────┼─────────────────────────────────┼──────────
Expand Down Expand Up @@ -179,7 +178,7 @@ mtcars %>% tab2(cyl, gear, am)
#> Total │ 19 13 │ 32

# ta() is a shortened alias for tab(), inspired by Stata
mtcars %>% ta(gear)
mtcars |> ta(gear)
#> gear │ Freq. Percent Cum.
#> ────────────┼───────────────────────────────────
#> 3 │ 15 46.9 46.9
Expand Down
12 changes: 0 additions & 12 deletions man/pipe.Rd

This file was deleted.

16 changes: 8 additions & 8 deletions man/tab.Rd

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

50 changes: 25 additions & 25 deletions tests/create-sample-data/create-sample-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,40 @@ library(utils)

# 1. demonstrate 'oneway' flavor for one-way tables of frequencies ----
a <-
mtcars %>%
count(gear) %>%
rename(Freq. = n) %>%
mtcars |>
count(gear) |>
rename(Freq. = n) |>
mutate(gear = as.character(gear))
a <-
a %>%
a |>
add_row(gear = "Total", Freq. = sum(a[, 2]))
table_a <-
capture.output(tidytab:::statascii(a, flavor = "oneway"))

# 2. demonstrate 'oneway' flavor with no padding ----
b <-
mtcars %>%
count(gear) %>%
rename(Freq. = n) %>%
mtcars |>
count(gear) |>
rename(Freq. = n) |>
mutate(gear = as.character(gear))
b <-
b %>%
b |>
add_row(gear = "Total", Freq. = sum(b[, 2]))
table_b <-
capture.output(tidytab:::statascii(b, flavor = "oneway", padding = "none"))

# 3. demonstrate 'twoway' flavor for n-way tables of frequencies ----
c <-
mtcars %>%
count(gear, carb, am) %>%
rename(Freq. = n) %>%
mtcars |>
count(gear, carb, am) |>
rename(Freq. = n) |>
mutate(
gear = as.character(gear),
carb = as.character(carb),
am = as.character(am)
)
c <-
c %>%
c |>
add_row(
gear = "Total",
carb = "",
Expand All @@ -57,16 +57,16 @@ table_c <-

# 4. demonstrate 'twoway' flavor with dashed group separator ----
d <-
mtcars %>%
count(gear, carb, am) %>%
rename(Freq. = n) %>%
mtcars |>
count(gear, carb, am) |>
rename(Freq. = n) |>
mutate(
gear = as.character(gear),
carb = as.character(carb),
am = as.character(am)
)
d <-
d %>%
d |>
add_row(
gear = "Total",
carb = "",
Expand All @@ -78,8 +78,8 @@ table_d <-

# 5. demonstrate 'summary' flavor for summary statistics ----
e <-
mtcars %>%
group_by(gear) %>%
mtcars |>
group_by(gear) |>
summarize(
Obs = n(),
Mean = mean(gear),
Expand All @@ -92,14 +92,14 @@ table_e <-

# 6. demonstrate wrapping feature for wide tables ----
f <-
mtcars %>%
mtcars |>
mutate(
cyl2 = cyl,
vs2 = vs,
am2 = am,
carb2 = carb
) %>%
filter(gear != 5) %>%
) |>
filter(gear != 5) |>
count(
gear,
carb,
Expand All @@ -110,12 +110,12 @@ f <-
am2,
vs2,
cyl2
) %>%
rename(Freq. = n) %>%
mutate(gear = as.character(gear)) %>%
) |>
rename(Freq. = n) |>
mutate(gear = as.character(gear)) |>
ungroup()
f <-
f %>%
f |>
add_row(gear = "Total", Freq. = sum(f[, 10]))
f[is.na(f)] <- ""
options("width" = 80)
Expand Down
Loading

0 comments on commit 1bc0508

Please sign in to comment.