From ee17c2ffd7d97af826522229ee24dbf2d4f72cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CJordan?= Date: Wed, 16 Sep 2026 10:29:19 -0700 Subject: [PATCH 1/4] add FeRx implementation support to model objects Mirrors existing NONMEM pattern: new `ferx` parameter in model_from_api() and new_ode_model(), stored as model attribute for downstream use. Co-Authored-By: Claude Opus 4.5 --- R/model_from_api.R | 6 ++++++ R/new_ode_model.R | 6 +++++- inst/template/R/model.R | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/R/model_from_api.R b/R/model_from_api.R index 081444a7..d814d012 100644 --- a/R/model_from_api.R +++ b/R/model_from_api.R @@ -3,6 +3,7 @@ #' @param url URL or file path to JSON representation of model #' @param model model id (used in messages) #' @param nonmem URL or file path to NONMEM file +#' @param ferx URL or file path to FeRx file #' @param verbose verbosity (T/F) #' @param get_definition return only the model definition, do not compile #' @param to_package compile to package? @@ -15,6 +16,7 @@ model_from_api <- function(url, model = NULL, nonmem = NULL, + ferx = NULL, verbose = TRUE, get_definition = FALSE, to_package = FALSE, @@ -41,6 +43,9 @@ model_from_api <- function(url, if(!is.null(nonmem) && !is.null(def$implementations$nonmem)) { nonmem <- paste(readLines(nonmem), collapse="\n") } + if(!is.null(ferx) && !is.null(def$implementations$ferx)) { + ferx <- paste(readLines(ferx), collapse="\n") + } if(is.null(def$comments)) def$comments <- "" mod <- NULL if(isTRUE(def$build) || install_all) { @@ -85,6 +90,7 @@ model_from_api <- function(url, state_init = def$state_init, verbose = verbose, nonmem = nonmem, + ferx = ferx, int_step_size = def$simulation$int_step_size, comments = stringr::str_replace_all(def$comments, '\\"', ""), version = ifelse(!is.null(def$version), def$version, "0.1.0"), diff --git a/R/new_ode_model.R b/R/new_ode_model.R index 2a852c7f..a6506d83 100644 --- a/R/new_ode_model.R +++ b/R/new_ode_model.R @@ -37,6 +37,7 @@ #' @param verbose show more output #' @param as_is use C-code as-is, don't substitute line-endings or shift indices #' @param nonmem add NONMEM code as attribute to model object +#' @param ferx add FeRx code as attribute to model object #' @param comments comments for model #' @param version number of library #' @param quiet passed on to `system2` as setting for stderr and stdout; how to @@ -85,6 +86,7 @@ new_ode_model <- function (model = NULL, verbose = FALSE, as_is = FALSE, nonmem = NULL, + ferx = NULL, comments = NULL, version = "0.1.0", quiet = "", @@ -346,6 +348,7 @@ new_ode_model <- function (model = NULL, state_init <- add_quotes(state_init) } if(is.null(nonmem) || length(nonmem) == 0) { nonmem <- "NULL" } + if(is.null(ferx) || length(ferx) == 0) { ferx <- "NULL" } if(is.null(int_step_size)) { int_step_size <- "NULL" } pars <- vector_to_R_code(reqd) covs <- vector_to_R_code(cov_names) @@ -380,7 +383,8 @@ new_ode_model <- function (model = NULL, "\\[CMT_MAPPING\\]", paste0(deparse(cmt_mapping), collapse = ""), "\\[INT_STEP_SIZE\\]", as.character(int_step_size), "\\[COMMENTS\\]", paste0("\n", as.character(paste0(paste0(" - ", comments), collapse = "\n"))), - "\\[NONMEM\\]", as.character(nonmem) + "\\[NONMEM\\]", as.character(nonmem), + "\\[FERX\\]", as.character(ferx) ), ncol=2, byrow=TRUE) if(verbose) { print(repl) diff --git a/inst/template/R/model.R b/inst/template/R/model.R index 7220675c..2c93e9f1 100644 --- a/inst/template/R/model.R +++ b/inst/template/R/model.R @@ -28,6 +28,7 @@ model <- function(mod = NULL) { attr(ret, "development") <- list([DEVELOPMENT]) attr(ret, "cmt_mapping") <- [CMT_MAPPING] attr(ret, "nonmem") <- "[NONMEM]" + attr(ret, "ferx") <- "[FERX]" attr(ret, "comments") <- "[COMMENTS]" attr(ret, "version") <- "[VERSION]" class(ret) <- c("PKPDsim", class(ret)) From b5603fdf4b7dce4c3da0d88e7d5d7f7e44ad76aa Mon Sep 17 00:00:00 2001 From: Ron Keizer Date: Fri, 23 Oct 2026 13:57:25 -0700 Subject: [PATCH 2/4] Add tests for FeRx implementation support Co-Authored-By: Claude Opus 5.5 --- tests/testthat/test_ferx.R | 160 +++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 tests/testthat/test_ferx.R diff --git a/tests/testthat/test_ferx.R b/tests/testthat/test_ferx.R new file mode 100644 index 00000000..867d5027 --- /dev/null +++ b/tests/testthat/test_ferx.R @@ -0,0 +1,160 @@ +# Helper: generate (but do not install) a model package, and return the path +# to the generated R/model.R file. +generate_model_package <- function(package, ferx = NULL, nonmem = NULL) { + folder <- tempfile("ferx_pkg") + dir.create(folder) + # install = FALSE runs `R CMD build`, which writes a tarball to the working + # directory, so build from within the temp folder. + old_wd <- setwd(folder) + on.exit(setwd(old_wd), add = TRUE) + suppressMessages( + new_ode_model( + code = "dAdt[0] = -(CL/V)*A[0];", + obs = list(cmt = 1, scale = "V"), + dose = list(cmt = 1, bioav = 1), + parameters = list(CL = 5, V = 50), + as_is = TRUE, + package = package, + folder = folder, + install = FALSE, + nonmem = nonmem, + ferx = ferx, + quiet = FALSE + ) + ) + file.path(folder, package, "R", "model.R") +} + +# Helper: evaluate the generated model.R and return the model attributes, +# without needing to compile/install the package. `model()` looks up the +# compiled simulation function in the attached package, so stub that lookup. +get_generated_attributes <- function(model_file) { + env <- new.env() + env$as.environment <- function(...) emptyenv() + env$get <- function(...) function() NULL + eval(parse(model_file), envir = env) + attributes(env$model()) +} + +test_that("ferx code is stored as model attribute in generated package", { + skip_on_cran() + ferx_code <- "model 1cmt_iv\n CL = THETA(1)\n V = THETA(2)\nend" + model_file <- generate_model_package("ferxtest1", ferx = ferx_code) + expect_true(file.exists(model_file)) + model_txt <- readLines(model_file) + expect_false(any(grepl("[FERX]", model_txt, fixed = TRUE))) + attrs <- get_generated_attributes(model_file) + expect_equal(attrs$ferx, ferx_code) +}) + +test_that("ferx attribute defaults to 'NULL' when not supplied", { + skip_on_cran() + model_file <- generate_model_package("ferxtest2") + attrs <- get_generated_attributes(model_file) + expect_equal(attrs$ferx, "NULL") + expect_equal(attrs$nonmem, "NULL") +}) + +test_that("ferx attribute defaults to 'NULL' when empty vector supplied", { + skip_on_cran() + model_file <- generate_model_package("ferxtest3", ferx = character(0)) + attrs <- get_generated_attributes(model_file) + expect_equal(attrs$ferx, "NULL") +}) + +test_that("ferx and nonmem attributes are independent", { + skip_on_cran() + model_file <- generate_model_package( + "ferxtest4", + nonmem = "$PROB nonmem code", + ferx = "ferx code" + ) + attrs <- get_generated_attributes(model_file) + expect_equal(attrs$nonmem, "$PROB nonmem code") + expect_equal(attrs$ferx, "ferx code") +}) + +test_that("model_from_api reads ferx file when implementation is defined", { + def_file <- tempfile(fileext = ".json5") + ferx_file <- tempfile(fileext = ".ferx") + writeLines(c("line 1", "line 2"), ferx_file) + def <- read_model_json(test_path("sample_json", "test_1cmt_iv.json5")) + def$implementations <- list(ferx = TRUE) + writeLines(jsonlite::toJSON(def, auto_unbox = TRUE), def_file) + + captured <- NULL + local_mocked_bindings( + new_ode_model = function(...) { + captured <<- list(...) + "model" + } + ) + model_from_api( + url = def_file, + model = "test_1cmt_iv", + ferx = ferx_file, + verbose = FALSE, + force = TRUE + ) + expect_equal(captured$ferx, "line 1\nline 2") + expect_null(captured$nonmem) +}) + +test_that("model_from_api passes NULL ferx when no ferx file supplied", { + def_file <- tempfile(fileext = ".json5") + def <- read_model_json(test_path("sample_json", "test_1cmt_iv.json5")) + def$implementations <- list(ferx = TRUE) + writeLines(jsonlite::toJSON(def, auto_unbox = TRUE), def_file) + + captured <- NULL + local_mocked_bindings( + new_ode_model = function(...) { + captured <<- list(...) + "model" + } + ) + model_from_api( + url = def_file, + model = "test_1cmt_iv", + verbose = FALSE, + force = TRUE + ) + expect_true("ferx" %in% names(captured)) + expect_null(captured$ferx) +}) + +test_that("ferx attribute available on installed model package", { + skip_on_cran() + instloc <- tempfile("package") + srcloc <- file.path(instloc, "src") + dir.create(instloc) + dir.create(srcloc) + on.exit(unlink(instloc, recursive = TRUE), add = TRUE) + ferx_code <- "ferx model code" + suppressWarnings( + suppressMessages( + new_ode_model( + code = "dAdt[0] = -(CL/V)*A[0];", + obs = list(cmt = 1, scale = "V"), + dose = list(cmt = 1, bioav = 1), + parameters = list(CL = 5, V = 50), + as_is = TRUE, + package = "ferxinstalltest", + folder = srcloc, + lib_location = instloc, + ferx = ferx_code, + quiet = NULL + ) + ) + ) + expect_true("ferxinstalltest" %in% installed.packages(lib.loc = instloc)) + suppressMessages(require(ferxinstalltest, lib.loc = instloc)) + on.exit( + detach("package:ferxinstalltest", unload = TRUE), + add = TRUE, + after = FALSE + ) + mod <- ferxinstalltest::model() + expect_equal(attr(mod, "ferx"), ferx_code) + expect_equal(attr(mod, "nonmem"), "NULL") +}) From 04d24cdaf511bd49eef8ada109516cdf7b129255 Mon Sep 17 00:00:00 2001 From: Ron Keizer Date: Fri, 23 Oct 2026 14:00:55 -0700 Subject: [PATCH 3/4] Address review feedback on FeRx support - Move ferx argument to end of model_from_api() and new_ode_model() signatures so existing positional calls keep their bindings - Set ferx attribute on directly compiled (non-package) models - Serialize ferx code as an escaped R string literal before inserting into the package template, so quotes/backslashes/newlines round-trip - Add tests for the above Co-Authored-By: Claude Opus 5.5 --- R/misc.R | 14 ++++++++++++++ R/model_from_api.R | 4 ++-- R/new_ode_model.R | 12 ++++++++---- inst/template/R/model.R | 2 +- man/model_from_api.Rd | 3 +++ man/new_ode_model.Rd | 5 ++++- tests/testthat/test_ferx.R | 39 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 71 insertions(+), 8 deletions(-) diff --git a/R/misc.R b/R/misc.R index 540defe4..933a1d75 100644 --- a/R/misc.R +++ b/R/misc.R @@ -55,6 +55,20 @@ search_replace_in_file <- function(files = c(), find = NULL, replacement = NULL) } +#' Convert a character vector into an R string literal +#' +#' Collapses `x` using newlines and deparses it, so that quotes and newlines +#' are escaped. Backslashes are escaped once more so the result can be used +#' as a `replacement` in [search_replace_in_file()] (which uses `gsub()`). +#' +#' @param x character vector +#' @return a single string containing R code for a string literal +#' @noRd +string_to_R_literal <- function(x) { + x <- paste0(deparse(paste(x, collapse = "\n")), collapse = "") + gsub("\\\\", "\\\\\\\\", x) +} + #' Put vector values in quotes #' #' @param x vector of string / numeric diff --git a/R/model_from_api.R b/R/model_from_api.R index d814d012..553cb1fc 100644 --- a/R/model_from_api.R +++ b/R/model_from_api.R @@ -3,12 +3,12 @@ #' @param url URL or file path to JSON representation of model #' @param model model id (used in messages) #' @param nonmem URL or file path to NONMEM file -#' @param ferx URL or file path to FeRx file #' @param verbose verbosity (T/F) #' @param get_definition return only the model definition, do not compile #' @param to_package compile to package? #' @param force force install even if same version number of model already installed. #' @param install_all force install all, even if model inactive +#' @param ferx URL or file path to FeRx file #' @param ... arguments passed to `new_ode_model()` function #' @export #' @return Model object created with [PKPDsim::new_ode_model()] @@ -16,12 +16,12 @@ model_from_api <- function(url, model = NULL, nonmem = NULL, - ferx = NULL, verbose = TRUE, get_definition = FALSE, to_package = FALSE, force = FALSE, install_all = FALSE, + ferx = NULL, ...) { def <- read_model_json(url) if(verbose) { diff --git a/R/new_ode_model.R b/R/new_ode_model.R index a6506d83..8634f572 100644 --- a/R/new_ode_model.R +++ b/R/new_ode_model.R @@ -37,7 +37,6 @@ #' @param verbose show more output #' @param as_is use C-code as-is, don't substitute line-endings or shift indices #' @param nonmem add NONMEM code as attribute to model object -#' @param ferx add FeRx code as attribute to model object #' @param comments comments for model #' @param version number of library #' @param quiet passed on to `system2` as setting for stderr and stdout; how to @@ -46,6 +45,7 @@ #' @param definition optional, filename for the JSON file the full definition #' for the model. The definition file will be stored as `definition.json` in the #' resulting package. +#' @param ferx add FeRx code as attribute to model object #' @export #' @return If package name is NULL, returns the model object. Otherwise has no #' return value. @@ -86,11 +86,11 @@ new_ode_model <- function (model = NULL, verbose = FALSE, as_is = FALSE, nonmem = NULL, - ferx = NULL, comments = NULL, version = "0.1.0", quiet = "", - definition = NULL + definition = NULL, + ferx = NULL ) { if (is.null(model) & is.null(code) & is.null(file) & is.null(func)) { stop(paste0("Either a model name (from the PKPDsim library), ODE code, an R function, or a file containing code for the ODE system have to be supplied to this function. The following models are available:\n ", model_library())) @@ -289,6 +289,9 @@ new_ode_model <- function (model = NULL, if(!is.null(int_step_size)) { attr(sim_out, "int_step_size") <- int_step_size } + if(!is.null(ferx)) { + attr(sim_out, "ferx") <- paste(ferx, collapse = "\n") + } class(sim_out) <- c("PKPDsim", class(sim_out)) return(sim_out) } @@ -349,6 +352,7 @@ new_ode_model <- function (model = NULL, } if(is.null(nonmem) || length(nonmem) == 0) { nonmem <- "NULL" } if(is.null(ferx) || length(ferx) == 0) { ferx <- "NULL" } + ferx <- string_to_R_literal(ferx) if(is.null(int_step_size)) { int_step_size <- "NULL" } pars <- vector_to_R_code(reqd) covs <- vector_to_R_code(cov_names) @@ -384,7 +388,7 @@ new_ode_model <- function (model = NULL, "\\[INT_STEP_SIZE\\]", as.character(int_step_size), "\\[COMMENTS\\]", paste0("\n", as.character(paste0(paste0(" - ", comments), collapse = "\n"))), "\\[NONMEM\\]", as.character(nonmem), - "\\[FERX\\]", as.character(ferx) + "\\[FERX\\]", ferx ), ncol=2, byrow=TRUE) if(verbose) { print(repl) diff --git a/inst/template/R/model.R b/inst/template/R/model.R index 2c93e9f1..8aaebe20 100644 --- a/inst/template/R/model.R +++ b/inst/template/R/model.R @@ -28,7 +28,7 @@ model <- function(mod = NULL) { attr(ret, "development") <- list([DEVELOPMENT]) attr(ret, "cmt_mapping") <- [CMT_MAPPING] attr(ret, "nonmem") <- "[NONMEM]" - attr(ret, "ferx") <- "[FERX]" + attr(ret, "ferx") <- [FERX] attr(ret, "comments") <- "[COMMENTS]" attr(ret, "version") <- "[VERSION]" class(ret) <- c("PKPDsim", class(ret)) diff --git a/man/model_from_api.Rd b/man/model_from_api.Rd index aad78a8d..ed6ffda5 100644 --- a/man/model_from_api.Rd +++ b/man/model_from_api.Rd @@ -13,6 +13,7 @@ model_from_api( to_package = FALSE, force = FALSE, install_all = FALSE, + ferx = NULL, ... ) } @@ -33,6 +34,8 @@ model_from_api( \item{install_all}{force install all, even if model inactive} +\item{ferx}{URL or file path to FeRx file} + \item{...}{arguments passed to \code{new_ode_model()} function} } \value{ diff --git a/man/new_ode_model.Rd b/man/new_ode_model.Rd index f40ff6d7..37c263b5 100644 --- a/man/new_ode_model.Rd +++ b/man/new_ode_model.Rd @@ -45,7 +45,8 @@ new_ode_model( comments = NULL, version = "0.1.0", quiet = "", - definition = NULL + definition = NULL, + ferx = NULL ) } \arguments{ @@ -134,6 +135,8 @@ TRUE captures the output and saves as a file.} \item{definition}{optional, filename for the JSON file the full definition for the model. The definition file will be stored as \code{definition.json} in the resulting package.} + +\item{ferx}{add FeRx code as attribute to model object} } \value{ If package name is NULL, returns the model object. Otherwise has no diff --git a/tests/testthat/test_ferx.R b/tests/testthat/test_ferx.R index 867d5027..7ad8715f 100644 --- a/tests/testthat/test_ferx.R +++ b/tests/testthat/test_ferx.R @@ -158,3 +158,42 @@ test_that("ferx attribute available on installed model package", { expect_equal(attr(mod, "ferx"), ferx_code) expect_equal(attr(mod, "nonmem"), "NULL") }) + +test_that("ferx code with quotes, backslashes and newlines round-trips", { + skip_on_cran() + ferx_code <- paste( + 'label = "central"', + "path = 'C:\\models\\1cmt'", + "regex = \\1 and \\\\", + sep = "\n" + ) + model_file <- generate_model_package("ferxtest5", ferx = ferx_code) + expect_silent(parse(model_file)) + attrs <- get_generated_attributes(model_file) + expect_equal(attrs$ferx, ferx_code) +}) + +test_that("ferx code is stored as attribute on directly compiled models", { + skip_on_cran() + ferx_code <- 'model "1cmt"\nend' + mod <- new_ode_model( + code = "dAdt[0] = -(CL/V)*A[0];", + obs = list(cmt = 1, scale = "V"), + parameters = list(CL = 5, V = 50), + ferx = ferx_code + ) + expect_equal(attr(mod, "ferx"), ferx_code) + expect_null(attr(mod_1cmt_iv, "ferx")) +}) + +test_that("ferx argument does not shift existing positional arguments", { + args_api <- names(formals(model_from_api)) + expect_equal( + args_api[1:8], + c("url", "model", "nonmem", "verbose", "get_definition", + "to_package", "force", "install_all") + ) + args_ode <- names(formals(new_ode_model)) + expect_equal(args_ode[length(args_ode)], "ferx") + expect_equal(args_ode[length(args_ode) - 1], "definition") +}) From c793860cf68a95d9de721d27efb00a900d74afce Mon Sep 17 00:00:00 2001 From: Ron Keizer Date: Fri, 23 Oct 2026 14:03:42 -0700 Subject: [PATCH 4/4] Apply FeRx escaping and attribute fixes to NONMEM code - Serialize nonmem code as an escaped R string literal in the package template, so quotes/backslashes/newlines round-trip - Set nonmem attribute on directly compiled (non-package) models Co-Authored-By: Claude Opus 5.5 --- R/new_ode_model.R | 6 +++++- inst/template/R/model.R | 2 +- tests/testthat/test_ferx.R | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/R/new_ode_model.R b/R/new_ode_model.R index 8634f572..b26349ee 100644 --- a/R/new_ode_model.R +++ b/R/new_ode_model.R @@ -289,6 +289,9 @@ new_ode_model <- function (model = NULL, if(!is.null(int_step_size)) { attr(sim_out, "int_step_size") <- int_step_size } + if(!is.null(nonmem)) { + attr(sim_out, "nonmem") <- paste(nonmem, collapse = "\n") + } if(!is.null(ferx)) { attr(sim_out, "ferx") <- paste(ferx, collapse = "\n") } @@ -351,6 +354,7 @@ new_ode_model <- function (model = NULL, state_init <- add_quotes(state_init) } if(is.null(nonmem) || length(nonmem) == 0) { nonmem <- "NULL" } + nonmem <- string_to_R_literal(nonmem) if(is.null(ferx) || length(ferx) == 0) { ferx <- "NULL" } ferx <- string_to_R_literal(ferx) if(is.null(int_step_size)) { int_step_size <- "NULL" } @@ -387,7 +391,7 @@ new_ode_model <- function (model = NULL, "\\[CMT_MAPPING\\]", paste0(deparse(cmt_mapping), collapse = ""), "\\[INT_STEP_SIZE\\]", as.character(int_step_size), "\\[COMMENTS\\]", paste0("\n", as.character(paste0(paste0(" - ", comments), collapse = "\n"))), - "\\[NONMEM\\]", as.character(nonmem), + "\\[NONMEM\\]", nonmem, "\\[FERX\\]", ferx ), ncol=2, byrow=TRUE) if(verbose) { diff --git a/inst/template/R/model.R b/inst/template/R/model.R index 8aaebe20..964be3eb 100644 --- a/inst/template/R/model.R +++ b/inst/template/R/model.R @@ -27,7 +27,7 @@ model <- function(mod = NULL) { attr(ret, "iov") <- list([IOV]) attr(ret, "development") <- list([DEVELOPMENT]) attr(ret, "cmt_mapping") <- [CMT_MAPPING] - attr(ret, "nonmem") <- "[NONMEM]" + attr(ret, "nonmem") <- [NONMEM] attr(ret, "ferx") <- [FERX] attr(ret, "comments") <- "[COMMENTS]" attr(ret, "version") <- "[VERSION]" diff --git a/tests/testthat/test_ferx.R b/tests/testthat/test_ferx.R index 7ad8715f..7021765a 100644 --- a/tests/testthat/test_ferx.R +++ b/tests/testthat/test_ferx.R @@ -197,3 +197,30 @@ test_that("ferx argument does not shift existing positional arguments", { expect_equal(args_ode[length(args_ode)], "ferx") expect_equal(args_ode[length(args_ode) - 1], "definition") }) + +test_that("nonmem code with quotes, backslashes and newlines round-trips", { + skip_on_cran() + nonmem_code <- paste( + "$PROB 'one cmt'", + '$INPUT ID TIME DV ; "quoted"', + "$DATA C:\\data\\nm.csv \\1", + sep = "\n" + ) + model_file <- generate_model_package("ferxtest6", nonmem = nonmem_code) + expect_silent(parse(model_file)) + attrs <- get_generated_attributes(model_file) + expect_equal(attrs$nonmem, nonmem_code) +}) + +test_that("nonmem code is stored as attribute on directly compiled models", { + skip_on_cran() + nonmem_code <- '$PROB "1cmt"\n$PK' + mod <- new_ode_model( + code = "dAdt[0] = -(CL/V)*A[0];", + obs = list(cmt = 1, scale = "V"), + parameters = list(CL = 5, V = 50), + nonmem = nonmem_code + ) + expect_equal(attr(mod, "nonmem"), nonmem_code) + expect_null(attr(mod_1cmt_iv, "nonmem")) +})