diff --git a/R/g_lineplot.R b/R/g_lineplot.R index c64302d466..c52e614472 100644 --- a/R/g_lineplot.R +++ b/R/g_lineplot.R @@ -10,6 +10,7 @@ #' * `x` (`character`)\cr name of x-axis variable. #' * `y` (`character`)\cr name of y-axis variable. #' * `strata` (`character`)\cr name of grouping variable, i.e. treatment arm. Can be `NA` to indicate lack of groups. +#' * `cohort_id` (`character`)\cr name of the variable that identifies group belonging. Only applies if `strata` is not NULL. #' * `paramcd` (`character`)\cr name of the variable for parameter's code. Used for y-axis label and plot's subtitle. #' Can be `NA` if `paramcd` is not to be added to the y-axis label or subtitle. #' * `y_unit` (`character`)\cr name of variable with units of `y`. Used for y-axis label and plot's subtitle. @@ -173,6 +174,7 @@ g_lineplot <- function(df, strata <- NULL # NULL if strata == NA or it is not in variables } else { strata <- variables[["strata"]] + cohort_id <- variables[["cohort_id"]] } checkmate::assert_flag(y_lab_add_paramcd, null.ok = TRUE) checkmate::assert_flag(subtitle_add_paramcd, null.ok = TRUE) @@ -216,7 +218,7 @@ g_lineplot <- function(df, if (!is.null(strata) && !is.null(alt_counts_df)) { strata_N <- paste0(strata, "_N") # nolint - df_N <- as.data.frame(table(alt_counts_df[[strata]], exclude = c(NA, NaN, Inf))) # nolint + df_N <- stats::aggregate(USUBJID ~ eval(parse(text = strata)), data = alt_counts_df, FUN = function(x) length(unique(x))) # nolint colnames(df_N) <- c(strata, "N") # nolint df_N[[strata_N]] <- paste0(df_N[[strata]], " (N = ", df_N$N, ")") # nolint @@ -471,6 +473,7 @@ h_format_row <- function(x, format, labels = NULL) { #' @param x (`character`)\cr x variable name. #' @param y (`character`)\cr y variable name. #' @param strata (`character` or `NA`)\cr strata variable name. +#' @param cohort_id (`character` or `NA`)\cr variable to identify subjects in cohorts. #' @param paramcd (`character` or `NA`)\cr `paramcd` variable name. #' @param y_unit (`character` or `NA`)\cr `y_unit` variable name. #' @@ -481,13 +484,14 @@ h_format_row <- function(x, format, labels = NULL) { #' control_lineplot_vars(strata = NA) #' #' @export -control_lineplot_vars <- function(x = "AVISIT", y = "AVAL", strata = "ARM", paramcd = "PARAMCD", y_unit = "AVALU") { +control_lineplot_vars <- function(x = "AVISIT", y = "AVAL", strata = "ARM", paramcd = "PARAMCD", y_unit = "AVALU", cohort_id = "USUBJID") { checkmate::assert_string(x) checkmate::assert_string(y) checkmate::assert_string(strata, na.ok = TRUE) + checkmate::assert_string(cohort_id, na.ok = TRUE) checkmate::assert_string(paramcd, na.ok = TRUE) checkmate::assert_string(y_unit, na.ok = TRUE) - variables <- c(x = x, y = y, strata = strata, paramcd = paramcd, y_unit = y_unit) + variables <- c(x = x, y = y, strata = strata, paramcd = paramcd, y_unit = y_unit, cohort_id = cohort_id) return(variables) } diff --git a/man/control_lineplot_vars.Rd b/man/control_lineplot_vars.Rd index 7d3200963b..f1100df4dd 100644 --- a/man/control_lineplot_vars.Rd +++ b/man/control_lineplot_vars.Rd @@ -9,7 +9,8 @@ control_lineplot_vars( y = "AVAL", strata = "ARM", paramcd = "PARAMCD", - y_unit = "AVALU" + y_unit = "AVALU", + cohort_id = "USUBJID" ) } \arguments{ @@ -22,6 +23,8 @@ control_lineplot_vars( \item{paramcd}{(\code{character} or \code{NA})\cr \code{paramcd} variable name.} \item{y_unit}{(\code{character} or \code{NA})\cr \code{y_unit} variable name.} + +\item{cohort_id}{(\code{character} or \code{NA})\cr variable to identify subjects in cohorts.} } \value{ A named character vector of variable names. diff --git a/man/g_lineplot.Rd b/man/g_lineplot.Rd index 1128182477..15c3e234f1 100644 --- a/man/g_lineplot.Rd +++ b/man/g_lineplot.Rd @@ -45,6 +45,7 @@ g_lineplot( \item \code{x} (\code{character})\cr name of x-axis variable. \item \code{y} (\code{character})\cr name of y-axis variable. \item \code{strata} (\code{character})\cr name of grouping variable, i.e. treatment arm. Can be \code{NA} to indicate lack of groups. +\item \code{cohort_id} (\code{character})\cr name of the variable that identifies group belonging. Only applies if \code{strata} is not NULL. \item \code{paramcd} (\code{character})\cr name of the variable for parameter's code. Used for y-axis label and plot's subtitle. Can be \code{NA} if \code{paramcd} is not to be added to the y-axis label or subtitle. \item \code{y_unit} (\code{character})\cr name of variable with units of \code{y}. Used for y-axis label and plot's subtitle.