-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1791 from Gilead-BioStats/fix-1790
Add a function to cleanly convert Params to Labels.
- Loading branch information
Showing
11 changed files
with
274 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#' Create Labels for Parameters | ||
#' | ||
#' @description `r lifecycle::badge("stable")` | ||
#' | ||
#' Convert a vector of parameters to labels in Title Case. `MakeParamLabels` | ||
#' adds a `Labels` column to a `data.frame` that has a `Params` column (such | ||
#' as `dfGroups`), while `MakeParamLabelsList` returns just the list of named | ||
#' parameters. | ||
#' | ||
#' @inheritParams shared-params | ||
#' | ||
#' @return `dfGroups` with an added `Label` column, or a list of labeled | ||
#' parameters. | ||
#' | ||
#' @examples | ||
#' head(gsm::reportingGroups) | ||
#' MakeParamLabels(head(gsm::reportingGroups)) | ||
#' MakeParamLabels( | ||
#' head(gsm::reportingGroups), | ||
#' list(ParticipantCount = "Number of Participants") | ||
#' ) | ||
#' MakeParamLabelsList(head(gsm::reportingGroups$Param)) | ||
#' | ||
#' @export | ||
MakeParamLabels <- function(dfGroups, lParamLabels = NULL) { | ||
chrParams <- sort(unique(dfGroups$Param)) | ||
labels <- MakeParamLabelsList(chrParams, lParamLabels) | ||
dfLabels <- tibble::enframe(labels, name = "Param", value = "Label") | ||
dfLabels$Label <- unlist(dfLabels$Label) | ||
return(dplyr::left_join(dfGroups, dfLabels, by = "Param")) | ||
} | ||
|
||
#' @rdname MakeParamLabels | ||
#' @param chrParams A character vector of parameters, or a list that can be | ||
#' coerced to a character vector. | ||
#' @export | ||
MakeParamLabelsList <- function(chrParams, lParamLabels = NULL) { | ||
chrParams <- unlist(chrParams) | ||
lParamLabels <- validate_lParamLabels(lParamLabels) | ||
known_params <- intersect(chrParams, names(lParamLabels)) | ||
new_params <- setdiff(chrParams, names(lParamLabels)) | ||
labels <- setNames(as.list(chrParams), chrParams) | ||
labels[known_params] <- lParamLabels[known_params] | ||
labels[new_params] <- ParamToLabel(new_params) | ||
return(labels) | ||
} | ||
|
||
validate_lParamLabels <- function(lParamLabels) { | ||
UseMethod("validate_lParamLabels") | ||
} | ||
|
||
#' @export | ||
validate_lParamLabels.NULL <- function(lParamLabels) { | ||
return(NULL) | ||
} | ||
|
||
#' @export | ||
validate_lParamLabels.list <- function(lParamLabels) { | ||
if (length(lParamLabels) && rlang::is_named(lParamLabels)) { | ||
# Uniquify. | ||
return(lParamLabels[unique(names(lParamLabels))]) | ||
} | ||
return(NULL) | ||
} | ||
|
||
#' @export | ||
validate_lParamLabels.character <- function(lParamLabels) { | ||
return(validate_lParamLabels.list(as.list(lParamLabels))) | ||
} | ||
|
||
ParamToLabel <- function(chrParams) { | ||
stringr::str_replace_all(chrParams, "[^A-Za-z0-9]", " ") %>% | ||
stringr::str_replace_all("([[:lower:]])([[:upper:]])", "\\1 \\2") %>% | ||
stringr::str_replace_all( | ||
"(^| )([[:lower:]])", toupper | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class: list | ||
definition: > | ||
Labels for parameters, with the parameters as names, and the label as value. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
test_that("MakeParamLabels generates labels", { | ||
dfGroups <- data.frame(Param = c( | ||
"param", | ||
"Param", | ||
"aParam", | ||
"b param", | ||
"c_param", | ||
"paramID" | ||
)) | ||
dfGroupsMod <- MakeParamLabels(dfGroups) | ||
expect_identical( | ||
dfGroupsMod$Label, | ||
c( | ||
"Param", | ||
"Param", | ||
"A Param", | ||
"B Param", | ||
"C Param", | ||
"Param ID" | ||
) | ||
) | ||
}) | ||
|
||
test_that("MakeParamLabels uses supplied labels", { | ||
dfGroups <- data.frame(Param = c( | ||
"param", | ||
"Param", | ||
"aParam", | ||
"b param", | ||
"c_param", | ||
"paramID" | ||
)) | ||
dfGroupsMod <- MakeParamLabels( | ||
dfGroups, | ||
list("aParam" = "alpha parameter", "c_param" = "Chi Parameter") | ||
) | ||
expect_identical( | ||
dfGroupsMod$Label, | ||
c( | ||
"Param", | ||
"Param", | ||
"alpha parameter", | ||
"B Param", | ||
"Chi Parameter", | ||
"Param ID" | ||
) | ||
) | ||
}) | ||
|
||
test_that("MakeParamLabels works with character labels", { | ||
dfGroups <- data.frame(Param = c( | ||
"param", | ||
"Param", | ||
"aParam", | ||
"b param", | ||
"c_param", | ||
"paramID" | ||
)) | ||
dfGroupsMod <- MakeParamLabels( | ||
dfGroups, | ||
c("aParam" = "alpha parameter", "c_param" = "Chi Parameter") | ||
) | ||
expect_identical( | ||
dfGroupsMod$Label, | ||
c( | ||
"Param", | ||
"Param", | ||
"alpha parameter", | ||
"B Param", | ||
"Chi Parameter", | ||
"Param ID" | ||
) | ||
) | ||
}) | ||
|
||
test_that("MakeParamLabels ignores length-0 labels", { | ||
dfGroups <- data.frame(Param = c( | ||
"param", | ||
"Param", | ||
"aParam", | ||
"b param", | ||
"c_param", | ||
"paramID" | ||
)) | ||
dfGroupsMod <- MakeParamLabels( | ||
dfGroups, | ||
list() | ||
) | ||
expect_identical( | ||
dfGroupsMod$Label, | ||
c( | ||
"Param", | ||
"Param", | ||
"A Param", | ||
"B Param", | ||
"C Param", | ||
"Param ID" | ||
) | ||
) | ||
}) | ||
|
||
test_that("MakeParamLabels works when no labels overlap", { | ||
dfGroups <- data.frame(Param = c( | ||
"param", | ||
"Param", | ||
"aParam", | ||
"b param", | ||
"c_param", | ||
"paramID" | ||
)) | ||
dfGroupsMod <- MakeParamLabels( | ||
dfGroups, | ||
list(new = "nothing") | ||
) | ||
expect_identical( | ||
dfGroupsMod$Label, | ||
c( | ||
"Param", | ||
"Param", | ||
"A Param", | ||
"B Param", | ||
"C Param", | ||
"Param ID" | ||
) | ||
) | ||
}) |