Skip to content

Commit

Permalink
Calculate density ranges based on treatment name and based on AVICI…
Browse files Browse the repository at this point in the history
…TCD for density plot (#308)

Fixes insightsengineering/goshawk#192

For proper density calculations, you need to calculate ranges within
groups inside the plot. Groups are created based on treatment variable
and based on AVICITCD plot. So the max ranges need to be calculated in
each group, and the max needs to be extracted.

> Set filters to: ADSL -> SEX -> Male, ADSL -> AGE -> 31.41, 35

# How it looked before

<img width="665" alt="image"
src="https://github.com/user-attachments/assets/344d408e-5357-4ebc-b037-7fa483b0e052">


# How it looks now

<img width="664" alt="image"
src="https://github.com/user-attachments/assets/d84fd7b1-6a06-43f9-8ff4-97310dcc629a">

# Example code

<details><summary> Example from `?tm_g_gh_density_distribution_plot`
</summary>

```r
pkgload::load_all("../teal.widgets")
pkgload::load_all("../teal")
pkgload::load_all(".")

data <- teal_data()
data <- within(data, {
  library(dplyr)
  library(stringr)
  
  # original ARM value = dose value
  arm_mapping <- list(
    "A: Drug X" = "150mg QD",
    "B: Placebo" = "Placebo",
    "C: Combination" = "Combination"
  )
  ADSL <- rADSL
  ADLB <- rADLB
  var_labels <- lapply(ADLB, function(x) attributes(x)$label)
  ADLB <- ADLB %>%
    mutate(
      AVISITCD = case_when(
        AVISIT == "SCREENING" ~ "SCR",
        AVISIT == "BASELINE" ~ "BL",
        grepl("WEEK", AVISIT) ~ paste("W", str_extract(AVISIT, "(?<=(WEEK ))[0-9]+")),
        TRUE ~ as.character(NA)
      ),
      AVISITCDN = case_when(
        AVISITCD == "SCR" ~ -2,
        AVISITCD == "BL" ~ 0,
        grepl("W", AVISITCD) ~ as.numeric(gsub("[^0-9]*", "", AVISITCD)),
        TRUE ~ as.numeric(NA)
      ),
      AVISITCD = factor(AVISITCD) %>% reorder(AVISITCDN),
      TRTORD = case_when(
        ARMCD == "ARM C" ~ 1,
        ARMCD == "ARM B" ~ 2,
        ARMCD == "ARM A" ~ 3
      ),
      ARM = as.character(arm_mapping[match(ARM, names(arm_mapping))]),
      ARM = factor(ARM) %>% reorder(TRTORD),
      ACTARM = as.character(arm_mapping[match(ACTARM, names(arm_mapping))]),
      ACTARM = factor(ACTARM) %>% reorder(TRTORD)
    )
  
  attr(ADLB[["ARM"]], "label") <- var_labels[["ARM"]]
  attr(ADLB[["ACTARM"]], "label") <- var_labels[["ACTARM"]]
})

datanames <- c("ADSL", "ADLB")
datanames(data) <- datanames
join_keys(data) <- default_cdisc_join_keys[datanames]

app <- init(
  data = data,
  modules = modules(
    tm_g_gh_density_distribution_plot(
      label = "Density Distribution Plot",
      dataname = "ADLB",
      param_var = "PARAMCD",
      param = choices_selected(c("ALT", "CRP", "IGA"), "ALT"),
      xaxis_var = choices_selected(c("AVAL", "BASE", "CHG", "PCHG"), "AVAL"),
      trt_group = choices_selected(c("ARM", "ACTARM"), "ARM"),
      color_manual = c(
        "150mg QD" = "#000000",
        "Placebo" = "#3498DB",
        "Combination" = "#E74C3C"
      ),
      color_comb = "#39ff14",
      comb_line = TRUE,
      plot_height = c(500, 200, 2000),
      font_size = c(12, 8, 20),
      line_size = c(1, .25, 3),
      hline_arb = c(.02, .05),
      hline_arb_color = c("red", "black"),
      hline_arb_label = c("Horizontal Line A", "Horizontal Line B")
    )
  )
)
if (interactive()) {
  shinyApp(app$ui, app$server)
}


```


</details>
  • Loading branch information
m7pr committed Sep 18, 2024
1 parent 5b9f696 commit dbbe6d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion R/tm_g_gh_density_distribution_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ srv_g_density_distribution_plot <- function(id, # nolint
"xaxis_var",
"xaxis_param",
anl_q,
is_density = TRUE
is_density = TRUE,
"trt_group"
)
keep_data_const_opts_updated(session, input, anl_q, "xaxis_param")

Expand Down
13 changes: 10 additions & 3 deletions R/utils-keep_range_slider_updated.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ keep_range_slider_updated <- function(session,
id_var,
id_param_var,
reactive_ANL, # nolint
is_density = FALSE) {
is_density = FALSE,
id_trt_group) {
stopifnot(is.function(update_slider_fcn))

observe({
Expand All @@ -25,8 +26,14 @@ keep_range_slider_updated <- function(session,
step <- NULL

if (isTRUE(is_density)) {
minmax <- c(0, round(max(stats::density(stats::na.omit(ANL[[varname]]))$y) * 1.5, 5))
step <- round(max(stats::density(stats::na.omit(ANL[[varname]]))$y) / 100, 5)
treatname <- input[[id_trt_group]]
ANL_split <- ANL %>% split(f = factor(paste0(ANL[["AVISITCD"]], ANL[[treatname]]))) # nolint
density_maxes <- lapply(ANL_split, function(x) {
max(stats::density(stats::na.omit(x[[varname]]))$y)
})
dmax <- max(unlist(density_maxes))
minmax <- c(0, round(dmax * 1.2, 5))
step <- round(dmax / 100, 5)
}

isolate(update_slider_fcn(
Expand Down

0 comments on commit dbbe6d9

Please sign in to comment.