Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .lintr
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ linters:
seq_linter = seq_linter()
)
encoding: "UTF-8"
exclusions: list("R/PKNCA_extra_parameters.R", "tests/testthat/test-PKNCA_extra_parameters.R", "inst/www/templates/script_template.R")
exclusions: list("R/PKNCA_extra_parameters.R", "tests/testthat/test-PKNCA_extra_parameters.R", "inst/www/templates/script_template.R", "inst/www/templates/script_template_sdtm.R")
16 changes: 14 additions & 2 deletions R/get_session_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,21 @@ get_settings_code <- function(
settings_file_path,
data_path,
output_path = "settings_code.R",
template_path = system.file("www/templates/script_template.R", package = "aNCA")
template_path = NULL
) {
settings <- read_settings(settings_file_path)

# Auto-select template based on input_mode if not explicitly provided
if (is.null(template_path)) {
mode <- settings[["input_mode"]] %||% "adnca"
template_file <- if (identical(mode, "sdtm")) {
"www/templates/script_template_sdtm.R"
} else {
"www/templates/script_template.R"
}
template_path <- system.file(template_file, package = "aNCA")
}

session <- list(
settings = settings[["settings"]],
slope_rules = settings[["slope_rules"]],
Expand All @@ -228,7 +239,8 @@ get_settings_code <- function(
extra_vars_to_keep = c(
settings[["mapping"]][["Grouping_Variables"]], "DOSEA", "ATPTREF", "ROUTE"
),
time_duplicate_rows = settings[["time_duplicate_keys"]]
time_duplicate_rows = settings[["time_duplicate_keys"]],
sdtm_metabolites = settings[["sdtm_metabolites"]]
)

get_code(
Expand Down
31 changes: 27 additions & 4 deletions inst/shiny/functions/zip-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,19 @@ prepare_export_files <- function(target_dir,
if ("r_script" %in% input$res_tree) {
progress$set(message = "Creating exports...",
detail = "Saving R script...")
saveRDS(session$userData$raw_data, file.path(target_dir, "input_data.rds"))
mode <- session$userData$input_mode %||% "adnca"
if (mode == "sdtm") {
sdtm <- session$userData$sdtm_raw
if (!is.null(sdtm)) {
saveRDS(sdtm$pc, file.path(target_dir, "input_pc.rds"))
saveRDS(sdtm$ex, file.path(target_dir, "input_ex.rds"))
if (!is.null(sdtm$dm)) {
saveRDS(sdtm$dm, file.path(target_dir, "input_dm.rds"))
}
}
} else {
saveRDS(session$userData$raw_data, file.path(target_dir, "input_data.rds"))
}
.export_script(target_dir, session)
}
} else {
Expand Down Expand Up @@ -569,9 +581,15 @@ prepare_export_files <- function(target_dir,
slope_rules = session$userData$slope_rules(),
filters = session$userData$applied_filters,
time_duplicate_keys = session$userData$time_duplicate_keys,
nca_ran = isTRUE(session$userData$nca_ran)
nca_ran = isTRUE(session$userData$nca_ran),
input_mode = session$userData$input_mode %||% "adnca"
)

# Store SDTM-specific settings
if (identical(payload$input_mode, "sdtm")) {
payload$sdtm_metabolites <- session$userData$sdtm_metabolites
}

dataset_name <- session$userData$dataset_filename %||% ""

active_tab <- tryCatch(
Expand Down Expand Up @@ -631,9 +649,14 @@ prepare_export_files <- function(target_dir,
#' @keywords internal
#' @noRd
.export_script <- function(target_dir, session) {
template_path <- "www/templates/script_template.R"
mode <- session$userData$input_mode %||% "adnca"
template_file <- if (mode == "sdtm") {
"www/templates/script_template_sdtm.R"
} else {
"www/templates/script_template.R"
}
get_session_code(
template_path = system.file(template_path, package = "aNCA"),
template_path = system.file(template_file, package = "aNCA"),
session,
file.path(target_dir, "session_code.R")
)
Expand Down
36 changes: 28 additions & 8 deletions inst/shiny/modules/tab_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,9 @@ tab_data_server <- function(id) {
id = "column_mapping",
adnca_data = uploaded_data$adnca_raw,
imported_mapping = imported_mapping,
trigger = trigger_mapping_submit
trigger = trigger_mapping_submit,
input_mode = uploaded_data$input_mode,
sdtm_raw = uploaded_data$sdtm_raw
)
#' Reactive value for the processed dataset
adnca_mapped <- column_mapping$processed_data
Expand Down Expand Up @@ -419,12 +421,29 @@ tab_data_server <- function(id) {
log_trace("Creating PKNCA::data object.")

tryCatch({
pknca_object <- PKNCA_create_data_object(
adnca_data = uploaded_data$adnca_raw(),
mapping = column_mapping$mapping(),
applied_filters = filtering_result$applied_filters(),
time_duplicate_rows = column_mapping$time_duplicate_rows()
)
mode <- uploaded_data$input_mode()

if (mode == "sdtm") {
# SDTM: PKNCAdata was already created in the mapping step;
# update concentration data with filtered rows
log_trace("SDTM mode: retrieving PKNCAdata from mapping step...")
pknca_object <- column_mapping$sdtm_pknca_data()
req(pknca_object)
pknca_object$conc$data <- processed_data()
log_trace(
"SDTM: PKNCAdata updated with filtered data (",
nrow(processed_data()), " rows)."
)
} else {
# ADNCA: create PKNCAdata from mapped data
pknca_object <- PKNCA_create_data_object(
adnca_data = uploaded_data$adnca_raw(),
mapping = column_mapping$mapping(),
applied_filters = filtering_result$applied_filters(),
time_duplicate_rows = column_mapping$time_duplicate_rows()
)
}

pknca_object$units <- .simplify_volume_units(pknca_object$units)
log_success("PKNCA data object created.")

Expand Down Expand Up @@ -453,7 +472,8 @@ tab_data_server <- function(id) {
adnca_raw = uploaded_data$adnca_raw,
extra_group_vars = extra_group_vars,
settings_override = uploaded_data$settings_override,
auto_replay_ready = auto_replay_ready
auto_replay_ready = auto_replay_ready,
input_mode = uploaded_data$input_mode
)
})
}
Loading
Loading