Skip to content

Commit

Permalink
add fct_yesno()
Browse files Browse the repository at this point in the history
fixes #19
  • Loading branch information
DanChaltiel committed Mar 31, 2024
1 parent 6604e81 commit 4b4a315
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export(edc_peek_options)
export(edc_reset_options)
export(edc_swimmerplot)
export(extend_lookup)
export(fct_yesno)
export(find_keyword)
export(get_common_cols)
export(get_datasets)
export(get_key_cols)
export(get_lookup)
export(get_yesno_lvl)
export(load_as_list)
export(load_list)
export(manual_correction)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ EDCimport is a package designed to easily import data from EDC software TrialMas

### New features

- New function `fct_yesno()`, to easily format Yes/No columns.

- New function `save_plotly()`, to save a `plotly` to an HTML file

- New experimental function `get_common_cols()` that might become useful to find keys to pivot or summarise data.
Expand Down
58 changes: 58 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,64 @@ assert_no_duplicate = function(df, id_col=get_key_cols()){
df
}

#' Format factor levels as Yes/No
#'
#' Format factor levels as arbitrary values of Yes/No (with Yes always first) while leaving untouched all vectors that contain other information.
#' Default level values can be set through options, which is best done before calling [read_trialmaster()].
#' Helper `get_yesno_lvl()` is a helper to provide default TM levels.
#'
#' @param x a vector of any type/class
#' @param lvl list of values to be considered as Yes/No values. See example.
#' @param mutate_character whether to turn characters into factor
#'
#' @return a factor, or `x` untouched
#' @export
#'
#' @examples
#' set.seed(42)
#' x = tibble(a=sample(c("Yes", "No"), size=20, replace=T),
#' b=sample(c("1-Yes", "0-No"), size=20, replace=T),
#' c=sample(c("Oui", "Non"), size=20, replace=T),
#' x=sample(0:1, size=20, replace=T),
#' y=1:20)
#'
#' # leave untouched unhandled vectors (c,x, and y)
#' x %>% purrr::iwalk(~{
#' cat("--- Levels of ", .y, " ---\n")
#' print(.x %>% factor() %>% levels())
#' print(.x %>% fct_yesno() %>% levels())
#' })
#'
#' #add other levels
#' supp_levels = list(c("Oui", "Non"), c("Ja", "Nein"))
#' options(edc_fct_yesno = get_yesno_lvl(supp_levels))
#' x %>% purrr::iwalk(~{
#' cat("--- Levels of ", .y, " ---\n")
#' print(.x %>% factor() %>% levels())
#' print(.x %>% fct_yesno() %>% levels())
#' })
fct_yesno = function(x, lvl=getOption("edc_fct_yesno", get_yesno_lvl()),
mutate_character=TRUE){
if(!is.factor(x) && !is.character(x)) return(x)
if(is.character(x) && isFALSE(mutate_character)) return(x)
lvls = lvl %>% keep(~all(x %in% union(.x, NA)))
if(length(lvls)>1) cli_abort("Duplicated Yes/No levels")
if(length(lvls)==0) return(x)
factor(x, levels=lvls[[1]]) %>% copy_label_from(x)
}


#' @describeIn fct_yesno
#' @export
get_yesno_lvl = function(add, keep_default=TRUE) {
default = list(c("Yes", "No"), c("1-Yes", "0-No"))
if(missing(add)) return(default)
if(isFALSE(keep_default)) return(add)
if(!is.list(add)) add = list(add)
c(default, add)
}




# Manual correction ---------------------------------------------------------------------------
Expand Down
54 changes: 54 additions & 0 deletions man/fct_yesno.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,27 @@ test_that("check_subjid works", {
check_subjid(1:52) %>% expect_warning(class="edc_check_subjid_additional")
})



test_that("fct_yesno works", {
set.seed(42)
x = tibble(a=sample(c("Yes", "No"), size=20, replace=T),
b=sample(c("1-Yes", "0-No"), size=20, replace=T),
c=sample(c("Oui", "Non"), size=20, replace=T),
x=sample(0:1, size=20, replace=T),
y=1:20)
edc_reset_options(quiet=TRUE)
x$a %>% factor() %>% levels() %>% expect_equal(c("No", "Yes"))
x$a %>% fct_yesno() %>% levels() %>% expect_equal(c("Yes", "No"))

x$b %>% factor() %>% levels() %>% expect_equal(c("0-No", "1-Yes"))
x$b %>% fct_yesno() %>% levels() %>% expect_equal(c("1-Yes", "0-No"))

x$c %>% factor() %>% levels() %>% expect_equal(c("Non", "Oui"))
x$c %>% fct_yesno() %>% levels() %>% expect_null()

supp_levels = list(c("Oui", "Non"), c("Ja", "Nein"))
local_options(edc_fct_yesno = get_yesno_lvl(supp_levels))
x$c %>% factor() %>% levels() %>% expect_equal(c("Non", "Oui"))
x$c %>% fct_yesno() %>% levels() %>% expect_equal(c("Oui", "Non"))
})

0 comments on commit 4b4a315

Please sign in to comment.