Skip to content

Commit

Permalink
tdata to teal_data - tm_g_heat_bygrade (#239)
Browse files Browse the repository at this point in the history
**Example App**

```r
data <- cdisc_data() |>
  within({
    library(dplyr)
    library(nestcolor)
    ADSL <- rADSL %>% slice(1:30)
    ADEX <- rADEX %>% filter(USUBJID %in% ADSL$USUBJID)
    ADAE <- rADAE %>% filter(USUBJID %in% ADSL$USUBJID)
    ADCM <- rADCM %>% filter(USUBJID %in% ADSL$USUBJID)
    # This preprocess is only to force legacy standard on ADCM
    ADCM <- ADCM %>%
      select(-starts_with("ATC")) %>%
      unique()
    # function to derive AVISIT from ADEX
    add_visit <- function(data_need_visit) {
      visit_dates <- ADEX %>%
        filter(PARAMCD == "DOSE") %>%
        distinct(USUBJID, AVISIT, ASTDTM) %>%
        group_by(USUBJID) %>%
        arrange(ASTDTM) %>%
        mutate(next_vis = lead(ASTDTM), is_last = ifelse(is.na(next_vis), TRUE, FALSE)) %>%
        rename(this_vis = ASTDTM)
      data_visit <- data_need_visit %>%
        select(USUBJID, ASTDTM) %>%
        left_join(visit_dates, by = "USUBJID") %>%
        filter(ASTDTM > this_vis & (ASTDTM < next_vis | is_last == TRUE)) %>%
        left_join(data_need_visit) %>%
        distinct()
      return(data_visit)
    }
    # derive AVISIT for ADAE and ADCM
    ADAE <- add_visit(ADAE)
    ADCM <- add_visit(ADCM)
    # derive ongoing status variable for ADEX
    ADEX <- ADEX %>%
      filter(PARCAT1 == "INDIVIDUAL") %>%
      mutate(ongo_status = (EOSSTT == "ONGOING"))
  })

datanames(data) <- c("ADSL", "ADEX", "ADAE", "ADCM")
join_keys(data) <- default_cdisc_join_keys[datanames(data)]

ADCM <- data[["ADCM"]]

app <- init(
  data = data,
  modules = modules(
    tm_g_heat_bygrade(
      label = "Heatmap by grade",
      sl_dataname = "ADSL",
      ex_dataname = "ADEX",
      ae_dataname = "ADAE",
      cm_dataname = "ADCM",
      id_var = choices_selected(
        selected = "USUBJID",
        choices = c("USUBJID", "SUBJID")
      ),
      visit_var = choices_selected(
        selected = "AVISIT",
        choices = c("AVISIT")
      ),
      ongo_var = choices_selected(
        selected = "ongo_status",
        choices = c("ongo_status")
      ),
      anno_var = choices_selected(
        selected = c("SEX", "COUNTRY"),
        choices = c("SEX", "COUNTRY", "USUBJID")
      ),
      heat_var = choices_selected(
        selected = "AETOXGR",
        choices = c("AETOXGR")
      ),
      conmed_var = choices_selected(
        selected = "CMDECOD",
        choices = c("CMDECOD")
      ),
      plot_height = c(600, 200, 2000)
    )
  )
)
if (interactive()) {
  shinyApp(app$ui, app$server)
}
```

---------

Co-authored-by: Aleksander Chlebowski <[email protected]>
  • Loading branch information
vedhav and Aleksander Chlebowski authored Nov 24, 2023
1 parent 6ea00ae commit 2b83f72
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions R/tm_g_heat_bygrade.R
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,20 @@ srv_g_heatmap_bygrade <- function(id,
plot_width) {
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
checkmate::assert_class(data, "tdata")
checkmate::assert_class(data, "reactive")
checkmate::assert_class(shiny::isolate(data()), "teal_data")
if (!is.na(sl_dataname)) checkmate::assert_names(sl_dataname, subset.of = names(data))
if (!is.na(ex_dataname)) checkmate::assert_names(ex_dataname, subset.of = names(data))
if (!is.na(ae_dataname)) checkmate::assert_names(ae_dataname, subset.of = names(data))
if (!is.na(cm_dataname)) checkmate::assert_names(cm_dataname, subset.of = names(data))

moduleServer(id, function(input, output, session) {
iv <- reactive({
ADSL <- data[[sl_dataname]]() # nolint
ADEX <- data[[ex_dataname]]() # nolint
ADAE <- data[[ae_dataname]]() # nolint
ADSL <- data()[[sl_dataname]] # nolint
ADEX <- data()[[ex_dataname]] # nolint
ADAE <- data()[[ae_dataname]] # nolint
if (isTRUE(input$plot_cm)) {
ADCM <- data[[cm_dataname]]() # nolint
ADCM <- data()[[cm_dataname]] # nolint
}

iv <- shinyvalidate::InputValidator$new()
Expand Down Expand Up @@ -341,11 +342,11 @@ srv_g_heatmap_bygrade <- function(id,
iv
})
iv_cm <- reactive({
ADSL <- data[[sl_dataname]]() # nolint
ADEX <- data[[ex_dataname]]() # nolint
ADAE <- data[[ae_dataname]]() # nolint
ADSL <- data()[[sl_dataname]] # nolint
ADEX <- data()[[ex_dataname]] # nolint
ADAE <- data()[[ae_dataname]] # nolint
if (isTRUE(input$plot_cm)) {
ADCM <- data[[cm_dataname]]() # nolint
ADCM <- data()[[cm_dataname]] # nolint
}

iv_cm <- shinyvalidate::InputValidator$new()
Expand Down Expand Up @@ -381,7 +382,7 @@ srv_g_heatmap_bygrade <- function(id,

if (!is.na(cm_dataname)) {
observeEvent(input$conmed_var, {
ADCM <- data[[cm_dataname]]() # nolint
ADCM <- data()[[cm_dataname]] # nolint
choices <- levels(ADCM[[input$conmed_var]])

updateSelectInput(
Expand All @@ -396,19 +397,20 @@ srv_g_heatmap_bygrade <- function(id,
output_q <- shiny::debounce(
millis = 200,
r = reactive({
ADSL <- data[[sl_dataname]]() # nolint
ADEX <- data[[ex_dataname]]() # nolint
ADAE <- data[[ae_dataname]]() # nolint
ADSL <- data()[[sl_dataname]] # nolint
ADEX <- data()[[ex_dataname]] # nolint
ADAE <- data()[[ae_dataname]] # nolint

teal::validate_has_data(ADSL, min_nrow = 1, msg = sprintf("%s contains no data", sl_dataname))
teal::validate_inputs(iv(), iv_cm())
if (isTRUE(input$plot_cm)) {
shiny::validate(shiny::need(all(input$conmed_level %in% ADCM[[input$conmed_var]]), "Updating Conmed Levels"))
}

qenv <- teal.code::new_qenv(tdata2env(data), code = teal::get_code_tdata(data))
qenv <- data()

if (isTRUE(input$plot_cm)) {
ADCM <- data[[cm_dataname]]() # nolint
ADCM <- qenv[[cm_dataname]] # nolint
qenv <- teal.code::eval_code(
qenv,
code = substitute(
Expand Down

0 comments on commit 2b83f72

Please sign in to comment.