Skip to content

Commit

Permalink
handle kooky variable names input to crosstab and tabyl
Browse files Browse the repository at this point in the history
closes #87
  • Loading branch information
sfirke committed Jan 8, 2017
1 parent e459789 commit d3dd992
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
4 changes: 3 additions & 1 deletion R/crosstab.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ crosstab.default <- function(vec1, vec2, percent = "none", show_na = TRUE, ...){
var_name <- names(vec1)
}

# an odd variable name can be deparsed into a vector of length >1, rare but breaks function, see issue #87
if(length(var_name) > 1){ var_name <- paste(var_name, collapse = "") }

if(!show_na){
dat <- dat[!is.na(dat[[1]]) & !is.na(dat[[2]]), ]
Expand All @@ -91,7 +93,7 @@ crosstab.default <- function(vec1, vec2, percent = "none", show_na = TRUE, ...){

# calculate percentages, if specified
if(percent != "none"){result <- ns_to_percents(result, denom = percent)}

result %>%
stats::setNames(., c(var_name, names(.)[-1])) %>%
data.frame(., check.names = FALSE)
Expand Down
3 changes: 3 additions & 0 deletions R/tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ tabyl.default <- function(vec, sort = FALSE, show_na = TRUE, ...) {
} else {
var_name <- names(vec)
}

# an odd variable name can be deparsed into a vector of length >1, rare but throws warning, see issue #87
if(length(var_name) > 1){ var_name <- paste(var_name, collapse = "") }

# calculate initial counts table
# convert vector to a 1 col data.frame
Expand Down
17 changes: 16 additions & 1 deletion tests/testthat/test-crosstab.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,19 @@ test_that("bad input variable name is preserved", {
k <- mtcars %>% mutate(`bad name` = cyl)
expect_equal(crosstab(k$`bad name`, k$gear) %>% names %>% .[[1]],
"k$`bad name`")
})
})

test_that("bizarre combination of %>%, quotes, and spaces in names is handled", {
dat <- data.frame(
`The candidate(s) applied directly to my school` = c("a", "b", "a", "b"),
x = 1:4,
check.names = FALSE,
stringsAsFactors = FALSE
)

expect_equal(
crosstab(dat$`The candidate(s) applied directly to my school` %>% gsub("hi", "there", .), dat$x) %>%
names() %>% .[1],
"dat$`The candidate(s) applied directly to my school` %>% gsub(\"hi\", \"there\", .)"
)
})
16 changes: 15 additions & 1 deletion tests/testthat/test-tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,18 @@ test_that("input variable names 'percent' and 'n' are handled", {
n_n = c(18, 7),
percent = c(18/25, 7/25))
)
})
})

test_that("bizarre combination of %>%, quotes, and spaces in names is handled", {
dat <- data.frame(
`The candidate(s) applied directly to my school` = c("a", "b", "a", "b"),
check.names = FALSE,
stringsAsFactors = FALSE
)

expect_equal(
tabyl(dat$`The candidate(s) applied directly to my school` %>% gsub("hi", "there", .)) %>%
names() %>% .[1],
"dat$`The candidate(s) applied directly to my school` %>% gsub(\"hi\", \"there\", .)"
)
})

0 comments on commit d3dd992

Please sign in to comment.