From 5837fd2c7823d6a8cafb7df2181b431a3476a8a6 Mon Sep 17 00:00:00 2001 From: Kyle Barrett Date: Tue, 13 Feb 2024 11:37:43 -0500 Subject: [PATCH] update handling of data paths processes from control stream files --- R/run-nmtran.R | 44 ++++++++++++++++++++++++++++++-------------- man/locate_nmtran.Rd | 23 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 man/locate_nmtran.Rd diff --git a/R/run-nmtran.R b/R/run-nmtran.R index 34013bf21..317b006b4 100644 --- a/R/run-nmtran.R +++ b/R/run-nmtran.R @@ -41,15 +41,19 @@ run_nmtran <- function( on.exit(unlink(temp_folder, recursive = TRUE, force = TRUE)) } - # copy model and dataset + # Copy model file.copy(mod_path, temp_folder, overwrite = TRUE) - file.copy(data_path, temp_folder) - # overwrite $DATA of new model - modify_data_path_ctl( - mod_path = file.path(temp_folder, basename(mod_path)), - data_path = basename(data_path) - ) + # Copy dataset & overwrite $DATA record of new model + # NMTRAN will error if data cannot be found + if(fs::file_exists(data_path)){ + file.copy(data_path, temp_folder, overwrite = TRUE) + # overwrite $DATA record of new model + modify_data_path_ctl( + mod_path = file.path(temp_folder, basename(mod_path)), + data_path = basename(data_path) + ) + } # Run NMTRAN nmtran_results <- c( @@ -74,17 +78,23 @@ run_nmtran <- function( #' #' @inheritParams run_nmtran #' -#' @noRd +#' @keywords internal locate_nmtran <- function(.mod = NULL, .config_path = NULL, nmtran_exe = NULL){ if(is.null(nmtran_exe)){ - check_model_object(.mod, "bbi_nonmem_model") - model_dir <- get_model_working_directory(.mod) + if(!is.null(.mod)){ + check_model_object(.mod, "bbi_nonmem_model") + model_dir <- get_model_working_directory(.mod) + } config_path <- .config_path %||% file.path(model_dir, "bbi.yaml") if(!file_exists(config_path)){ - stop(paste("No bbi configuration was found in the execution directory.", - "Please run `bbi_init()` with the appropriate directory to continue.")) + rlang::abort( + c( + "x" = "No bbi configuration was found in the execution directory.", + "i" = "Please run `bbi_init()` with the appropriate directory to continue." + ) + ) } if(!is.null(.config_path)){ @@ -189,10 +199,16 @@ get_data_path_from_ctl <- function(.mod){ data_path_norm <- fs::path_norm(file.path(mod_path, data_path)) if(!fs::file_exists(data_path_norm)){ - stop(glue("Could not find data at {data_path_norm}")) + # The first error message line is what NMTRAN would return in this situation + rlang::warn( + c( + "x" = "Input data file does not exist or cannot be opened", + "i" = glue("Referenced input data path: {data_path_norm}") + ) + ) } - return(data_path_norm) + return(as.character(data_path_norm)) } #' Modify the specified data path in a control stream file diff --git a/man/locate_nmtran.Rd b/man/locate_nmtran.Rd new file mode 100644 index 000000000..a1aeb3571 --- /dev/null +++ b/man/locate_nmtran.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/run-nmtran.R +\name{locate_nmtran} +\alias{locate_nmtran} +\title{Search for and validate existence of an \code{NMTRAN} executable} +\usage{ +locate_nmtran(.mod = NULL, .config_path = NULL, nmtran_exe = NULL) +} +\arguments{ +\item{.mod}{a \code{bbr} model object} + +\item{.config_path}{Path to a bbi configuration file. If \code{NULL}, the +default, will attempt to use a \code{bbi.yaml} in the same directory as the +model.} + +\item{nmtran_exe}{Path to an \code{NMTRAN} executable. If \code{NULL}, will look for a +\code{bbi.yaml} file in the same directory as the model.} +} +\description{ +If \code{nmtran_exe = NULL}, this will look for a \code{bbi.yaml} file in the same +directory as the model. +} +\keyword{internal}