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

Add targets branching #15

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Suggests:
testr,
testthat (>= 3.0.0),
tidyr,
withr
withr,
whisker
VignetteBuilder:
knitr
Remotes:
Expand Down
4 changes: 3 additions & 1 deletion R/apply_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ apply_stats <-
type = c("stat_by_strata_by_trt",
"stat_by_strata_across_trt",
"stat_across_strata_across_trt")) {

checkmate::assert_data_table(ep)
# If no functions are given by the user, no results table needs to be
# produced
nm <- names(ep)
if (length(nm) <= 2 &&
if (length(nm) <= 3 &&
nm[1] == "SKIP_") {
return(data.table(NULL))
}
Expand Down
2 changes: 1 addition & 1 deletion R/prepare_for_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ prepare_for_stats <- function(

# If no functions of the type are supplied, return early
if(nrow(ep_fn)==0){
return(data.table::data.table(SKIP_=TRUE))
return(data.table::data.table(SKIP_=TRUE, endpoint_spec_id="DUMMY_"))
}


Expand Down
158 changes: 116 additions & 42 deletions inst/templates/template-pipeline.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,95 +6,169 @@ chef::check_duplicate_functions(fun_dir)
## Load your R files
lapply(list.files(normalizePath(fun_dir), full.names = TRUE), source)

tar_option_set(packages = c("chef", "data.table"), format = "qs")
targets::tar_option_set(packages = c("chef", "data.table"), format = "qs")
list(
targets::tar_target(ep,
mk_endpoint_def(),
),
targets::tar_target(ep_id, add_id(ep)),
targets::tar_target(ep_fn_map,
unnest_endpoint_functions(ep_id),
cue = targets::tar_cue(mode = "always")),
mk_endpoint_def()),
tarchetypes::tar_group_by(ep_id, add_id(ep), endpoint_spec_id),
targets::tar_target(
ep_fn_map,
unnest_endpoint_functions(data.table::as.data.table(ep_id)),
cue = targets::tar_cue(mode = "always"),
pattern = map(ep_id)
),

targets::tar_target(user_def_fn,
mk_userdef_fn_dt(ep_fn_map, env = environment())),

targets::tar_target(fn_map,
merge(ep_fn_map[, .(endpoint_spec_id, fn_hash)], user_def_fn, by = c("fn_hash"))),
tarchetypes::tar_group_by(
fn_map_tibble,
merge(ep_fn_map[, .(endpoint_spec_id, fn_hash)],
user_def_fn,
by = c("fn_hash")),
endpoint_spec_id
),

targets::tar_target(
fn_map,
data.table::as.data.table(fn_map_tibble),
pattern = map(fn_map_tibble)
),

# targets::tar_target(fn_map, as.data.table(fn_map_), pattern = map(fn_map_)),
targets::tar_target(
study_data,
fetch_db_data(study_metadata = ep_id$study_metadata[[1]], fn_dt = user_def_fn),
cue = targets::tar_cue(mode = "always")
),

targets::tar_target(ep_and_data,
filter_db_data(ep_id, ep_fn_map, study_data)),
targets::tar_target(
ep_and_data,
filter_db_data(data.table::as.data.table(ep_id), ep_fn_map, study_data),
pattern = map(ep_id)
),

targets::tar_target(ep_with_data_key, ep_and_data$ep, pattern = map(ep_and_data)),
targets::tar_target(
analysis_data_container,
ep_and_data$analysis_data_container,
pattern = map(ep_and_data)
),

targets::tar_target(ep_with_data_key, ep_and_data$ep),
targets::tar_target(analysis_data_container, ep_and_data$analysis_data_container),
targets::tar_target(
ep_expanded,
expand_over_endpoints(ep_with_data_key, analysis_data_container),
pattern = map(ep_with_data_key, analysis_data_container)
),

targets::tar_target(ep_expanded, expand_over_endpoints(ep_with_data_key, analysis_data_container)),
targets::tar_target(
ep_event_index,
add_event_index(ep_expanded, analysis_data_container),
pattern = map(ep_expanded, analysis_data_container)
),

targets::tar_target(ep_event_index, add_event_index(ep_expanded, analysis_data_container)),

# `fn_map` has to be supplied to the command function of tar_targets without the
# `tar_group` variable. This is because this variable exists in the ep data
# model, and fn_map is merged onto ep. However, since the data.tables are not
# keyed by `tar_group`, this cause naming problems. It's easier to just pass
# the fn_map without this variable since no derivation of fn_map is passed to
# downstream targets.
targets::tar_target(
ep_crit_endpoint,
apply_criterion_endpoint(ep_event_index, analysis_data_container, fn_map)
apply_criterion_endpoint(ep_event_index, analysis_data_container, fn_map[,!"tar_group"]),
pattern = map(ep_event_index, analysis_data_container, fn_map)
),
targets::tar_target(
ep_crit_by_strata_by_trt,
apply_criterion_by_strata(ep_crit_endpoint, analysis_data_container, fn_map)
apply_criterion_by_strata(ep_crit_endpoint, analysis_data_container, fn_map[,!"tar_group"]),
pattern = map(ep_crit_endpoint, analysis_data_container, fn_map)
),

targets::tar_target(
ep_crit_by_strata_across_trt,
apply_criterion_by_strata(ep_crit_by_strata_by_trt, analysis_data_container, fn_map, type = "by_strata_across_trt")
apply_criterion_by_strata(
ep_crit_by_strata_by_trt,
analysis_data_container,
fn_map[,!"tar_group"],
type = "by_strata_across_trt"
),
pattern = map(ep_crit_by_strata_by_trt, analysis_data_container, fn_map)
),

tarchetypes::tar_group_size(
targets::tar_target(
ep_prep_by_strata_by_trt,
prepare_for_stats(ep_crit_by_strata_across_trt,
analysis_data_container,
fn_map,
type = "stat_by_strata_by_trt"),
size = {{branch_group_size}}
prepare_for_stats(
ep_crit_by_strata_across_trt,
analysis_data_container,
fn_map[,!"tar_group"],
type = "stat_by_strata_by_trt"
),
pattern = map(
ep_crit_by_strata_across_trt,
analysis_data_container,
fn_map
)
),

tarchetypes::tar_group_size(
targets::tar_target(
ep_prep_by_strata_across_trt,
prepare_for_stats(ep_crit_by_strata_across_trt,
analysis_data_container,
fn_map,
type = "stat_by_strata_across_trt"),
size = {{branch_group_size}}
prepare_for_stats(
ep_crit_by_strata_across_trt,
analysis_data_container,
fn_map[,!"tar_group"],
type = "stat_by_strata_across_trt"
),
pattern = map(
ep_crit_by_strata_across_trt,
analysis_data_container,
fn_map
)
),

tarchetypes::tar_group_size(
targets::tar_target(
ep_prep_across_strata_across_trt,
prepare_for_stats(ep_crit_by_strata_across_trt,
analysis_data_container,
fn_map,
type = "stat_across_strata_across_trt"),
size = {{branch_group_size}}
prepare_for_stats(
ep_crit_by_strata_across_trt,
analysis_data_container,
fn_map[,!"tar_group"],
type = "stat_across_strata_across_trt"
),
pattern = map(
ep_crit_by_strata_across_trt,
analysis_data_container,
fn_map
)
),


targets::tar_target(
ep_stat_by_strata_by_trt,
apply_stats(ep_prep_by_strata_by_trt, analysis_data_container, type = "stat_by_strata_by_trt"),
pattern = map(ep_prep_by_strata_by_trt)
apply_stats(ep_prep_by_strata_by_trt,
analysis_data_container,
type = "stat_by_strata_by_trt"),
pattern = map(ep_prep_by_strata_by_trt,
analysis_data_container)
),

targets::tar_target(
ep_stat_by_strata_across_trt,
apply_stats(ep_prep_by_strata_across_trt, analysis_data_container, type = "stat_by_strata_across_trt"),
pattern = map(ep_prep_by_strata_across_trt)
apply_stats(ep_prep_by_strata_across_trt,
analysis_data_container,
type = "stat_by_strata_across_trt"),
pattern = map(ep_prep_by_strata_across_trt,
analysis_data_container)
),

targets::tar_target(
ep_stat_across_strata_across_trt,
apply_stats(ep_prep_across_strata_across_trt, analysis_data_container, type = "stat_across_strata_across_trt"),
pattern = map(ep_prep_across_strata_across_trt)
apply_stats(
ep_prep_across_strata_across_trt,
analysis_data_container,
type = "stat_across_strata_across_trt"
),
pattern = map(ep_prep_across_strata_across_trt,
analysis_data_container)
),

targets::tar_target(
Expand Down
Loading
Loading