Skip to content

Commit

Permalink
add_totals works on grouped_df
Browse files Browse the repository at this point in the history
closes #97
  • Loading branch information
sfirke committed Mar 19, 2017
1 parent 55f93c9 commit c326e44
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion R/add_totals.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@


add_totals <- function(dat, which = c("row", "col"), fill = "-", na.rm = TRUE){
# clean_dat <- clean_names(dat) # bad names will make select_if choke
if("grouped_df" %in% class(dat)){ dat <- dplyr::ungroup(dat) } # grouped_df causes problems, #97

dat[[1]] <- as.character(dat[[1]]) # for type matching when binding the word "Total" on a factor. Moved up to this line so that if only 1st col is numeric, the function errors
if(sum(unlist(lapply(dat, is.numeric))) == 0){stop("data.frame must contain at least one column of class numeric")}

Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-add-totals.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ test_that("numeric first column is ignored", {
stringsAsFactors = FALSE))
})

test_that("grouped_df gets ungrouped and succeeds", {
ct <- mtcars %>% group_by(cyl, gear) %>% tally() %>% tidyr::spread(gear, n)
expect_equal(ct %>% add_totals(),
ct %>% ungroup() %>% add_totals
)
})

test_that("na.rm value gets passed through", {

})
Expand Down

0 comments on commit c326e44

Please sign in to comment.