diff --git a/DESCRIPTION b/DESCRIPTION index 7a93c01..09c0612 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: bhhitools Type: Package Title: Tools For BHHI R Users -Version: 0.6.0 +Version: 0.6.1 Authors@R: person("Eve", "Perry", email = "eve.perry@ucsf.edu", role = c("aut", "cre")) Description: A collection of functions to make R work at BHHI easier. Encoding: UTF-8 diff --git a/NEWS.md b/NEWS.md index 26305f6..a9dfa8a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# bhhitools 0.6.1 + +* Fixed bug with `bhhi_gt_crosstab()` and `bhhi_format_crosstab()` when the row variable name ended with "_" and 2-4 other characters. + # bhhitools 0.6.0 * Auto-convert variables with Stata value labels from [haven::labelled] to factors in `bhhi_crosstab()` and `bhhi_gt_crosstab()`. diff --git a/R/bhhi_format_crosstab.R b/R/bhhi_format_crosstab.R index 265c7a1..8214e38 100644 --- a/R/bhhi_format_crosstab.R +++ b/R/bhhi_format_crosstab.R @@ -88,7 +88,11 @@ detect_ci <- function(.data) { } get_column_name_stems <- function(.data) { - x <- stringr::str_extract(names(.data), "(.*)_.{2,4}$", group = 1) + column_names = names(.data) + # first column is the row var so remove it + column_names = column_names[2:length(column_names)] + + x <- stringr::str_extract(column_names, "(.*)_.{2,4}$", group = 1) unique(x[!is.na(x)]) } diff --git a/tests/testthat/test-bhhi_format_crosstab.R b/tests/testthat/test-bhhi_format_crosstab.R index 6af31a2..95eba6e 100644 --- a/tests/testthat/test-bhhi_format_crosstab.R +++ b/tests/testthat/test-bhhi_format_crosstab.R @@ -16,6 +16,16 @@ test_that("bhhi_format_crosstab with ci works", { expect_gt_output("bhhi_format_crosstab_ci") }) +test_that("bhhi_format_crosstab with ci & 2-4 letter suffix name works", { + create_test_svy_tbl() |> + srvyr::rename(race_cat = race) |> + srvyr::group_by(race_cat, gender) |> + bhhi_cascade(srvyr::survey_prop(vartype = "ci", proportion = TRUE)) |> + bhhi_reshape_crosstab(race_cat, gender) |> + bhhi_format_crosstab() |> + expect_gt_output("bhhi_format_crosstab_ci") +}) + test_that("bhhi_format_crosstab with no vartype works", { create_test_svy_tbl() |> srvyr::group_by(race, gender) |>