diff --git a/DESCRIPTION b/DESCRIPTION index 38a39ee..105c517 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,7 +17,6 @@ Imports: dplyr, grDevices, here, - magrittr, purrr, readr, rlang (>= 0.2.0), diff --git a/NAMESPACE b/NAMESPACE index 6b08291..f733c74 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,5 @@ # Generated by roxygen2: do not edit by hand -export("%>%") export("%gin%") export(br) export(ftab) @@ -8,7 +7,6 @@ export(ta) export(tab) export(tab1) export(tab2) -importFrom(magrittr,"%>%") importFrom(rlang,"%|%") importFrom(rlang,":=") importFrom(rlang,.data) diff --git a/NEWS.md b/NEWS.md index f182959..ee4384f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/tab.R b/R/tab.R index d84ebe3..2cc9fed 100644 --- a/R/tab.R +++ b/R/tab.R @@ -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 diff --git a/R/utils-pipe.R b/R/utils-pipe.R deleted file mode 100644 index fb8c818..0000000 --- a/R/utils-pipe.R +++ /dev/null @@ -1,11 +0,0 @@ -#' Pipe operator -#' -#' See \code{magrittr::\link[magrittr]{\%>\%}} for details. -#' -#' @name %>% -#' @rdname pipe -#' @keywords internal -#' @export -#' @importFrom magrittr %>% -#' @usage lhs \%>\% rhs -NULL diff --git a/README.Rmd b/README.Rmd index 1835bfd..3e1602a 100644 --- a/README.Rmd +++ b/README.Rmd @@ -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 diff --git a/README.md b/README.md index 9b8f539..b320749 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 #> ───────────┼─────────────────────────────────┼────────── @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 #> ───────────┼─────────────────────────────────┼────────── @@ -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 diff --git a/man/pipe.Rd b/man/pipe.Rd deleted file mode 100644 index b7daf6a..0000000 --- a/man/pipe.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils-pipe.R -\name{\%>\%} -\alias{\%>\%} -\title{Pipe operator} -\usage{ -lhs \%>\% rhs -} -\description{ -See \code{magrittr::\link[magrittr]{\%>\%}} for details. -} -\keyword{internal} diff --git a/man/tab.Rd b/man/tab.Rd index 1cb5a4b..0f7e731 100644 --- a/man/tab.Rd +++ b/man/tab.Rd @@ -46,31 +46,31 @@ The invisibly returned tibble excludes total rows and columns to avoid collision } \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) } \seealso{ diff --git a/tests/create-sample-data/create-sample-data.R b/tests/create-sample-data/create-sample-data.R index 8a9543c..25243c5 100644 --- a/tests/create-sample-data/create-sample-data.R +++ b/tests/create-sample-data/create-sample-data.R @@ -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 = "", @@ -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 = "", @@ -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), @@ -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, @@ -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) diff --git a/tests/testthat/test-statascii.R b/tests/testthat/test-statascii.R index 50d74bd..6e0983d 100644 --- a/tests/testthat/test-statascii.R +++ b/tests/testthat/test-statascii.R @@ -10,22 +10,22 @@ context("test-statascii.R") tables <- read_rds("sample-tables.rds") test_that("data frame must have at least two columns", { - expect_error(statascii(as.data.frame(letters[1:3]))) + expect_error(tidytab:::statascii(as.data.frame(letters[1:3]))) }) test_that("data frame must have at least three columns for 'twoway' flavor", { - expect_error(statascii(as.data.frame(letters[1:3]), flavor = "twoway")) + expect_error(tidytab:::statascii(as.data.frame(letters[1:3]), flavor = "twoway")) }) test_that("oneway flavor works", { skip_on_os("windows") 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])) expect_equal( tables[[1]], @@ -38,17 +38,17 @@ test_that("oneway flavor works", { test_that("oneway flavor with no padding works", { skip_on_os("windows") 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])) expect_equal( tables[[2]], ansi_strip( - capture.output(statascii(b, flavor = "oneway", padding = "none")) + capture.output(tidytab:::statascii(b, flavor = "oneway", padding = "none")) ) ) }) @@ -56,16 +56,16 @@ test_that("oneway flavor with no padding works", { test_that("twowway flavor works for 3-way table", { skip_on_os("windows") 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 = "", @@ -75,7 +75,7 @@ test_that("twowway flavor works for 3-way table", { expect_equal( tables[[3]], ansi_strip( - capture.output(statascii(c, flavor = "twoway")) + capture.output(tidytab:::statascii(c, flavor = "twoway")) ) ) }) @@ -83,16 +83,16 @@ test_that("twowway flavor works for 3-way table", { test_that("twoway flavor works with dashed group separators", { skip_on_os("windows") 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 = "", @@ -102,7 +102,7 @@ test_that("twoway flavor works with dashed group separators", { expect_equal( tables[[4]], ansi_strip( - capture.output(statascii(d, flavor = "twoway", separators = TRUE)) + capture.output(tidytab:::statascii(d, flavor = "twoway", separators = TRUE)) ) ) }) @@ -110,8 +110,8 @@ test_that("twoway flavor works with dashed group separators", { test_that("summary flavor with summary padding works", { skip_on_os("windows") e <- - mtcars %>% - group_by(gear) %>% + mtcars |> + group_by(gear) |> summarize( Obs = n(), Mean = mean(gear), @@ -122,21 +122,21 @@ test_that("summary flavor with summary padding works", { expect_equal( tables[[5]], ansi_strip( - capture.output(statascii(e, flavor = "summary", padding = "summary")) + capture.output(tidytab:::statascii(e, flavor = "summary", padding = "summary")) ) ) }) test_that("wrap_tbl() works", { skip_on_os("windows") - f <- mtcars %>% + f <- mtcars |> mutate( cyl2 = cyl, vs2 = vs, am2 = am, carb2 = carb - ) %>% - filter(gear != 5) %>% + ) |> + filter(gear != 5) |> count( gear, carb, @@ -147,17 +147,17 @@ test_that("wrap_tbl() works", { am2, vs2, cyl2 - ) %>% - rename(Freq. = n) %>% - mutate(gear = as.character(gear)) %>% + ) |> + rename(Freq. = n) |> + mutate(gear = as.character(gear)) |> ungroup() - f <- f %>% add_row(gear = "Total", Freq. = sum(f[, 10])) + f <- f |> add_row(gear = "Total", Freq. = sum(f[, 10])) f[is.na(f)] <- "" options("width" = 80) expect_equal( tables[[6]], ansi_strip( - capture.output(statascii(f, flavor = "oneway", separators = TRUE)) + capture.output(tidytab:::statascii(f, flavor = "oneway", separators = TRUE)) ) ) })