Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function to cleanly convert Params to Labels. #1791

Merged
merged 9 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method(validate_lParamLabels,"NULL")
S3method(validate_lParamLabels,character)
S3method(validate_lParamLabels,list)
export("%>%")
export(":=")
export(.data)
Expand All @@ -22,6 +25,8 @@ export(MakeBounds)
export(MakeCharts)
export(MakeLongMeta)
export(MakeMetric)
export(MakeParamLabels)
export(MakeParamLabelsList)
export(MakeWideGroups)
export(MakeWorkflowList)
export(Make_Timeline)
Expand Down
1 change: 0 additions & 1 deletion R/Report_Setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#' - `StudyID` (character): The study ID.
#' - `red_kris` (numeric): The number of red flags.
#' - `amber_kris` (numeric): The number of amber flags.
#'

Report_Setup <- function(dfGroups = NULL, dfMetrics = NULL, dfResults = NULL) {
output <- list()
Expand Down
31 changes: 17 additions & 14 deletions R/Report_StudyInfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#'
#' @keywords internal


Report_StudyInfo <- function(
lStudy,
lStudyLabels = NULL
Expand All @@ -30,23 +29,27 @@ Report_StudyInfo <- function(
)
}

study_status_table <- lStudy %>% imap_dfr(function(value, param) {
data.frame(
Description = ifelse(
param %in% names(lStudyLabels),
lStudyLabels[[param]],
param
),
Value = ifelse(
is.na(value),
value,
prettyNum(value, drop0trailing = TRUE)
lLabels <- MakeParamLabelsList(names(lStudy), lStudyLabels)
dfLabels <- data.frame(
Param = names(lLabels),
Description = unname(unlist(lLabels))
)

study_status_table <- data.frame(
Param = names(lStudy),
Value = unname(unlist(lStudy))
) %>%
dplyr::left_join(dfLabels, by = "Param") %>%
dplyr::select(-"Param") %>%
dplyr::mutate(
Value = dplyr::if_else(
is.na(.data$Value),
.data$Value,
prettyNum(.data$Value, drop0trailing = TRUE)
)
)
})

show_table <- study_status_table %>%
slice(1:5) %>%
gsm_gt(id = "study_table")

hide_table <- study_status_table %>%
Expand Down
1 change: 1 addition & 0 deletions R/aaa-shared.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gloss_extra <- function(x) {
#' @param dfGroups `r gloss_param("dfGroups")`
#' @param dfInput `r gloss_param("dfInput")`
#' @param lMetric `r gloss_param("lMetric")`
#' @param lParamLabels `r gloss_param("lParamLabels")`
#' @param bDebug `r gloss_param("bDebug")`
#'
#'
Expand Down
77 changes: 77 additions & 0 deletions R/util-MakeParamLabels.R
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$Params))
jonthegeek marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @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
)
}
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ reference:
- MakeCharts
- MakeMetric
- MakeLongMeta
- MakeParamLabels
- MakeParamLabelsList
- MakeWideGroups
- MakeWorkflowList
- ParseThreshold
Expand Down
41 changes: 41 additions & 0 deletions man/MakeParamLabels.Rd

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

3 changes: 3 additions & 0 deletions man/glossary/lParamLabels.yaml
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.
2 changes: 2 additions & 0 deletions man/shared-params.Rd

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

1 change: 0 additions & 1 deletion tests/testthat/test-Report_StudyInfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ test_that("Uses default study labels when lStudyLabels is NULL", {
expect_true(any(grepl("Study Status", output)))
expect_true(any(grepl("Show Details", output)))
expect_true(any(grepl("Unique Study ID", output)))
expect_true(any(grepl("protocol_title", output)))
})

test_that("Uses custom study labels when lStudyLabels is provided", {
Expand Down
126 changes: 126 additions & 0 deletions tests/testthat/test-util-MakeParamLabels.R
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"
)
)
})