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 to tm_g_pp_patient_timeline #1270

Conversation

llrs-roche
Copy link
Contributor

@llrs-roche llrs-roche commented Nov 28, 2024

Part of insightsengineering/teal#1371

Example using cowplot::add_sub with ggplot2
devtools::load_all("../teal.modules.general")
devtools::load_all("../teal.reporter")
devtools::load_all("../teal")
devtools::load_all(".")
library(nestcolor)
library(dplyr)

data <- teal_data()
data <- within(data, {
  ADAE <- tmc_ex_adae
  ADSL <- tmc_ex_adsl %>%
    filter(USUBJID %in% ADAE$USUBJID)
  ADCM <- tmc_ex_adcm %>%
    mutate(
      CMSTDY = case_when(
        CMCAT == "medcl B" ~ 20,
        CMCAT == "medcl C" ~ 150,
        TRUE ~ 1
      ) %>% with_label("Study Day of Start of Medication"),
      CMENDY = case_when(
        CMCAT == "medcl B" ~ 700,
        CMCAT == "medcl C" ~ 1000,
        TRUE ~ 500
      ) %>% with_label("Study Day of End of Medication"),
      CMASTDTM = ASTDTM,
      CMAENDTM = AENDTM
    )
})

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


caption_decorator <- function(annotation = "I am a good decorator", var_to_decorate = "plot") {
  teal_transform_module(
    label = "Annotation",
    ui = function(id) shiny::textInput(shiny::NS(id, "annotation"), "Annotation", value = annotation),
    server = make_teal_transform_server(
      substitute({
        var_to_decorate <- cowplot::add_sub(var_to_decorate, annotation)
      }, env = list(var_to_decorate = as.name(var_to_decorate)))
    )
  )
}


app <- init(
  data = data,
  modules = modules(
    tm_g_pp_patient_timeline(
      label = "Patient Timeline",
      dataname_adae = "ADAE",
      dataname_adcm = "ADCM",
      parentname = "ADSL",
      patient_col = "USUBJID",
      plot_height = c(600L, 200L, 2000L),
      cmdecod = choices_selected(
        choices = variable_choices(data[["ADCM"]], "CMDECOD"),
        selected = "CMDECOD",
      ),
      aeterm = choices_selected(
        choices = variable_choices(data[["ADAE"]], "AETERM"),
        selected = c("AETERM")
      ),
      aetime_start = choices_selected(
        choices = variable_choices(data[["ADAE"]], "ASTDTM"),
        selected = c("ASTDTM")
      ),
      aetime_end = choices_selected(
        choices = variable_choices(data[["ADAE"]], "AENDTM"),
        selected = c("AENDTM")
      ),
      dstime_start = choices_selected(
        choices = variable_choices(data[["ADCM"]], "CMASTDTM"),
        selected = c("CMASTDTM")
      ),
      dstime_end = choices_selected(
        choices = variable_choices(data[["ADCM"]], "CMAENDTM"),
        selected = c("CMAENDTM")
      ),
      aerelday_start = choices_selected(
        choices = variable_choices(data[["ADAE"]], "ASTDY"),
        selected = c("ASTDY")
      ),
      aerelday_end = choices_selected(
        choices = variable_choices(data[["ADAE"]], "AENDY"),
        selected = c("AENDY")
      ),
      dsrelday_start = choices_selected(
        choices = variable_choices(data[["ADCM"]], "ASTDY"),
        selected = c("ASTDY")
      ),
      dsrelday_end = choices_selected(
        choices = variable_choices(data[["ADCM"]], "AENDY"),
        selected = c("AENDY")
      ),
      decorators = list(caption_decorator())
    )
  )
)
shinyApp(app$ui, app$server)

@llrs-roche llrs-roche marked this pull request as ready for review November 29, 2024 09:14
@llrs-roche
Copy link
Contributor Author

In this module I wasn't sure where to place the decorator. I end up deciding to put it on the last element of the fixed input. Below there are some input forms that might change. I think this will make it more stable for user but it might not be consistent with other modules that also have conditional inputs

@m7pr
Copy link
Contributor

m7pr commented Nov 29, 2024

When running the code I see below in the console. This is most probably from print(plot)
that gets passed to srv_decorate_teal_data that goes

eval_code(decorated_output(), expr)

Have you seen this in other modules that you did/review?

> shinyApp(app$ui, app$server)

Listening on http://127.0.0.1:3885
TableGrob (19 x 14) "layout": 24 grobs
    z         cells             name
1   0 ( 1-19, 1-14)       background
2   1 ( 9- 9, 7- 7)        panel-1-1
3   1 (11-11, 7- 7)        panel-2-1
4   3 ( 8- 8, 7- 7)         axis-t-1
5   3 (12-12, 7- 7)         axis-b-1
6   3 ( 9- 9, 6- 6)         axis-l-1
7   3 (11-11, 6- 6)         axis-l-2
8   3 ( 9- 9, 9- 9)         axis-r-1
9   3 (11-11, 9- 9)         axis-r-2
10  2 ( 9- 9, 8- 8)        strip-r-1
11  2 (11-11, 8- 8)        strip-r-2
12  4 ( 7- 7, 7- 7)           xlab-t
13  5 (13-13, 7- 7)           xlab-b
14  6 ( 9-11, 5- 5)           ylab-l
15  7 ( 9-11,10-10)           ylab-r
16  8 ( 9-11,12-12)  guide-box-right
17  9 ( 9-11, 3- 3)   guide-box-left
18 10 (15-15, 7- 7) guide-box-bottom
19 11 ( 5- 5, 7- 7)    guide-box-top
20 12 ( 9-11, 7- 7) guide-box-inside
21 13 ( 4- 4, 7- 7)         subtitle
22 14 ( 3- 3, 7- 7)            title
23 15 (16-16, 7- 7)          caption
24 16 (17-17, 7- 7)              sub
                                            grob
1                rect[plot.background..rect.237]
2                       gTree[panel-1.gTree.175]
3                       gTree[panel-2.gTree.185]
4                                 zeroGrob[NULL]
5            absoluteGrob[GRID.absoluteGrob.190]
6            absoluteGrob[GRID.absoluteGrob.195]
7            absoluteGrob[GRID.absoluteGrob.200]
8                                 zeroGrob[NULL]
9                                 zeroGrob[NULL]
10                                 gtable[strip]
11                                 gtable[strip]
12                                zeroGrob[NULL]
13 titleGrob[axis.title.x.bottom..titleGrob.227]
14   titleGrob[axis.title.y.left..titleGrob.230]
15                                zeroGrob[NULL]
16                                zeroGrob[NULL]
17                                zeroGrob[NULL]
18                                zeroGrob[NULL]
19                                zeroGrob[NULL]
20                                zeroGrob[NULL]
21         zeroGrob[plot.subtitle..zeroGrob.234]
22          titleGrob[plot.title..titleGrob.233]
23          zeroGrob[plot.caption..zeroGrob.235]
24                           text[GRID.text.163]
TableGrob (19 x 14) "layout": 24 grobs
    z         cells             name
1   0 ( 1-19, 1-14)       background
2   1 ( 9- 9, 7- 7)        panel-1-1
3   1 (11-11, 7- 7)        panel-2-1
4   3 ( 8- 8, 7- 7)         axis-t-1
5   3 (12-12, 7- 7)         axis-b-1
6   3 ( 9- 9, 6- 6)         axis-l-1
7   3 (11-11, 6- 6)         axis-l-2
8   3 ( 9- 9, 9- 9)         axis-r-1
9   3 (11-11, 9- 9)         axis-r-2
10  2 ( 9- 9, 8- 8)        strip-r-1
11  2 (11-11, 8- 8)        strip-r-2
12  4 ( 7- 7, 7- 7)           xlab-t
13  5 (13-13, 7- 7)           xlab-b
14  6 ( 9-11, 5- 5)           ylab-l
15  7 ( 9-11,10-10)           ylab-r
16  8 ( 9-11,12-12)  guide-box-right
17  9 ( 9-11, 3- 3)   guide-box-left
18 10 (15-15, 7- 7) guide-box-bottom
19 11 ( 5- 5, 7- 7)    guide-box-top
20 12 ( 9-11, 7- 7) guide-box-inside
21 13 ( 4- 4, 7- 7)         subtitle
22 14 ( 3- 3, 7- 7)            title
23 15 (16-16, 7- 7)          caption
24 16 (17-17, 7- 7)              sub
                                            grob
1                rect[plot.background..rect.312]
2                       gTree[panel-1.gTree.250]
3                       gTree[panel-2.gTree.260]
4                                 zeroGrob[NULL]
5            absoluteGrob[GRID.absoluteGrob.265]
6            absoluteGrob[GRID.absoluteGrob.270]
7            absoluteGrob[GRID.absoluteGrob.275]
8                                 zeroGrob[NULL]
9                                 zeroGrob[NULL]
10                                 gtable[strip]
11                                 gtable[strip]
12                                zeroGrob[NULL]
13 titleGrob[axis.title.x.bottom..titleGrob.302]
14   titleGrob[axis.title.y.left..titleGrob.305]
15                                zeroGrob[NULL]
16                                zeroGrob[NULL]
17                                zeroGrob[NULL]
18                                zeroGrob[NULL]
19                                zeroGrob[NULL]
20                                zeroGrob[NULL]
21         zeroGrob[plot.subtitle..zeroGrob.309]
22          titleGrob[plot.title..titleGrob.308]
23          zeroGrob[plot.caption..zeroGrob.310]
24                           text[GRID.text.238]
TableGrob (19 x 14) "layout": 24 grobs
    z         cells             name
1   0 ( 1-19, 1-14)       background
2   1 ( 9- 9, 7- 7)        panel-1-1
3   1 (11-11, 7- 7)        panel-2-1
4   3 ( 8- 8, 7- 7)         axis-t-1
5   3 (12-12, 7- 7)         axis-b-1
6   3 ( 9- 9, 6- 6)         axis-l-1
7   3 (11-11, 6- 6)         axis-l-2
8   3 ( 9- 9, 9- 9)         axis-r-1
9   3 (11-11, 9- 9)         axis-r-2
10  2 ( 9- 9, 8- 8)        strip-r-1
11  2 (11-11, 8- 8)        strip-r-2
12  4 ( 7- 7, 7- 7)           xlab-t
13  5 (13-13, 7- 7)           xlab-b
14  6 ( 9-11, 5- 5)           ylab-l
15  7 ( 9-11,10-10)           ylab-r
16  8 ( 9-11,12-12)  guide-box-right
17  9 ( 9-11, 3- 3)   guide-box-left
18 10 (15-15, 7- 7) guide-box-bottom
19 11 ( 5- 5, 7- 7)    guide-box-top
20 12 ( 9-11, 7- 7) guide-box-inside
21 13 ( 4- 4, 7- 7)         subtitle
22 14 ( 3- 3, 7- 7)            title
23 15 (16-16, 7- 7)          caption
24 16 (17-17, 7- 7)              sub
                                            grob
1                rect[plot.background..rect.387]
2                       gTree[panel-1.gTree.325]
3                       gTree[panel-2.gTree.335]
4                                 zeroGrob[NULL]
5            absoluteGrob[GRID.absoluteGrob.340]
6            absoluteGrob[GRID.absoluteGrob.345]
7            absoluteGrob[GRID.absoluteGrob.350]
8                                 zeroGrob[NULL]
9                                 zeroGrob[NULL]
10                                 gtable[strip]
11                                 gtable[strip]
12                                zeroGrob[NULL]
13 titleGrob[axis.title.x.bottom..titleGrob.377]
14   titleGrob[axis.title.y.left..titleGrob.380]
15                                zeroGrob[NULL]
16                                zeroGrob[NULL]
17                                zeroGrob[NULL]
18                                zeroGrob[NULL]
19                                zeroGrob[NULL]
20                                zeroGrob[NULL]
21         zeroGrob[plot.subtitle..zeroGrob.384]
22          titleGrob[plot.title..titleGrob.383]
23          zeroGrob[plot.caption..zeroGrob.385]
24                           text[GRID.text.313]

@llrs-roche
Copy link
Contributor Author

I just started seeing this today, but only when there was an issue with the module if my memory serves me well.

R/tm_g_pp_patient_timeline.R Outdated Show resolved Hide resolved
@m7pr m7pr self-assigned this Nov 29, 2024
@m7pr m7pr merged commit 98520eb into 1187_decorate_output@main Nov 29, 2024
1 check passed
@m7pr m7pr deleted the tm_g_pp_patient_timeline@1187_decorate_output@main branch November 29, 2024 10:01
@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.

2 participants