Skip to content

Commit

Permalink
tabyl handles grouped data.frame inputs
Browse files Browse the repository at this point in the history
closes #125
  • Loading branch information
sfirke committed Jul 27, 2017
1 parent e20dbef commit c7dc39c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion R/tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ tabyl.default <- function(dat, show_na = TRUE, ...) {
tabyl.data.frame <- function(dat, var1, var2, var3, show_na = TRUE, ...){
if("data.frame" %in% class(dat) &
missing(var1) & missing(var2) & missing(var3)){stop("if calling on a data.frame, specify unquoted column names(s) to tabulate. Did you mean to call tabyl() on a vector?")}
# TODO: check that variable names are present in data.frame
if(dplyr::is_grouped_df(dat)){ dat <- dplyr::ungroup(dat) }

if(missing(var2) & missing(var3)){
tabyl_1way(dat, rlang::enquo(var1), show_na = show_na)
} else if(missing(var3)){
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ test_that("bizarre combination of %>%, quotes, and spaces in names is handled",
)
})

test_that("grouped data.frame inputs are handled (#125)", {
expect_equal(mtcars %>% group_by(cyl) %>% tabyl(carb, gear),
mtcars %>% tabyl(carb, gear))
})

test_that("if called on non-existent vector, returns useful error message", {
expect_error(tabyl(mtcars$moose), "object mtcars\\$moose not found")
expect_error(tabyl(moose), "object 'moose' not found")
expect_error(mtcars %>% tabyl(moose), "object 'moose' not found")
})

0 comments on commit c7dc39c

Please sign in to comment.