Skip to content

Commit

Permalink
update g_lineplot code to handle special characters (#1230)
Browse files Browse the repository at this point in the history
update g_lineplot code to handle special characters in group_var. add
test for maintaining factor levels.

related to #1212

---------

Signed-off-by: Abinaya Yogasekaram <73252787+ayogasekaram@users.noreply.github.com>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ayogasekaram and github-actions[bot] authored Apr 26, 2024
1 parent c179156 commit fc9bfbe
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# tern 0.9.4.9002

### Enhancements
* Added `facet_var` to `g_lineplot` to allow plot faceting by a factor variable.
* Updated `g_lineplot` legend to follow factor levels set by users.
* Added examples and tests for `label_all` parameter to `extract_survival_biomarkers` and `extract_survival_subgroups`.

### Miscellaneous
Expand Down
20 changes: 13 additions & 7 deletions R/g_lineplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,15 @@ g_lineplot <- function(df,
df_N[[strata_N]] <- paste0(df_N[[group_var]], " (N = ", df_N$N, ")") # nolint

# keep strata factor levels
matches <- sapply(unique(df_N[[group_var]]),
function(x) unique(df_N[[paste0(group_var, "_N")]]) #nolint
[grepl(paste0("^", x), unique(df_N[[paste0(group_var, "_N")]]))])
df_N[[paste0(group_var, "_N")]] <- factor(df_N[[group_var]]) #nolint
levels(df_N[[paste0(group_var, "_N")]]) <- unlist(matches) #nolint
matches <- sapply(unique(df_N[[group_var]]), function(x) {
regex_pattern <- gsub("([][(){}^$.|*+?\\\\])", "\\\\\\1", x)
unique(df_N[[paste0(group_var, "_N")]])[grepl(
paste0("^", regex_pattern),
unique(df_N[[paste0(group_var, "_N")]])
)]
})
df_N[[paste0(group_var, "_N")]] <- factor(df_N[[group_var]]) # nolint
levels(df_N[[paste0(group_var, "_N")]]) <- unlist(matches) # nolint

# strata_N should not be in colnames(df_stats)
checkmate::assert_disjunct(strata_N, colnames(df_stats))
Expand Down Expand Up @@ -551,7 +555,9 @@ control_lineplot_vars <- function(x = "AVISIT",
checkmate::assert_string(paramcd, na.ok = TRUE, null.ok = TRUE)
checkmate::assert_string(y_unit, na.ok = TRUE, null.ok = TRUE)

variables <- c(x = x, y = y, group_var = group_var, paramcd = paramcd,
y_unit = y_unit, subject_var = subject_var, facet_var = facet_var)
variables <- c(
x = x, y = y, group_var = group_var, paramcd = paramcd,
y_unit = y_unit, subject_var = subject_var, facet_var = facet_var
)
return(variables)
}
Loading

0 comments on commit fc9bfbe

Please sign in to comment.