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

introduce decorators for tm_g_pp_therapy #1268

Merged

Conversation

m7pr
Copy link
Contributor

@m7pr m7pr commented Nov 28, 2024

Part of insightsengineering/teal#1371

Working Example
devtools::load_all("../teal.reporter")
devtools::load_all("../teal")
devtools::load_all(".")

library(nestcolor)
library(dplyr)

data <- teal_data()
data <- within(data, {
  ADCM <- tmc_ex_adcm
  ADSL <- tmc_ex_adsl %>% filter(USUBJID %in% ADCM$USUBJID)
  ADCM$CMASTDTM <- ADCM$ASTDTM
  ADCM$CMAENDTM <- ADCM$AENDTM
})

join_keys(data) <- default_cdisc_join_keys[c("ADSL", "ADCM")]
adcm_keys <- c("STUDYID", "USUBJID", "ASTDTM", "CMSEQ", "ATC1", "ATC2", "ATC3", "ATC4")
join_keys(data)["ADCM", "ADCM"] <- adcm_keys

ADSL <- data[["ADSL"]]
ADCM <- data[["ADCM"]]

caption_decorator <- function(default_caption = "I am a good decorator", .var_to_replace = "plot") {
  teal_transform_module(
    label = "Caption",
    ui = function(id) shiny::textInput(shiny::NS(id, "footnote"), "Footnote", value = default_caption),
    server = make_teal_transform_server(
      substitute({
        .var_to_replace <- .var_to_replace + ggplot2::labs(caption = footnote)
      }, env = list(.var_to_replace = as.name(.var_to_replace)))
    )
  )
}

head_decorator <- function(default_value = 6, .var_to_replace = "object") {
  teal_transform_module(
    label = "Head",
    ui = function(id) shiny::numericInput(shiny::NS(id, "n"), "N rows", value = default_value),
    server = make_teal_transform_server(
      substitute({
        .var_to_replace <- utils::head(.var_to_replace, n = n)
      }, env = list(.var_to_replace = as.name(.var_to_replace)))
    )
  )
}

app <- init(
  data = data,
  modules = modules(
    tm_g_pp_therapy(
      label = "Therapy",
      dataname = "ADCM",
      parentname = "ADSL",
      patient_col = "USUBJID",
      plot_height = c(600L, 200L, 2000L),
      atirel = choices_selected(
        choices = variable_choices(ADCM, "ATIREL"),
        selected = c("ATIREL")
      ),
      cmdecod = choices_selected(
        choices = variable_choices(ADCM, "CMDECOD"),
        selected = "CMDECOD"
      ),
      cmindc = choices_selected(
        choices = variable_choices(ADCM, "CMINDC"),
        selected = "CMINDC"
      ),
      cmdose = choices_selected(
        choices = variable_choices(ADCM, "CMDOSE"),
        selected = "CMDOSE"
      ),
      cmtrt = choices_selected(
        choices = variable_choices(ADCM, "CMTRT"),
        selected = "CMTRT"
      ),
      cmdosu = choices_selected(
        choices = variable_choices(ADCM, "CMDOSU"),
        selected = c("CMDOSU")
      ),
      cmroute = choices_selected(
        choices = variable_choices(ADCM, "CMROUTE"),
        selected = "CMROUTE"
      ),
      cmdosfrq = choices_selected(
        choices = variable_choices(ADCM, "CMDOSFRQ"),
        selected = "CMDOSFRQ"
      ),
      cmstdy = choices_selected(
        choices = variable_choices(ADCM, "ASTDY"),
        selected = "ASTDY"
      ),
      cmendy = choices_selected(
        choices = variable_choices(ADCM, "AENDY"),
        selected = "AENDY"
      ),
      decorators = list(plot = caption_decorator('Marcin', 'plot'), table = head_decorator(2, 'table'))
    )
  )
)
if (interactive()) {
  shinyApp(app$ui, app$server)
}

Copy link
Contributor

@llrs-roche llrs-roche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks fine but the example has an error that prevents the module to display.

Warning: Error in : One or more variables in the dataframe have class 'difftime'. Please convert to factor or character. 
 when evaluating qenv code:
table <- rlistings::as_listing(table, key_cols = NULL, default_formatting = list(all = fmt_config(align = "left")))

This prevents the module to display the plot with the caption:
screenshot showing the error above

@m7pr
Copy link
Contributor Author

m7pr commented Nov 29, 2024

@llrs-roche this is what I see

image

@m7pr
Copy link
Contributor Author

m7pr commented Nov 29, 2024

Can you confirm you

  • pulled teal on 1187_decorate_output@main branch, and checked-out to this branch
  • pulled teal.reporter on main branch, and checked-out to this branch
  • pulled teal.modules.clinical on tm_g_pp_therapy@1187_decorate_output@main, and checked-out to this branch

and then. before running the example did you run

devtools::load_all("../teal.reporter")
devtools::load_all("../teal")
devtools::load_all(".")

@llrs-roche
Copy link
Contributor

I can confirm that I run all the steps above and I still see those warnings/errors.
The issue could be that I'm using rlistings 0.2.9.9010.

Copy link
Contributor

@llrs-roche llrs-roche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing to CRAN's version for rlistings worked. I'm not sure if this should be a separate issue, sooner or later teal* packages will be facing this

@vedhav
Copy link
Contributor

vedhav commented Nov 29, 2024

Yup it’s the rlistings issue. Have to use character or factor column instead.

@m7pr
Copy link
Contributor Author

m7pr commented Nov 29, 2024

Alrighty, the rlistings thing was fixed in here #1271

@m7pr m7pr merged commit f462d60 into 1187_decorate_output@main Nov 29, 2024
1 check passed
@m7pr m7pr deleted the tm_g_pp_therapy@1187_decorate_output@main branch November 29, 2024 12:29
@github-actions github-actions bot locked and limited conversation to collaborators Nov 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants