When creating apps which do not use DDL
, once the
datasets are created there is often some pre-processing required before
initializing the teal app. Similarly, in the case of delayed data
additional code instructions to pre-process data can be added to
DDL
objects which will be run after the data is loaded,
which may happen after the launching of the shiny app or when the
pull()
method is called.
mutate_dataset
: Individual datasets can be processed
using the mutate_dataset
function. For reproducibility to
be maintained with mutate_dataset
, all pre-processing code
should modify one dataset at a time.library(scda)
library(teal.data)
library(magrittr)
<- callable_function(function() synthetic_cdisc_data("latest")$adsl)
adsl_cf <- cdisc_dataset_connector(
adsl dataname = "ADSL",
pull_callable = adsl_cf,
keys = get_cdisc_keys("ADSL")
%>%
) mutate_dataset("ADSL$SEX <- as.factor(ADSL$SEX)")
<- callable_function(function() synthetic_cdisc_data("latest")$adae)
adae_cf <- cdisc_dataset_connector(
adae dataname = "ADAE",
pull_callable = adae_cf,
keys = get_cdisc_keys("ADAE")
%>%
) mutate_dataset("ADAE$X <- rep(ADSL$SEX[1])", vars = list(ADSL = adsl))
$pull() %>%
adslget_raw_data() %>%
head(n = 3)
## # A tibble: 3 × 44
## STUDYID USUBJID SUBJID SITEID AGE AGEU SEX RACE ETHNIC COUNTRY DTHFL
## <chr> <chr> <chr> <chr> <int> <fct> <fct> <fct> <fct> <fct> <fct>
## 1 AB12345 AB12345-CH… id-128 CHN-3 32 YEARS M ASIAN NOT H… CHN N
## 2 AB12345 AB12345-CH… id-262 CHN-15 35 YEARS M BLAC… NOT H… CHN N
## 3 AB12345 AB12345-RU… id-378 RUS-3 30 YEARS F ASIAN NOT H… RUS N
## # … with 33 more variables: INVID <chr>, INVNAM <chr>, ARM <fct>, ARMCD <fct>,
## # ACTARM <fct>, ACTARMCD <fct>, TRT01P <fct>, TRT01A <fct>, REGION1 <fct>,
## # STRATA1 <fct>, STRATA2 <fct>, BMRKR1 <dbl>, BMRKR2 <fct>, ITTFL <fct>,
## # SAFFL <fct>, BMEASIFL <fct>, BEP01FL <fct>, RANDDT <date>, TRTSDTM <dttm>,
## # TRTEDTM <dttm>, EOSSTT <fct>, EOTSTT <fct>, EOSDT <date>, EOSDY <int>,
## # DCSREAS <fct>, DTHDT <date>, DTHCAUS <fct>, DTHCAT <fct>, LDDTHELD <int>,
## # LDDTHGR1 <fct>, LSTALVDT <date>, DTHADY <int>, study_duration_secs <dbl>
$pull() %>%
adaeget_raw_data() %>%
head(n = 3)
## # A tibble: 3 × 75
## STUDYID USUBJID SUBJID SITEID AGE AGEU SEX RACE ETHNIC COUNTRY DTHFL
## <chr> <chr> <chr> <chr> <int> <fct> <fct> <fct> <fct> <fct> <fct>
## 1 AB12345 AB12345-BR… id-134 BRA-1 47 YEARS M WHITE NOT H… BRA N
## 2 AB12345 AB12345-BR… id-134 BRA-1 47 YEARS M WHITE NOT H… BRA N
## 3 AB12345 AB12345-BR… id-134 BRA-1 47 YEARS M WHITE NOT H… BRA N
## # … with 64 more variables: INVID <chr>, INVNAM <chr>, ARM <fct>, ARMCD <fct>,
## # ACTARM <fct>, ACTARMCD <fct>, TRT01P <fct>, TRT01A <fct>, REGION1 <fct>,
## # STRATA1 <fct>, STRATA2 <fct>, BMRKR1 <dbl>, BMRKR2 <fct>, ITTFL <fct>,
## # SAFFL <fct>, BMEASIFL <fct>, BEP01FL <fct>, RANDDT <date>, TRTSDTM <dttm>,
## # TRTEDTM <dttm>, EOSSTT <fct>, EOTSTT <fct>, EOSDT <date>, EOSDY <int>,
## # DCSREAS <fct>, DTHDT <date>, DTHCAUS <fct>, DTHCAT <fct>, LDDTHELD <int>,
## # LDDTHGR1 <fct>, LSTALVDT <date>, DTHADY <int>, study_duration_secs <dbl>, …
mutate_data
: Collections of datasets should only be
processed using the mutate_data
function:cdisc_data(adsl, adae, check = TRUE) %>%
mutate_data("ADAE$x <- ADSL$SUBJID[1]")
The code is processed in the order the datasets are pulled so if
there are dependencies between datasets it matters the order in which
pre-processing code is added to the CDISCTealData
object
just as order matters when the arguments are inputted to the
cdisc_data
function to create the
CDISCTealData
object.
Finally, the code
argument directly in
teal_data
and cdisc_data
call does not need to
be used for DDL
because data loaded with DDL
are reproducible by design. Because of this, it is recommended to set
argument check = TRUE
inside cdisc_data
function when creating apps with DDL
.
It may be required to generate a delayed data object that is dependent on some other delayed object or some constant value.
For this, when creating your delayed data object it’s possible to
supply the additional variables that are to be accessed during the data
loading (pull) using additional arguments through ...
:
get_code(adsl)
## [1] "ADSL <- (function() synthetic_cdisc_data(\"latest\")$adsl)()\nADSL$SEX <- as.factor(ADSL$SEX)"
<- callable_function(
pull_fun_adae function() {
synthetic_cdisc_data("latest")$adae
}
)<- dataset_connector(
adae dataname = "ADAE",
pull_callable = pull_fun_adae,
keys = get_cdisc_keys("ADAE")
)
get_code(adae)
## [1] "ADAE <- (function() {\n synthetic_cdisc_data(\"latest\")$adae\n})()"
It’s also possible to supply these additional variables after
creating your object using the mutate_dataset
function.
<- Sys.Date() # constant value stored as a variable in the current session
last_run
<- callable_function(function() synthetic_cdisc_data("latest")$adsl)
adsl_cf <- cdisc_dataset_connector(
adsl dataname = "ADSL",
pull_callable = adsl_cf,
keys = get_cdisc_keys("ADSL")
%>%
) mutate_dataset("ADSL$last_run <- last_run", vars = list(last_run = last_run))
cat(get_code(adsl))
## ADSL <- (function() synthetic_cdisc_data("latest")$adsl)()
## last_run <- structure(19188, class = "Date")
## ADSL$last_run <- last_run
# compared to evaluating the variable at the time of loading
<- callable_function(function() synthetic_cdisc_data("latest")$adsl)
adsl_cf <- cdisc_dataset_connector(
adsl dataname = "ADSL",
pull_callable = adsl_cf,
keys = get_cdisc_keys("ADSL")
%>%
) mutate_dataset("last_run <- Sys.Date()\nADSL$last_run <- last_run")
%>%
adsl get_code() %>%
cat()
## ADSL <- (function() synthetic_cdisc_data("latest")$adsl)()
## last_run <- Sys.Date()
## ADSL$last_run <- last_run
This is also required when creating the object depends on another delayed data object:
<- synthetic_cdisc_data("latest")$adsl
adsl <- callable_function(function() synthetic_cdisc_data("latest")$adae)
adae_cf <- cdisc_dataset_connector(
adae dataname = "ADAE",
pull_callable = adae_cf,
keys = get_cdisc_keys("ADAE")
%>%
) mutate_dataset("ADAE$n <- nrow(ADSL)")
cat(get_code(adae)) # the code returned by `adae` is not sufficient to reproduce `adae`
## ADAE <- (function() synthetic_cdisc_data("latest")$adae)()
## ADAE$n <- nrow(ADSL)
<- callable_function(function() synthetic_cdisc_data("latest")$adsl)
adsl_cf <- cdisc_dataset_connector(
adsl dataname = "ADSL",
pull_callable = adsl_cf,
keys = get_cdisc_keys("ADSL")
)<- callable_function(function() synthetic_cdisc_data("latest")$adae)
adae_cf <- cdisc_dataset_connector(
adae dataname = "ADAE",
pull_callable = adae_cf,
keys = get_cdisc_keys("ADAE")
%>%
) mutate_dataset("ADAE$n <- nrow(ADSL)", vars = list(ADSL = adsl))
cat(get_code(adae)) # this code can be run independently
## ADAE <- (function() synthetic_cdisc_data("latest")$adae)()
## ADSL <- (function() synthetic_cdisc_data("latest")$adsl)()
## ADAE$n <- nrow(ADSL)
Related to this idea, it is possible to provide the code on a
Data
level. However, this will always return all the code
used to generate all the datasets in the object:
<- cdisc_data(
adsl_adae
adsl,
adae%>% mutate_data("ADAE$avg_age <- mean(ADAE$AGE)")
)
# the output for all 3 are the same
%>%
adsl_adae get_code() %>%
cat()
## ADSL <- (function() synthetic_cdisc_data("latest")$adsl)()
## ADAE <- (function() synthetic_cdisc_data("latest")$adae)()
## ADAE$n <- nrow(ADSL)
## ADAE$avg_age <- mean(ADAE$AGE)
%>%
adsl_adae get_code(dataname = "ADAE") %>%
cat()
## ADSL <- (function() synthetic_cdisc_data("latest")$adsl)()
## ADAE <- (function() synthetic_cdisc_data("latest")$adae)()
## ADAE$n <- nrow(ADSL)
## ADAE$avg_age <- mean(ADAE$AGE)
%>%
adsl_adae get_code(dataname = "ADSL") %>%
cat()
## ADSL <- (function() synthetic_cdisc_data("latest")$adsl)()
## ADAE <- (function() synthetic_cdisc_data("latest")$adae)()
## ADAE$n <- nrow(ADSL)
## ADAE$avg_age <- mean(ADAE$AGE)
The better approach would be to supply the code on a
Dataset
level. This ensures that the code accessed on a
dataset level only contains the snippets that pertains to itself:
<- cdisc_data(
adsl_adae
adsl,%>% mutate_dataset("ADAE$avg_age <- mean(ADAE$AGE)")
adae
)
%>%
adsl_adae get_code() %>%
cat()
## ADSL <- (function() synthetic_cdisc_data("latest")$adsl)()
## ADAE <- (function() synthetic_cdisc_data("latest")$adae)()
## ADAE$n <- nrow(ADSL)
## ADAE$avg_age <- mean(ADAE$AGE)
%>%
adsl_adae get_code("ADAE") %>%
cat()
## ADSL <- (function() synthetic_cdisc_data("latest")$adsl)()
## ADAE <- (function() synthetic_cdisc_data("latest")$adae)()
## ADAE$n <- nrow(ADSL)
## ADAE$avg_age <- mean(ADAE$AGE)
%>%
adsl_adae get_code("ADSL") %>%
cat()
## ADSL <- (function() synthetic_cdisc_data("latest")$adsl)()
Related to this idea, the delayed data object needs to be supplied
with the code needed to reproduce the data. This can be provided at the
Dataset
level or the Data
level.
Below is a comparison of these two approaches:
<- synthetic_cdisc_data("latest")$adsl
adsl cdisc_dataset("ADSL", adsl) %>% get_code() # no reproducible code
## [1] ""
# provide the code to reproduce the data:
cdisc_dataset("ADSL", adsl,
code = "ADSL <- synthetic_cdisc_data(\"latest\")$adsl"
%>%
) get_code()
## [1] "ADSL <- synthetic_cdisc_data(\"latest\")$adsl"
# it's possible to supply the code at the `Data` level:
<- synthetic_cdisc_data("latest")$adae
adae <- cdisc_data(
adsl_adae cdisc_dataset("ADSL", adsl),
cdisc_dataset("ADAE", adae),
code = "ADSL <- synthetic_cdisc_data(\"latest\")$adsl\nADAE <- synthetic_cdisc_data(\"latest\")$adae"
)
%>%
adsl_adae get_code() %>%
cat()
## ADSL <- synthetic_cdisc_data("latest")$adsl
## ADAE <- synthetic_cdisc_data("latest")$adae
# but it's not possible then to access the code at a `Dataset` level:
%>%
adsl_adae get_code("ADSL") %>%
cat()
## ADSL <- synthetic_cdisc_data("latest")$adsl
## ADAE <- synthetic_cdisc_data("latest")$adae
# this can be avoided by storing the code like so:
<- cdisc_data(
adsl_adae cdisc_dataset("ADSL", adsl, code = "ADSL <- synthetic_cdisc_data(\"latest\")$adsl"),
cdisc_dataset("ADAE", adae, code = "ADAE <- synthetic_cdisc_data(\"latest\")$adsl")
)
%>%
adsl_adae get_code("ADSL") %>%
cat()
## ADSL <- synthetic_cdisc_data("latest")$adsl