Skip to content

Commit

Permalink
Stop using datanames in favour of using . prefix names to hide them (
Browse files Browse the repository at this point in the history
…#181)

Closes insightsengineering/teal#1366

This also fixes the failed longitudinal app deployment failure in the
`dev` channel.
  • Loading branch information
vedhav authored Oct 9, 2024
1 parent a160925 commit 2612746
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 160 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ rsconnect/

# Markdown generated files
*/README.html
*/shiny_bookmarks
teal_app.lock
1 change: 0 additions & 1 deletion RNA-seq/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ data <- within(data, {
dplyr::mutate(is_event = CNSR == 0)
MAE <- hermes::multi_assay_experiment
})
datanames(data) <- c("ADTTE", "MAE")

## App header and footer ----
nest_logo <- "https://raw.githubusercontent.com/insightsengineering/hex-stickers/main/PNG/nest.png"
Expand Down
23 changes: 7 additions & 16 deletions early-dev/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ data <- within(data, {
ADSL <- radsl(seed = 1)

# derive ADSL treatment duration
adsl_labels <- teal.data::col_labels(ADSL, fill = FALSE)
.adsl_labels <- teal.data::col_labels(ADSL, fill = FALSE)
ADSL <- ADSL %>%
mutate(
TRTDURD = as.numeric(as.Date(TRTEDTM) - as.Date(TRTSDTM)) + 1,
Expand All @@ -29,12 +29,11 @@ data <- within(data, {
EOSSTT = "End of Study Status"
) %>%
droplevels()
teal.data::col_labels(ADSL)[c(names(adsl_labels))] <- adsl_labels
teal.data::col_labels(ADSL)[c(names(.adsl_labels))] <- .adsl_labels

ADAE <- radae(ADSL, seed = 1)

# derive common flags for AEs
adae_labels <- teal.data::col_labels(ADAE, fill = FALSE)
ADAE <- ADAE %>%
mutate_at(c("AESOC", "AEBODSYS", "AEHLT", "AEDECOD", "AETERM", "AELLT"), as.character) %>%
mutate(
Expand Down Expand Up @@ -93,7 +92,7 @@ data <- within(data, {
ADTR <- radtr(ADSL, seed = 1)

# process ADTR
adtr_labels <- teal.data::col_labels(ADTR, fill = FALSE)
.adtr_labels <- teal.data::col_labels(ADTR, fill = FALSE)
ADTR <- ADTR %>%
mutate(
PCHG = ifelse(AVISIT == "BASELINE", 0, PCHG),
Expand All @@ -102,7 +101,7 @@ data <- within(data, {
AVALC = ifelse(AVISIT == "BASELINE", as.character(BASE), AVALC)
) %>%
filter(AVISIT != "SCREENING")
teal.data::col_labels(ADTR) <- adtr_labels
teal.data::col_labels(ADTR) <- .adtr_labels

ADTRWF <- ADTR %>%
filter(AVISIT != "BASELINE")
Expand All @@ -115,7 +114,6 @@ data <- within(data, {
arrange(USUBJID)

ADRS <- radrs(ADSL, seed = 1)
adrs_labels <- teal.data::col_labels(ADRS, fill = FALSE)
ADRS <- ADRS %>%
filter(PARAMCD %in% c("BESRSPI", "INVET")) %>%
mutate(ADT = as.Date(ADTM)) %>%
Expand All @@ -136,12 +134,10 @@ data <- within(data, {
)
})

# set datanames
datanames <- c("ADSL", "ADAE", "ADCM", "ADEX", "ADTR", "ADTRWF", "ADRS", "ADRSSWIM", "ADLB")
datanames(data) <- datanames

# set join keys
join_keys(data) <- default_cdisc_join_keys[datanames] # get default keys by name
join_keys(data) <- default_cdisc_join_keys[
c("ADSL", "ADAE", "ADCM", "ADEX", "ADTR", "ADTRWF", "ADRS", "ADRSSWIM", "ADLB")
] # get default keys by name
join_keys(data)["ADTR", "ADTR"] <- c("STUDYID", "USUBJID", "PARAMCD", "AVISIT")
join_keys(data)["ADTRWF", "ADTRWF"] <- c("STUDYID", "USUBJID", "PARAMCD", "AVISIT")
join_keys(data)["ADRSSWIM", "ADRSSWIM"] <- c("STUDYID", "USUBJID", "PARAMCD", "AVISIT")
Expand All @@ -162,13 +158,8 @@ ADRSSWIM <- data[["ADRSSWIM"]]
ADLB <- data[["ADLB"]]


adsl_labels <- teal.data::col_labels(ADSL)
fact_vars_asl <- names(Filter(isTRUE, sapply(ADSL, is.factor)))

date_vars_asl <-
names(ADSL)[vapply(ADSL, function(x) inherits(x, c("Date", "POSIXct", "POSIXlt")), logical(1))]
demog_vars_asl <- names(ADSL)[!(names(ADSL) %in% c("USUBJID", "STUDYID", date_vars_asl))]

arm_vars <- c("ARMCD", "ARM", "ACTARMCD", "ACTARM", "EOSSTT")
aeflag_vars <- c("RELFL", "CTC35FL", "SERFL", "RELSERFL")
facet_vars <- c("SEX", "BMRKR2", "RACE", "STRATA1", "STRATA2")
Expand Down
26 changes: 11 additions & 15 deletions efficacy/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ data <- within(data, {
library(sparkline)

ADSL <- radsl(seed = 1)
adsl_labels <- teal.data::col_labels(ADSL, fill = FALSE)
.adsl_labels <- teal.data::col_labels(ADSL, fill = FALSE)

char_vars_asl <- names(Filter(isTRUE, sapply(ADSL, is.character)))
.char_vars_asl <- names(Filter(isTRUE, sapply(ADSL, is.character)))

adsl_labels <- c(
adsl_labels,
.adsl_labels <- c(
.adsl_labels,
AGEGR1 = "Age Group"
)
ADSL <- ADSL %>%
Expand All @@ -27,19 +27,19 @@ data <- within(data, {
AGE >= 45 ~ ">=45"
))
) %>%
mutate_at(char_vars_asl, factor)
mutate_at(.char_vars_asl, factor)

teal.data::col_labels(ADSL) <- adsl_labels
teal.data::col_labels(ADSL) <- .adsl_labels

ADTTE <- radtte(ADSL, seed = 1)

ADRS <- radrs(ADSL, seed = 1)
adrs_labels <- teal.data::col_labels(ADRS, fill = FALSE)
.adrs_labels <- teal.data::col_labels(ADRS, fill = FALSE)
ADRS <- filter(ADRS, PARAMCD == "BESRSPI" | AVISIT == "FOLLOW UP")
teal.data::col_labels(ADRS) <- adrs_labels
teal.data::col_labels(ADRS) <- .adrs_labels

ADQS <- radqs(ADSL, seed = 1)
adqs_labels <- teal.data::col_labels(ADQS, fill = FALSE)
.adqs_labels <- teal.data::col_labels(ADQS, fill = FALSE)
ADQS <- ADQS %>%
filter(ABLFL != "Y" & ABLFL2 != "Y") %>%
filter(AVISIT %in% c("WEEK 1 DAY 8", "WEEK 2 DAY 15", "WEEK 3 DAY 22")) %>%
Expand All @@ -50,15 +50,11 @@ data <- within(data, {
as.numeric() %>%
as.factor()
)
teal.data::col_labels(ADQS) <- adqs_labels
teal.data::col_labels(ADQS) <- .adqs_labels
})

# set datanames
datanames <- c("ADSL", "ADTTE", "ADRS", "ADQS")
datanames(data) <- datanames

# set join_keys
join_keys(data) <- default_cdisc_join_keys[datanames]
join_keys(data) <- default_cdisc_join_keys[c("ADSL", "ADTTE", "ADRS", "ADQS")]

## Reusable Configuration For Modules
ADSL <- data[["ADSL"]]
Expand Down
5 changes: 1 addition & 4 deletions exploratory/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ data <- within(data, {
)
})

datanames <- c("ADSL", "ADRS", "ADLB", "ADLBPCA")
datanames(data) <- datanames

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

## Reusable Configuration For Modules
ADSL <- data[["ADSL"]]
Expand Down
123 changes: 50 additions & 73 deletions longitudinal/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ data <- within(data, {
# for example if biomarker is already log2 transformed then exclude
# value of AVAL will then be assigned to XXXXXL2 variables. e.g. AVALL2
# e.g. if x assay is already log2 transformed then assigns AVAL to AVALL2
exclude_l2 <- c("")
.exclude_l2 <- c("")

# for example if study specific biomarker represents a CHG value then exclude
# value NA is assigned to XXXXXL2. e.g. AVALL2
# e.g. if x assay are CHG values then assigns NA to AVALL2
exclude_chg <- c("")
.exclude_chg <- c("")

# the app expects ARM to be the main treatment variable
# to use other treatment variables it is easiest to rename to ARM and process as ARM
Expand Down Expand Up @@ -64,13 +64,13 @@ data <- within(data, {

# convenience operator: create an operator that keeps the variable attributes
# variable labels often get clobbered and we would like to keep them)
`%keep_label%` <- function(lhv, rhv) {
.keep_label <- function(lhv, rhv) {
attributes(lhv) <- attributes(rhv)
lhv
}

# convenience operator: create an operator that adds a label to newly created or re-processed variables
`%make_label%` <- function(lhv, label) {
.make_label <- function(lhv, label) {
attr(lhv, "label") <- label
lhv
}
Expand All @@ -86,10 +86,10 @@ data <- within(data, {
TRT01P == "B: Placebo" ~ 3,
TRUE ~ as.numeric(NA)
),
TRTORD = TRTORD %make_label% "Treatment Order",
TRTORD = .make_label(TRTORD, "Treatment Order"),
TRT01P = as.character(arm_mapping[match(TRT01P, names(arm_mapping))]),
TRT01P = factor(ARM) %>% reorder(TRTORD),
TRT01P = TRT01P %make_label% "Planned Treatment for Period 01"
TRT01P = .make_label(TRT01P, "Planned Treatment for Period 01")
)

# capture state of variable labels to apply back onto variables after data filtering
Expand Down Expand Up @@ -198,72 +198,76 @@ data <- within(data, {
select(USUBJID, PARAMCD, AVAL) %>%
group_by(PARAMCD) %>%
summarise(AVAL_MIN = min(AVAL, na.rm = TRUE), .groups = "drop") %>%
mutate(PARAMCD = PARAMCD %make_label% "Parameter Code")
mutate(PARAMCD = .make_label(PARAMCD, "Parameter Code"))

# post process the data to create several new variables and adjust existing record specific values per specification
# - adjust existing BASELINE record values where values are missing
ADLB_SUPED1 <- ADLB_SUBSET %>%
mutate(BASE2 = ifelse(toupper(AVISIT) == "SCREENING" & is.na(BASE2), AVAL, BASE2) %keep_label% BASE2) %>%
mutate(CHG2 = ifelse(toupper(AVISIT) == "SCREENING" & is.na(CHG2), 0, CHG2) %keep_label% CHG2) %>%
mutate(PCHG2 = ifelse(toupper(AVISIT) == "SCREENING" & is.na(PCHG2), 0, PCHG2) %keep_label% PCHG2) %>%
mutate(BASE = ifelse(toupper(AVISIT) == "BASELINE" & is.na(BASE), AVAL, BASE) %keep_label% BASE) %>%
mutate(CHG = ifelse(toupper(AVISIT) == "BASELINE" & is.na(CHG), 0, CHG) %keep_label% CHG) %>%
mutate(PCHG = ifelse(toupper(AVISIT) == "BASELINE" & is.na(PCHG), 0, PCHG) %keep_label% PCHG) %>%
mutate(TRTORD = TRTORD %make_label% "Treatment Order")
mutate(BASE2 = .keep_label(ifelse(toupper(AVISIT) == "SCREENING" & is.na(BASE2), AVAL, BASE2), BASE2)) %>%
mutate(CHG2 = .keep_label(ifelse(toupper(AVISIT) == "SCREENING" & is.na(CHG2), 0, CHG2), CHG2)) %>%
mutate(PCHG2 = .keep_label(ifelse(toupper(AVISIT) == "SCREENING" & is.na(PCHG2), 0, PCHG2), PCHG2)) %>%
mutate(BASE = .keep_label(ifelse(toupper(AVISIT) == "BASELINE" & is.na(BASE), AVAL, BASE), BASE)) %>%
mutate(CHG = .keep_label(ifelse(toupper(AVISIT) == "BASELINE" & is.na(CHG), 0, CHG), CHG)) %>%
mutate(PCHG = .keep_label(ifelse(toupper(AVISIT) == "BASELINE" & is.na(PCHG), 0, PCHG), PCHG)) %>%
mutate(TRTORD = .make_label(TRTORD, "Treatment Order"))

# Inconsequential Warning issued: Warning: "PARAMCD" has different attributes on LHS and RHS of join.
# merge minimum AVAL value onto the ADLB data to calculate the log2 variables. preserve the variable order
ADLB_SUPED2 <- inner_join(PARAM_MINS, ADLB_SUPED1, by = "PARAMCD")[, union(names(ADLB_SUPED1), names(PARAM_MINS))] %>%
# visit values
# excludes biomarkers where log2 is not appropriate: for example assay value already log2
mutate(AVALL2 = ifelse(PARAMCD %in% exclude_l2, AVAL,
# excludes biomarkers where log2 is not appropriate: for example CHG type assay
ifelse(PARAMCD %in% exclude_chg, NA,
ifelse(AVAL == 0 & AVAL_MIN > 0, log2(AVAL_MIN / 2),
# would be taking log2 of 0 or negative value so set to NA
ifelse(AVAL == 0 & AVAL_MIN <= 0, NA,
ifelse(AVAL > 0, log2(AVAL), NA)
mutate(AVALL2 = .make_label(
ifelse(PARAMCD %in% .exclude_l2, AVAL,
# excludes biomarkers where log2 is not appropriate: for example CHG type assay
ifelse(PARAMCD %in% .exclude_chg, NA,
ifelse(AVAL == 0 & AVAL_MIN > 0, log2(AVAL_MIN / 2),
# would be taking log2 of 0 or negative value so set to NA
ifelse(AVAL == 0 & AVAL_MIN <= 0, NA,
ifelse(AVAL > 0, log2(AVAL), NA)
)
)
)
)
) %make_label% "Log2 of AVAL") %>%
), "Log2 of AVAL"
)) %>%
# baseline values
mutate(BASEL2 = ifelse(PARAMCD %in% exclude_l2, BASE,
ifelse(PARAMCD %in% exclude_chg, NA,
mutate(BASEL2 = .make_label(ifelse(PARAMCD %in% .exclude_l2, BASE,
ifelse(PARAMCD %in% .exclude_chg, NA,
ifelse(BASE == 0 & AVAL_MIN > 0, log2(AVAL_MIN / 2),
ifelse(BASE == 0 & AVAL_MIN <= 0, NA,
ifelse(BASE > 0, log2(BASE), NA)
)
)
)
) %make_label% "Log2 of BASE") %>%
), "Log2 of BASE")) %>%
# screening
mutate(BASE2L2 = ifelse(PARAMCD %in% exclude_l2, BASE2,
ifelse(PARAMCD %in% exclude_chg, NA,
ifelse(BASE2 == 0 & AVAL_MIN > 0, log2(AVAL_MIN / 2),
ifelse(BASE2 == 0 & AVAL_MIN <= 0, NA,
ifelse(BASE2 > 0, log2(BASE2), NA)
mutate(BASE2L2 = .make_label(
ifelse(PARAMCD %in% .exclude_l2, BASE2,
ifelse(PARAMCD %in% .exclude_chg, NA,
ifelse(BASE2 == 0 & AVAL_MIN > 0, log2(AVAL_MIN / 2),
ifelse(BASE2 == 0 & AVAL_MIN <= 0, NA,
ifelse(BASE2 > 0, log2(BASE2), NA)
)
)
)
)
) %make_label% "Log2 of BASE2") %>%
mutate(AVAL_MIN = AVAL_MIN %make_label% "Minimum AVAL Within PARAMCD")
), "Log2 of BASE2"
)) %>%
mutate(AVAL_MIN = .make_label(AVAL_MIN, "Minimum AVAL Within PARAMCD"))

# create final data set used by goshawk
# all data set passed into a goshawk app must have all of the columns
# `AVISITCD`, `BASE`, `BASE2`, `AVALU`, `LBSTRESC`, `LOQFL`, `CHG2`, and `PCHG2`.
ADLB <- ADLB_SUPED2 %>%
mutate(
TRT01P = as.character(arm_mapping[match(TRT01P, names(arm_mapping))]),
TRT01P = factor(TRT01P) %>% reorder(TRTORD) %make_label% "Planned Treatment for Period 01",
TRT01P = .make_label(factor(TRT01P) %>% reorder(TRTORD), "Planned Treatment for Period 01"),
TRT01A = as.character(arm_mapping[match(TRT01A, names(arm_mapping))]),
TRT01A = factor(TRT01A) %>% reorder(TRTORD) %make_label% "Actual Treatment for Period 01",
LOQFL = LOQFL %make_label% "Limit of Quantification",
AVISITCD = factor(AVISITCD) %>% reorder(AVISITCDN) %make_label% "Analysis Visit Window Code",
AVISITCDN = AVISITCDN %make_label% "Analysis Visit Window Code (N)",
BASE2 = BASE2 %make_label% "Screening Value",
CHG2 = CHG2 %make_label% "Absolute Change from Screening",
PCHG2 = PCHG2 %make_label% "Percent Change from Screening"
TRT01A = .make_label(factor(TRT01A) %>% reorder(TRTORD), "Actual Treatment for Period 01"),
LOQFL = .make_label(LOQFL, "Limit of Quantification"),
AVISITCD = .make_label(factor(AVISITCD) %>% reorder(AVISITCDN), "Analysis Visit Window Code"),
AVISITCDN = .make_label(AVISITCDN, "Analysis Visit Window Code (N)"),
BASE2 = .make_label(BASE2, "Screening Value"),
CHG2 = .make_label(CHG2, "Absolute Change from Screening"),
PCHG2 = .make_label(PCHG2, "Percent Change from Screening")
)

# add LLOQ and ULOQ variables to support adding horizontal/vertical range lines
Expand All @@ -279,15 +283,13 @@ data <- within(data, {
################################################################################
})

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

## App configuration ----
ADSL <- data[["ADSL"]]
ADLB <- data[["ADLB"]]
exclude_l2 <- data[["exclude_l2"]]
exclude_chg <- data[["exclude_chg"]]
.exclude_l2 <- data[[".exclude_l2"]]
.exclude_chg <- data[[".exclude_chg"]]

# study information: used to label app and in "Example Info Page" module
ATYPE <- "Exploratory" # define here and in the "# code >" section below
Expand Down Expand Up @@ -408,7 +410,7 @@ paramcd_list <- paramDict$PARAMCD

# create list of biomarkers excluded from log2 transformation
paramexcldDict <- paramDict %>%
filter(PARAMCD %in% c(exclude_l2, exclude_chg))
filter(PARAMCD %in% c(.exclude_l2, .exclude_chg))
paramexcld_list <- paramexcldDict$PARAM
paramcdexcld_list <- paramexcldDict$PARAMCD

Expand Down Expand Up @@ -775,29 +777,4 @@ app <- teal::init(
)
)

# Add context sensitive help code
body(app$server)[[length(body(app$server)) + 1]] <- quote(
observeEvent(input$showAnlVarLegendModal, {
showModal(modalDialog(
title = "Analysis Variable Pull Down Menu",
tags$p(
"These variables will appear in the Visualizations 'Analysis Variable' pull down menu. This legend
provides the variable labels to help clarify the short analysis variable names displayed in the pull down
menu."
),
p("BASE2 = Screening Visit Value"),
p("BASE2L2 = Log2(BASE2)"),
p("CHG2 = Change from Screening"),
p("PCHG2 = % Change from Screening"),
p("BASE = Baseline Visit Value"),
p("BASEL2 = Log2(BASE)"),
p("CHG = Change from Baseline"),
p("PCHG = % Change from Baseline"),
p("AVAL = Visit Values"),
p("AVALL2 = Log2(AVAL)"),
easyClose = TRUE
))
})
)

shinyApp(app$ui, app$server)
4 changes: 1 addition & 3 deletions patient-profile/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ data <- within(data, {
teal.data::col_labels(ADMH[c("MHDISTAT")]) <- c("Status of Disease")
})

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

## App configuration ----
ADSL <- data[["ADSL"]]
Expand Down
Loading

0 comments on commit 2612746

Please sign in to comment.