From 0ffe33f7b8a29135c67b02644ddd2eff051da731 Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Thu, 5 Dec 2024 15:12:26 -0600 Subject: [PATCH 01/11] Add expit and logit ui named interface --- NAMESPACE | 2 ++ R/utils.R | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index acec56761..6568d6c2a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -159,6 +159,7 @@ S3method(rxSolve,rxode2tos) S3method(rxTrans,character) S3method(rxTrans,default) S3method(rxUdfUi,default) +S3method(rxUdfUi,expit) S3method(rxUdfUi,linMod) S3method(rxUdfUi,linMod0) S3method(rxUdfUi,linModA) @@ -169,6 +170,7 @@ S3method(rxUdfUi,linModD) S3method(rxUdfUi,linModD0) S3method(rxUdfUi,linModM) S3method(rxUdfUi,linModM0) +S3method(rxUdfUi,logit) S3method(rxUdfUi,ribeta) S3method(rxUdfUi,ribinom) S3method(rxUdfUi,ricauchy) diff --git a/R/utils.R b/R/utils.R index 4e8090347..9af8e7d20 100644 --- a/R/utils.R +++ b/R/utils.R @@ -507,6 +507,67 @@ expit <- function(alpha, low = 0, high = 1) { .rxTransform(alpha, 1.0, low, high, 4L, TRUE) } +.logit <- function(x, low = 0, high=1) { + .x <- as.character(substitute(x)) + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + if (!inherits(.tmp, "try-error")) { + if (is.character(.tmp)) { + .x <- x + } + } + .low <- as.character(substitute(low)) + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + if (!inherits(.tmp, "try-error")) { + if (is.character(.tmp)) { + .low <- low + } + } + .high <- as.character(substitute(high)) + .tmp <- suppressWarnings(try(force(high), silent = TRUE)) + if (!inherits(.tmp, "try-error")) { + if (is.character(.tmp)) { + .high <- high + } + } + list(replace = paste0("logit(", .x, ", ", .low, ", ", .high, ")")) +} + +.expit <- function(x, low = 0, high=1) { + .x <- as.character(substitute(x)) + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + if (!inherits(.tmp, "try-error")) { + if (is.character(.tmp)) { + .x <- x + } + } + .low <- as.character(substitute(low)) + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + if (!inherits(.tmp, "try-error")) { + if (is.character(.tmp)) { + .low <- low + } + } + .high <- as.character(substitute(high)) + .tmp <- suppressWarnings(try(force(high), silent = TRUE)) + if (!inherits(.tmp, "try-error")) { + if (is.character(.tmp)) { + .high <- high + } + } + list(replace = paste0("expit(", .x, ", ", .low, ", ", .high, ")")) +} + +#' @export +rxUdfUi.logit <- function(fun) { + .fun <- fun + .fun[[1]] <- str2lang(paste0(".", deparse1(fun[[1]]))) + eval(.fun) +} + +#' @export +rxUdfUi.expit <- rxUdfUi.logit + + #' @rdname logit #' @export logitNormInfo <- function(mean = 0, sd = 1, low = 0, high = 1, abs.tol = 1e-6, ...) { From f95844076924d0ce9e7969f36b980c5552c82f76 Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Thu, 5 Dec 2024 15:23:23 -0600 Subject: [PATCH 02/11] Add some more tests and change numeric and character values --- R/rxrandomui.R | 96 ++++++++++++++++++------------------- R/utils.R | 10 ++-- tests/testthat/test-logit.R | 33 +++++++++++++ 3 files changed, 86 insertions(+), 53 deletions(-) diff --git a/R/rxrandomui.R b/R/rxrandomui.R index 5c1b5da80..81b8aef66 100644 --- a/R/rxrandomui.R +++ b/R/rxrandomui.R @@ -4,14 +4,14 @@ .mean <- as.character(substitute(mean)) .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .mean <- mean } } .sd <- as.character(substitute(sd)) .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .sd <- sd } } @@ -28,7 +28,7 @@ rxUdfUi.rxnorm <- rxUdfUi.rxpois .lambda <- as.character(substitute(lambda)) .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .lambda <- lambda } } @@ -45,7 +45,7 @@ rxUdfUi.rxpois <- rxUdfUi.rxpois .df <- as.character(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .df <- df } } @@ -62,14 +62,14 @@ rxUdfUi.rxt <- rxUdfUi.rxpois .min <- as.character(substitute(min)) .tmp <- suppressWarnings(try(force(min), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .min <- min } } .max <- as.character(substitute(max)) .tmp <- suppressWarnings(try(force(max), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .max <- max } } @@ -86,18 +86,18 @@ rxUdfUi.rxunif <- rxUdfUi.rxpois .shape <- as.character(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .shape <- shape } } .scale <- as.character(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .scale <- scale } } - list(replace = paste0("rxweibull(", .shape, ", ", .scale, + list(replace = paste0("rxweibull(", .shape, ", ", .scale, ")")) } @@ -111,7 +111,7 @@ rxUdfUi.rxweibull <- rxUdfUi.rxpois .prob <- as.character(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .prob <- prob } } @@ -128,18 +128,18 @@ rxUdfUi.rxgeom <- rxUdfUi.rxpois .shape1 <- as.character(substitute(shape1)) .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .shape1 <- shape1 } } .shape2 <- as.character(substitute(shape2)) .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .shape2 <- shape2 } } - list(replace = paste0("rxbeta(", .shape1, ", ", .shape2, + list(replace = paste0("rxbeta(", .shape1, ", ", .shape2, ")")) } @@ -153,14 +153,14 @@ rxUdfUi.rxbeta <- rxUdfUi.rxpois .shape <- as.character(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .shape <- shape } } .rate <- as.character(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .rate <- rate } } @@ -177,14 +177,14 @@ rxUdfUi.rxgamma <- rxUdfUi.rxpois .df1 <- as.character(substitute(df1)) .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .df1 <- df1 } } .df2 <- as.character(substitute(df2)) .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .df2 <- df2 } } @@ -201,7 +201,7 @@ rxUdfUi.rxf <- rxUdfUi.rxpois .rate <- as.character(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .rate <- rate } } @@ -218,7 +218,7 @@ rxUdfUi.rxexp <- rxUdfUi.rxpois .df <- as.character(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .df <- df } } @@ -235,18 +235,18 @@ rxUdfUi.rxchisq <- rxUdfUi.rxpois .location <- as.character(substitute(location)) .tmp <- suppressWarnings(try(force(location), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .location <- location } } .scale <- as.character(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .scale <- scale } } - list(replace = paste0("rxcauchy(", .location, ", ", .scale, + list(replace = paste0("rxcauchy(", .location, ", ", .scale, ")")) } @@ -260,14 +260,14 @@ rxUdfUi.rxcauchy <- rxUdfUi.rxpois .size <- as.character(substitute(size)) .tmp <- suppressWarnings(try(force(size), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .size <- size } } .prob <- as.character(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .prob <- prob } } @@ -284,14 +284,14 @@ rxUdfUi.rxbinom <- rxUdfUi.rxpois .mean <- as.character(substitute(mean)) .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .mean <- mean } } .sd <- as.character(substitute(sd)) .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .sd <- sd } } @@ -308,7 +308,7 @@ rxUdfUi.rinorm <- rxUdfUi.rxpois .lambda <- as.character(substitute(lambda)) .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .lambda <- lambda } } @@ -325,7 +325,7 @@ rxUdfUi.ripois <- rxUdfUi.rxpois .df <- as.character(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .df <- df } } @@ -342,14 +342,14 @@ rxUdfUi.rit <- rxUdfUi.rxpois .min <- as.character(substitute(min)) .tmp <- suppressWarnings(try(force(min), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .min <- min } } .max <- as.character(substitute(max)) .tmp <- suppressWarnings(try(force(max), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .max <- max } } @@ -366,18 +366,18 @@ rxUdfUi.riunif <- rxUdfUi.rxpois .shape <- as.character(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .shape <- shape } } .scale <- as.character(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .scale <- scale } } - list(replace = paste0("riweibull(", .shape, ", ", .scale, + list(replace = paste0("riweibull(", .shape, ", ", .scale, ")")) } @@ -391,7 +391,7 @@ rxUdfUi.riweibull <- rxUdfUi.rxpois .prob <- as.character(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .prob <- prob } } @@ -408,18 +408,18 @@ rxUdfUi.rigeom <- rxUdfUi.rxpois .shape1 <- as.character(substitute(shape1)) .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .shape1 <- shape1 } } .shape2 <- as.character(substitute(shape2)) .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .shape2 <- shape2 } } - list(replace = paste0("ribeta(", .shape1, ", ", .shape2, + list(replace = paste0("ribeta(", .shape1, ", ", .shape2, ")")) } @@ -433,14 +433,14 @@ rxUdfUi.ribeta <- rxUdfUi.rxpois .shape <- as.character(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .shape <- shape } } .rate <- as.character(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .rate <- rate } } @@ -457,14 +457,14 @@ rxUdfUi.rigamma <- rxUdfUi.rxpois .df1 <- as.character(substitute(df1)) .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .df1 <- df1 } } .df2 <- as.character(substitute(df2)) .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .df2 <- df2 } } @@ -481,7 +481,7 @@ rxUdfUi.rif <- rxUdfUi.rxpois .rate <- as.character(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .rate <- rate } } @@ -498,7 +498,7 @@ rxUdfUi.riexp <- rxUdfUi.rxpois .df <- as.character(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .df <- df } } @@ -515,18 +515,18 @@ rxUdfUi.richisq <- rxUdfUi.rxpois .location <- as.character(substitute(location)) .tmp <- suppressWarnings(try(force(location), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .location <- location } } .scale <- as.character(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .scale <- scale } } - list(replace = paste0("ricauchy(", .location, ", ", .scale, + list(replace = paste0("ricauchy(", .location, ", ", .scale, ")")) } @@ -540,14 +540,14 @@ rxUdfUi.ricauchy <- rxUdfUi.rxpois .size <- as.character(substitute(size)) .tmp <- suppressWarnings(try(force(size), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .size <- size } } .prob <- as.character(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .prob <- prob } } diff --git a/R/utils.R b/R/utils.R index 9af8e7d20..9ba26898c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -511,14 +511,14 @@ expit <- function(alpha, low = 0, high = 1) { .x <- as.character(substitute(x)) .tmp <- suppressWarnings(try(force(x), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .x <- x } } .low <- as.character(substitute(low)) .tmp <- suppressWarnings(try(force(low), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .low <- low } } @@ -535,7 +535,7 @@ expit <- function(alpha, low = 0, high = 1) { .expit <- function(x, low = 0, high=1) { .x <- as.character(substitute(x)) .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { + if (!inherits(.tmp, "try-error") || is.numeric(.tmp)) { if (is.character(.tmp)) { .x <- x } @@ -543,14 +543,14 @@ expit <- function(alpha, low = 0, high = 1) { .low <- as.character(substitute(low)) .tmp <- suppressWarnings(try(force(low), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .low <- low } } .high <- as.character(substitute(high)) .tmp <- suppressWarnings(try(force(high), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .high <- high } } diff --git a/tests/testthat/test-logit.R b/tests/testthat/test-logit.R index 0b3573985..870c74723 100644 --- a/tests/testthat/test-logit.R +++ b/tests/testthat/test-logit.R @@ -1,4 +1,37 @@ rxTest({ + + test_test("logit ui test", { + + f <- function() { + model({ + a <- logit(x, high=10) + }) + } + + expect_equal((f() %>% modelExtract), + "a <- logit(x, 0, 10)") + + f <- function() { + model({ + a <- expit(x, high=10+1) + }) + } + + expect_equal((f() %>% modelExtract), + "a <- expit(x, 0, 11)") + + f <- function() { + model({ + a <- expit(high=10+1, low=a, x) + }) + } + + expect_equal((f() %>% modelExtract), + "a <- expit(x, a, 11)") + + + }) + test_that("logit tests", { expect_equal( rxToSE("logit(a)"), From 93685e7efc0159b7ed4585556fe888e7673a850f Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Thu, 5 Dec 2024 15:30:14 -0600 Subject: [PATCH 03/11] Update news --- NEWS.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS.md b/NEWS.md index dc9722cc0..1252515e4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,11 @@ # rxode2 (development version) +- Add `logit`/`expit` named expressions, that is `logit(x, high=20)` + becomes `logit(x, 0, 20)` in ui models. + +- Updated random ui models like `rxnorm(sd=10)` to accept complex + numeric expressions like `rxnorm(sd=10+1)`. + - Rework the `tad()` and related functions so they use the same interface as compartments (this way they do not depend on the order of compartments); See #815. For mu-referencing, Also allow dummy From ab483b6c80791257e559a62f3093fc195b1dfe3c Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Fri, 13 Dec 2024 12:35:02 -0600 Subject: [PATCH 04/11] Move names around --- R/utils.R | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/R/utils.R b/R/utils.R index 9ba26898c..24a2001bd 100644 --- a/R/utils.R +++ b/R/utils.R @@ -507,6 +507,7 @@ expit <- function(alpha, low = 0, high = 1) { .rxTransform(alpha, 1.0, low, high, 4L, TRUE) } + .logit <- function(x, low = 0, high=1) { .x <- as.character(substitute(x)) .tmp <- suppressWarnings(try(force(x), silent = TRUE)) @@ -525,7 +526,7 @@ expit <- function(alpha, low = 0, high = 1) { .high <- as.character(substitute(high)) .tmp <- suppressWarnings(try(force(high), silent = TRUE)) if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { + if (is.character(.tmp) || is.numeric(.tmp)) { .high <- high } } @@ -535,8 +536,8 @@ expit <- function(alpha, low = 0, high = 1) { .expit <- function(x, low = 0, high=1) { .x <- as.character(substitute(x)) .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - if (!inherits(.tmp, "try-error") || is.numeric(.tmp)) { - if (is.character(.tmp)) { + if (!inherits(.tmp, "try-error")) { + if (is.character(.tmp) || is.numeric(.tmp)) { .x <- x } } From 369886dfdb7fde5e1aa310cd72058874e8417a8c Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Fri, 13 Dec 2024 12:38:03 -0600 Subject: [PATCH 05/11] Fix test-logit --- tests/testthat/test-logit.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-logit.R b/tests/testthat/test-logit.R index 870c74723..9e6277338 100644 --- a/tests/testthat/test-logit.R +++ b/tests/testthat/test-logit.R @@ -1,6 +1,6 @@ rxTest({ - test_test("logit ui test", { + test_that("logit ui test", { f <- function() { model({ From aec078afc447948fe2887f231808008f175ecec2 Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Fri, 13 Dec 2024 12:46:32 -0600 Subject: [PATCH 06/11] Add .uiArg --- R/utils.R | 52 ++++++++++++++++++++++------------------------------ 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/R/utils.R b/R/utils.R index 24a2001bd..297bdbeec 100644 --- a/R/utils.R +++ b/R/utils.R @@ -508,53 +508,45 @@ expit <- function(alpha, low = 0, high = 1) { } +.uiArg <- function(char, f) { + if (!inherits(f, "try-error")) { + if (is.numeric(f)) { + return(as.character(f)) + } + if (is.character(f)) { + return(f) + } + } + char +} + .logit <- function(x, low = 0, high=1) { .x <- as.character(substitute(x)) .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .x <- x - } - } + .x <- .uiArg(.x, .tmp) + .low <- as.character(substitute(low)) .tmp <- suppressWarnings(try(force(low), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .low <- low - } - } + .low <- .uiArg(.low, .tmp) + .high <- as.character(substitute(high)) .tmp <- suppressWarnings(try(force(high), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .high <- high - } - } + .high <- .uiArg(.high, .tmp) list(replace = paste0("logit(", .x, ", ", .low, ", ", .high, ")")) } .expit <- function(x, low = 0, high=1) { .x <- as.character(substitute(x)) .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .x <- x - } - } + .x <- .uiArg(.x, .tmp) + .low <- as.character(substitute(low)) .tmp <- suppressWarnings(try(force(low), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .low <- low - } - } + .low <- .uiArg(.low, .tmp) + .high <- as.character(substitute(high)) .tmp <- suppressWarnings(try(force(high), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .high <- high - } - } + .high <- .uiArg(.high, .tmp) list(replace = paste0("expit(", .x, ", ", .low, ", ", .high, ")")) } From 20ea8d173b5a23b10ed1aa98e80472b12adfbeaf Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Fri, 13 Dec 2024 13:22:02 -0600 Subject: [PATCH 07/11] Add logit/expit to named functions in random --- NAMESPACE | 1 + R/build.R | 10 +- R/rxrandomui.R | 344 ++++++++++++++++++------------------------------- R/utils.R | 56 ++------ 4 files changed, 147 insertions(+), 264 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 6568d6c2a..934de1e63 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -343,6 +343,7 @@ export(.udfEnvSet) export(.udfEnvSetUdf) export(.udfExists) export(.udfMd5Info) +export(.uiArg) export(.useUtf) export(.vecDf) export(ELU) diff --git a/R/build.R b/R/build.R index 8ca93c905..2018cdb2c 100644 --- a/R/build.R +++ b/R/build.R @@ -69,11 +69,7 @@ d/dt(blood) = a*intestine - b*blood .ret <- bquote({ .(.dotArg) <- as.character(substitute(.(.arg))) .tmp <- suppressWarnings(try(force(.(.arg)), silent=TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .(.dotArg) <- .(.arg) - } - } + .(.dotArg) <- .uiArg(.(.dotArg), .tmp) }) lapply(seq_along(.ret)[-1], function(i) { .ret[[i]] @@ -108,7 +104,9 @@ d/dt(blood) = a*intestine - b*blood "rxexp"="rate", "rxchisq"="df", "rxcauchy"=c(location = 0, scale = 1), - "rxbinom"=c("size", "prob")) + "rxbinom"=c("size", "prob"), + "logit"=c("x"=NA, "low"=0, "hi"=1), + "expit"=c("x"=NA, "low"=0, "hi"=1)) .lst2 <- .lst names(.lst2) <- gsub("rx", "ri", names(.lst2)) .lst <- c(.lst, .lst2) diff --git a/R/rxrandomui.R b/R/rxrandomui.R index 81b8aef66..45cc95fdc 100644 --- a/R/rxrandomui.R +++ b/R/rxrandomui.R @@ -3,18 +3,10 @@ .rxnorm <- function(mean = 0, sd = 1) { .mean <- as.character(substitute(mean)) .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .mean <- mean - } - } + .mean <- .uiArg(.mean, .tmp) .sd <- as.character(substitute(sd)) .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .sd <- sd - } - } + .sd <- .uiArg(.sd, .tmp) list(replace = paste0("rxnorm(", .mean, ", ", .sd, ")")) } @@ -27,11 +19,7 @@ rxUdfUi.rxnorm <- rxUdfUi.rxpois .rxpois <- function(lambda) { .lambda <- as.character(substitute(lambda)) .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .lambda <- lambda - } - } + .lambda <- .uiArg(.lambda, .tmp) list(replace = paste0("rxpois(", .lambda, ")")) } @@ -44,11 +32,7 @@ rxUdfUi.rxpois <- rxUdfUi.rxpois .rxt <- function(df) { .df <- as.character(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .df <- df - } - } + .df <- .uiArg(.df, .tmp) list(replace = paste0("rxt(", .df, ")")) } @@ -61,18 +45,10 @@ rxUdfUi.rxt <- rxUdfUi.rxpois .rxunif <- function(min = 0, max = 1) { .min <- as.character(substitute(min)) .tmp <- suppressWarnings(try(force(min), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .min <- min - } - } + .min <- .uiArg(.min, .tmp) .max <- as.character(substitute(max)) .tmp <- suppressWarnings(try(force(max), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .max <- max - } - } + .max <- .uiArg(.max, .tmp) list(replace = paste0("rxunif(", .min, ", ", .max, ")")) } @@ -85,19 +61,11 @@ rxUdfUi.rxunif <- rxUdfUi.rxpois .rxweibull <- function(shape, scale = 1) { .shape <- as.character(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .shape <- shape - } - } + .shape <- .uiArg(.shape, .tmp) .scale <- as.character(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .scale <- scale - } - } - list(replace = paste0("rxweibull(", .shape, ", ", .scale, + .scale <- .uiArg(.scale, .tmp) + list(replace = paste0("rxweibull(", .shape, ", ", .scale, ")")) } @@ -110,11 +78,7 @@ rxUdfUi.rxweibull <- rxUdfUi.rxpois .rxgeom <- function(prob) { .prob <- as.character(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .prob <- prob - } - } + .prob <- .uiArg(.prob, .tmp) list(replace = paste0("rxgeom(", .prob, ")")) } @@ -127,19 +91,11 @@ rxUdfUi.rxgeom <- rxUdfUi.rxpois .rxbeta <- function(shape1, shape2) { .shape1 <- as.character(substitute(shape1)) .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .shape1 <- shape1 - } - } + .shape1 <- .uiArg(.shape1, .tmp) .shape2 <- as.character(substitute(shape2)) .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .shape2 <- shape2 - } - } - list(replace = paste0("rxbeta(", .shape1, ", ", .shape2, + .shape2 <- .uiArg(.shape2, .tmp) + list(replace = paste0("rxbeta(", .shape1, ", ", .shape2, ")")) } @@ -152,18 +108,10 @@ rxUdfUi.rxbeta <- rxUdfUi.rxpois .rxgamma <- function(shape, rate = 1) { .shape <- as.character(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .shape <- shape - } - } + .shape <- .uiArg(.shape, .tmp) .rate <- as.character(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .rate <- rate - } - } + .rate <- .uiArg(.rate, .tmp) list(replace = paste0("rxgamma(", .shape, ", ", .rate, ")")) } @@ -176,18 +124,10 @@ rxUdfUi.rxgamma <- rxUdfUi.rxpois .rxf <- function(df1, df2) { .df1 <- as.character(substitute(df1)) .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .df1 <- df1 - } - } + .df1 <- .uiArg(.df1, .tmp) .df2 <- as.character(substitute(df2)) .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .df2 <- df2 - } - } + .df2 <- .uiArg(.df2, .tmp) list(replace = paste0("rxf(", .df1, ", ", .df2, ")")) } @@ -200,11 +140,7 @@ rxUdfUi.rxf <- rxUdfUi.rxpois .rxexp <- function(rate) { .rate <- as.character(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .rate <- rate - } - } + .rate <- .uiArg(.rate, .tmp) list(replace = paste0("rxexp(", .rate, ")")) } @@ -217,11 +153,7 @@ rxUdfUi.rxexp <- rxUdfUi.rxpois .rxchisq <- function(df) { .df <- as.character(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .df <- df - } - } + .df <- .uiArg(.df, .tmp) list(replace = paste0("rxchisq(", .df, ")")) } @@ -234,19 +166,11 @@ rxUdfUi.rxchisq <- rxUdfUi.rxpois .rxcauchy <- function(location = 0, scale = 1) { .location <- as.character(substitute(location)) .tmp <- suppressWarnings(try(force(location), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .location <- location - } - } + .location <- .uiArg(.location, .tmp) .scale <- as.character(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .scale <- scale - } - } - list(replace = paste0("rxcauchy(", .location, ", ", .scale, + .scale <- .uiArg(.scale, .tmp) + list(replace = paste0("rxcauchy(", .location, ", ", .scale, ")")) } @@ -259,42 +183,66 @@ rxUdfUi.rxcauchy <- rxUdfUi.rxpois .rxbinom <- function(size, prob) { .size <- as.character(substitute(size)) .tmp <- suppressWarnings(try(force(size), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .size <- size - } - } + .size <- .uiArg(.size, .tmp) .prob <- as.character(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .prob <- prob - } - } + .prob <- .uiArg(.prob, .tmp) list(replace = paste0("rxbinom(", .size, ", ", .prob, ")")) } #'@export rxUdfUi.rxbinom <- rxUdfUi.rxpois +## nocov end +# This file is generated by .generateRandomUiFuns() in build.R +## nocov start +.logit <- function(x, low = 0, hi = 1) { + .x <- as.character(substitute(x)) + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + .x <- .uiArg(.x, .tmp) + .low <- as.character(substitute(low)) + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + .low <- .uiArg(.low, .tmp) + .hi <- as.character(substitute(hi)) + .tmp <- suppressWarnings(try(force(hi), silent = TRUE)) + .hi <- .uiArg(.hi, .tmp) + list(replace = paste0("logit(", .x, ", ", .low, ", ", .hi, + ")")) +} + +#'@export +rxUdfUi.logit <- rxUdfUi.rxpois + +## nocov end +# This file is generated by .generateRandomUiFuns() in build.R +## nocov start +.expit <- function(x, low = 0, hi = 1) { + .x <- as.character(substitute(x)) + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + .x <- .uiArg(.x, .tmp) + .low <- as.character(substitute(low)) + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + .low <- .uiArg(.low, .tmp) + .hi <- as.character(substitute(hi)) + .tmp <- suppressWarnings(try(force(hi), silent = TRUE)) + .hi <- .uiArg(.hi, .tmp) + list(replace = paste0("expit(", .x, ", ", .low, ", ", .hi, + ")")) +} + +#'@export +rxUdfUi.expit <- rxUdfUi.rxpois + ## nocov end # This file is generated by .generateRandomUiFuns() in build.R ## nocov start .rinorm <- function(mean = 0, sd = 1) { .mean <- as.character(substitute(mean)) .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .mean <- mean - } - } + .mean <- .uiArg(.mean, .tmp) .sd <- as.character(substitute(sd)) .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .sd <- sd - } - } + .sd <- .uiArg(.sd, .tmp) list(replace = paste0("rinorm(", .mean, ", ", .sd, ")")) } @@ -307,11 +255,7 @@ rxUdfUi.rinorm <- rxUdfUi.rxpois .ripois <- function(lambda) { .lambda <- as.character(substitute(lambda)) .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .lambda <- lambda - } - } + .lambda <- .uiArg(.lambda, .tmp) list(replace = paste0("ripois(", .lambda, ")")) } @@ -324,11 +268,7 @@ rxUdfUi.ripois <- rxUdfUi.rxpois .rit <- function(df) { .df <- as.character(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .df <- df - } - } + .df <- .uiArg(.df, .tmp) list(replace = paste0("rit(", .df, ")")) } @@ -341,18 +281,10 @@ rxUdfUi.rit <- rxUdfUi.rxpois .riunif <- function(min = 0, max = 1) { .min <- as.character(substitute(min)) .tmp <- suppressWarnings(try(force(min), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .min <- min - } - } + .min <- .uiArg(.min, .tmp) .max <- as.character(substitute(max)) .tmp <- suppressWarnings(try(force(max), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .max <- max - } - } + .max <- .uiArg(.max, .tmp) list(replace = paste0("riunif(", .min, ", ", .max, ")")) } @@ -365,19 +297,11 @@ rxUdfUi.riunif <- rxUdfUi.rxpois .riweibull <- function(shape, scale = 1) { .shape <- as.character(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .shape <- shape - } - } + .shape <- .uiArg(.shape, .tmp) .scale <- as.character(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .scale <- scale - } - } - list(replace = paste0("riweibull(", .shape, ", ", .scale, + .scale <- .uiArg(.scale, .tmp) + list(replace = paste0("riweibull(", .shape, ", ", .scale, ")")) } @@ -390,11 +314,7 @@ rxUdfUi.riweibull <- rxUdfUi.rxpois .rigeom <- function(prob) { .prob <- as.character(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .prob <- prob - } - } + .prob <- .uiArg(.prob, .tmp) list(replace = paste0("rigeom(", .prob, ")")) } @@ -407,19 +327,11 @@ rxUdfUi.rigeom <- rxUdfUi.rxpois .ribeta <- function(shape1, shape2) { .shape1 <- as.character(substitute(shape1)) .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .shape1 <- shape1 - } - } + .shape1 <- .uiArg(.shape1, .tmp) .shape2 <- as.character(substitute(shape2)) .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .shape2 <- shape2 - } - } - list(replace = paste0("ribeta(", .shape1, ", ", .shape2, + .shape2 <- .uiArg(.shape2, .tmp) + list(replace = paste0("ribeta(", .shape1, ", ", .shape2, ")")) } @@ -432,18 +344,10 @@ rxUdfUi.ribeta <- rxUdfUi.rxpois .rigamma <- function(shape, rate = 1) { .shape <- as.character(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .shape <- shape - } - } + .shape <- .uiArg(.shape, .tmp) .rate <- as.character(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .rate <- rate - } - } + .rate <- .uiArg(.rate, .tmp) list(replace = paste0("rigamma(", .shape, ", ", .rate, ")")) } @@ -456,18 +360,10 @@ rxUdfUi.rigamma <- rxUdfUi.rxpois .rif <- function(df1, df2) { .df1 <- as.character(substitute(df1)) .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .df1 <- df1 - } - } + .df1 <- .uiArg(.df1, .tmp) .df2 <- as.character(substitute(df2)) .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .df2 <- df2 - } - } + .df2 <- .uiArg(.df2, .tmp) list(replace = paste0("rif(", .df1, ", ", .df2, ")")) } @@ -480,11 +376,7 @@ rxUdfUi.rif <- rxUdfUi.rxpois .riexp <- function(rate) { .rate <- as.character(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .rate <- rate - } - } + .rate <- .uiArg(.rate, .tmp) list(replace = paste0("riexp(", .rate, ")")) } @@ -497,11 +389,7 @@ rxUdfUi.riexp <- rxUdfUi.rxpois .richisq <- function(df) { .df <- as.character(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .df <- df - } - } + .df <- .uiArg(.df, .tmp) list(replace = paste0("richisq(", .df, ")")) } @@ -514,19 +402,11 @@ rxUdfUi.richisq <- rxUdfUi.rxpois .ricauchy <- function(location = 0, scale = 1) { .location <- as.character(substitute(location)) .tmp <- suppressWarnings(try(force(location), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .location <- location - } - } + .location <- .uiArg(.location, .tmp) .scale <- as.character(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .scale <- scale - } - } - list(replace = paste0("ricauchy(", .location, ", ", .scale, + .scale <- .uiArg(.scale, .tmp) + list(replace = paste0("ricauchy(", .location, ", ", .scale, ")")) } @@ -539,18 +419,10 @@ rxUdfUi.ricauchy <- rxUdfUi.rxpois .ribinom <- function(size, prob) { .size <- as.character(substitute(size)) .tmp <- suppressWarnings(try(force(size), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .size <- size - } - } + .size <- .uiArg(.size, .tmp) .prob <- as.character(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp) || is.numeric(.tmp)) { - .prob <- prob - } - } + .prob <- .uiArg(.prob, .tmp) list(replace = paste0("ribinom(", .size, ", ", .prob, ")")) } @@ -558,3 +430,43 @@ rxUdfUi.ricauchy <- rxUdfUi.rxpois rxUdfUi.ribinom <- rxUdfUi.rxpois ## nocov end +# This file is generated by .generateRandomUiFuns() in build.R +## nocov start +.logit <- function(x, low = 0, hi = 1) { + .x <- as.character(substitute(x)) + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + .x <- .uiArg(.x, .tmp) + .low <- as.character(substitute(low)) + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + .low <- .uiArg(.low, .tmp) + .hi <- as.character(substitute(hi)) + .tmp <- suppressWarnings(try(force(hi), silent = TRUE)) + .hi <- .uiArg(.hi, .tmp) + list(replace = paste0("logit(", .x, ", ", .low, ", ", .hi, + ")")) +} + +#'@export +rxUdfUi.logit <- rxUdfUi.rxpois + +## nocov end +# This file is generated by .generateRandomUiFuns() in build.R +## nocov start +.expit <- function(x, low = 0, hi = 1) { + .x <- as.character(substitute(x)) + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + .x <- .uiArg(.x, .tmp) + .low <- as.character(substitute(low)) + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + .low <- .uiArg(.low, .tmp) + .hi <- as.character(substitute(hi)) + .tmp <- suppressWarnings(try(force(hi), silent = TRUE)) + .hi <- .uiArg(.hi, .tmp) + list(replace = paste0("expit(", .x, ", ", .low, ", ", .hi, + ")")) +} + +#'@export +rxUdfUi.expit <- rxUdfUi.rxpois + +## nocov end diff --git a/R/utils.R b/R/utils.R index 297bdbeec..4291904fb 100644 --- a/R/utils.R +++ b/R/utils.R @@ -507,7 +507,20 @@ expit <- function(alpha, low = 0, high = 1) { .rxTransform(alpha, 1.0, low, high, 4L, TRUE) } - +#' Handle arguments for ui functions +#' +#' Note this is an internal function but it is exported in case it is +#' useful. +#' +#' @param char This is the character equivalent of the argument +#' @param f This is the forced equivalent of the argument +#' @return character representing the underlying rxode2 code for the argument +#' @export +#' @author Matthew L. Fidler +#' @keywords internal +#' @examples +#' +#' .uiArg("1.0", 1.0) .uiArg <- function(char, f) { if (!inherits(f, "try-error")) { if (is.numeric(f)) { @@ -520,47 +533,6 @@ expit <- function(alpha, low = 0, high = 1) { char } -.logit <- function(x, low = 0, high=1) { - .x <- as.character(substitute(x)) - .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - .x <- .uiArg(.x, .tmp) - - .low <- as.character(substitute(low)) - .tmp <- suppressWarnings(try(force(low), silent = TRUE)) - .low <- .uiArg(.low, .tmp) - - .high <- as.character(substitute(high)) - .tmp <- suppressWarnings(try(force(high), silent = TRUE)) - .high <- .uiArg(.high, .tmp) - list(replace = paste0("logit(", .x, ", ", .low, ", ", .high, ")")) -} - -.expit <- function(x, low = 0, high=1) { - .x <- as.character(substitute(x)) - .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - .x <- .uiArg(.x, .tmp) - - .low <- as.character(substitute(low)) - .tmp <- suppressWarnings(try(force(low), silent = TRUE)) - .low <- .uiArg(.low, .tmp) - - .high <- as.character(substitute(high)) - .tmp <- suppressWarnings(try(force(high), silent = TRUE)) - .high <- .uiArg(.high, .tmp) - list(replace = paste0("expit(", .x, ", ", .low, ", ", .high, ")")) -} - -#' @export -rxUdfUi.logit <- function(fun) { - .fun <- fun - .fun[[1]] <- str2lang(paste0(".", deparse1(fun[[1]]))) - eval(.fun) -} - -#' @export -rxUdfUi.expit <- rxUdfUi.logit - - #' @rdname logit #' @export logitNormInfo <- function(mean = 0, sd = 1, low = 0, high = 1, abs.tol = 1e-6, ...) { From dc6a0b775b144fd2ea120222ef8321c2439ce84f Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Fri, 13 Dec 2024 13:30:12 -0600 Subject: [PATCH 08/11] update logit generation --- R/build.R | 8 +++---- R/rxrandomui.R | 60 +++++++++----------------------------------------- 2 files changed, 14 insertions(+), 54 deletions(-) diff --git a/R/build.R b/R/build.R index 2018cdb2c..69f3681f1 100644 --- a/R/build.R +++ b/R/build.R @@ -104,12 +104,12 @@ d/dt(blood) = a*intestine - b*blood "rxexp"="rate", "rxchisq"="df", "rxcauchy"=c(location = 0, scale = 1), - "rxbinom"=c("size", "prob"), - "logit"=c("x"=NA, "low"=0, "hi"=1), - "expit"=c("x"=NA, "low"=0, "hi"=1)) + "rxbinom"=c("size", "prob")) .lst2 <- .lst names(.lst2) <- gsub("rx", "ri", names(.lst2)) - .lst <- c(.lst, .lst2) + .lst <- c(.lst, .lst2, + list("logit"=c("x"=NA, "low"=0, "high"=1), + "expit"=c("x"=NA, "low"=0, "high"=1))) paste0("# This file is generated by .generateRandomUiFuns() in build.R\n## nocov start\n", vapply(names(.lst), function(fun) { if (is.null(names(.lst[[fun]]))) { diff --git a/R/rxrandomui.R b/R/rxrandomui.R index 45cc95fdc..713d860c0 100644 --- a/R/rxrandomui.R +++ b/R/rxrandomui.R @@ -193,46 +193,6 @@ rxUdfUi.rxcauchy <- rxUdfUi.rxpois #'@export rxUdfUi.rxbinom <- rxUdfUi.rxpois -## nocov end -# This file is generated by .generateRandomUiFuns() in build.R -## nocov start -.logit <- function(x, low = 0, hi = 1) { - .x <- as.character(substitute(x)) - .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - .x <- .uiArg(.x, .tmp) - .low <- as.character(substitute(low)) - .tmp <- suppressWarnings(try(force(low), silent = TRUE)) - .low <- .uiArg(.low, .tmp) - .hi <- as.character(substitute(hi)) - .tmp <- suppressWarnings(try(force(hi), silent = TRUE)) - .hi <- .uiArg(.hi, .tmp) - list(replace = paste0("logit(", .x, ", ", .low, ", ", .hi, - ")")) -} - -#'@export -rxUdfUi.logit <- rxUdfUi.rxpois - -## nocov end -# This file is generated by .generateRandomUiFuns() in build.R -## nocov start -.expit <- function(x, low = 0, hi = 1) { - .x <- as.character(substitute(x)) - .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - .x <- .uiArg(.x, .tmp) - .low <- as.character(substitute(low)) - .tmp <- suppressWarnings(try(force(low), silent = TRUE)) - .low <- .uiArg(.low, .tmp) - .hi <- as.character(substitute(hi)) - .tmp <- suppressWarnings(try(force(hi), silent = TRUE)) - .hi <- .uiArg(.hi, .tmp) - list(replace = paste0("expit(", .x, ", ", .low, ", ", .hi, - ")")) -} - -#'@export -rxUdfUi.expit <- rxUdfUi.rxpois - ## nocov end # This file is generated by .generateRandomUiFuns() in build.R ## nocov start @@ -432,17 +392,17 @@ rxUdfUi.ribinom <- rxUdfUi.rxpois ## nocov end # This file is generated by .generateRandomUiFuns() in build.R ## nocov start -.logit <- function(x, low = 0, hi = 1) { +.logit <- function(x, low = 0, high = 1) { .x <- as.character(substitute(x)) .tmp <- suppressWarnings(try(force(x), silent = TRUE)) .x <- .uiArg(.x, .tmp) .low <- as.character(substitute(low)) .tmp <- suppressWarnings(try(force(low), silent = TRUE)) .low <- .uiArg(.low, .tmp) - .hi <- as.character(substitute(hi)) - .tmp <- suppressWarnings(try(force(hi), silent = TRUE)) - .hi <- .uiArg(.hi, .tmp) - list(replace = paste0("logit(", .x, ", ", .low, ", ", .hi, + .high <- as.character(substitute(high)) + .tmp <- suppressWarnings(try(force(high), silent = TRUE)) + .high <- .uiArg(.high, .tmp) + list(replace = paste0("logit(", .x, ", ", .low, ", ", .high, ")")) } @@ -452,17 +412,17 @@ rxUdfUi.logit <- rxUdfUi.rxpois ## nocov end # This file is generated by .generateRandomUiFuns() in build.R ## nocov start -.expit <- function(x, low = 0, hi = 1) { +.expit <- function(x, low = 0, high = 1) { .x <- as.character(substitute(x)) .tmp <- suppressWarnings(try(force(x), silent = TRUE)) .x <- .uiArg(.x, .tmp) .low <- as.character(substitute(low)) .tmp <- suppressWarnings(try(force(low), silent = TRUE)) .low <- .uiArg(.low, .tmp) - .hi <- as.character(substitute(hi)) - .tmp <- suppressWarnings(try(force(hi), silent = TRUE)) - .hi <- .uiArg(.hi, .tmp) - list(replace = paste0("expit(", .x, ", ", .low, ", ", .hi, + .high <- as.character(substitute(high)) + .tmp <- suppressWarnings(try(force(high), silent = TRUE)) + .high <- .uiArg(.high, .tmp) + list(replace = paste0("expit(", .x, ", ", .low, ", ", .high, ")")) } From 97232c5b94ddeedb974193672b29b6f387d232d7 Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Fri, 13 Dec 2024 18:39:48 -0600 Subject: [PATCH 09/11] Use deparsing for complex expressions --- R/build.R | 9 +- R/rudfui.R | 3 +- R/rxrandomui.R | 432 ++++++++++++++++++++++++++++++++++++----------- man/dot-uiArg.Rd | 28 +++ 4 files changed, 373 insertions(+), 99 deletions(-) create mode 100644 man/dot-uiArg.Rd diff --git a/R/build.R b/R/build.R index 69f3681f1..98b84e671 100644 --- a/R/build.R +++ b/R/build.R @@ -68,8 +68,13 @@ d/dt(blood) = a*intestine - b*blood .arg <- str2lang(arg) .ret <- bquote({ .(.dotArg) <- as.character(substitute(.(.arg))) - .tmp <- suppressWarnings(try(force(.(.arg)), silent=TRUE)) - .(.dotArg) <- .uiArg(.(.dotArg), .tmp) + if (is.character(.(.dotArg)) && + length(.(.dotArg)) > 1) { + .(.dotArg) <- deparse1(substitute(.(.arg))) + } else { + .tmp <- suppressWarnings(try(force(.(.arg)), silent=TRUE)) + .(.dotArg) <- .uiArg(.(.dotArg), .tmp) + } }) lapply(seq_along(.ret)[-1], function(i) { .ret[[i]] diff --git a/R/rudfui.R b/R/rudfui.R index c98580313..13ddc07ee 100644 --- a/R/rudfui.R +++ b/R/rudfui.R @@ -248,7 +248,8 @@ rxUdfUiParsing <- function() { } expr <- .t } else { - stop("rxode2 ui user function '", .c, "' failed to produce code that could be parsed in the", + + stop("rxode2 ui user function '", .c, "' failed to produce code that could be parsed", call.=FALSE) } .handleUdifUiBeforeOrAfter("before", .e, env, .c) diff --git a/R/rxrandomui.R b/R/rxrandomui.R index 713d860c0..e4748e7bf 100644 --- a/R/rxrandomui.R +++ b/R/rxrandomui.R @@ -2,11 +2,21 @@ ## nocov start .rxnorm <- function(mean = 0, sd = 1) { .mean <- as.character(substitute(mean)) - .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) - .mean <- .uiArg(.mean, .tmp) + if (is.character(.mean) && length(.mean) > 1) { + .mean <- deparse1(substitute(mean)) + } + else { + .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) + .mean <- .uiArg(.mean, .tmp) + } .sd <- as.character(substitute(sd)) - .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) - .sd <- .uiArg(.sd, .tmp) + if (is.character(.sd) && length(.sd) > 1) { + .sd <- deparse1(substitute(sd)) + } + else { + .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) + .sd <- .uiArg(.sd, .tmp) + } list(replace = paste0("rxnorm(", .mean, ", ", .sd, ")")) } @@ -18,8 +28,13 @@ rxUdfUi.rxnorm <- rxUdfUi.rxpois ## nocov start .rxpois <- function(lambda) { .lambda <- as.character(substitute(lambda)) - .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) - .lambda <- .uiArg(.lambda, .tmp) + if (is.character(.lambda) && length(.lambda) > 1) { + .lambda <- deparse1(substitute(lambda)) + } + else { + .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) + .lambda <- .uiArg(.lambda, .tmp) + } list(replace = paste0("rxpois(", .lambda, ")")) } @@ -31,8 +46,13 @@ rxUdfUi.rxpois <- rxUdfUi.rxpois ## nocov start .rxt <- function(df) { .df <- as.character(substitute(df)) - .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - .df <- .uiArg(.df, .tmp) + if (is.character(.df) && length(.df) > 1) { + .df <- deparse1(substitute(df)) + } + else { + .tmp <- suppressWarnings(try(force(df), silent = TRUE)) + .df <- .uiArg(.df, .tmp) + } list(replace = paste0("rxt(", .df, ")")) } @@ -44,11 +64,21 @@ rxUdfUi.rxt <- rxUdfUi.rxpois ## nocov start .rxunif <- function(min = 0, max = 1) { .min <- as.character(substitute(min)) - .tmp <- suppressWarnings(try(force(min), silent = TRUE)) - .min <- .uiArg(.min, .tmp) + if (is.character(.min) && length(.min) > 1) { + .min <- deparse1(substitute(min)) + } + else { + .tmp <- suppressWarnings(try(force(min), silent = TRUE)) + .min <- .uiArg(.min, .tmp) + } .max <- as.character(substitute(max)) - .tmp <- suppressWarnings(try(force(max), silent = TRUE)) - .max <- .uiArg(.max, .tmp) + if (is.character(.max) && length(.max) > 1) { + .max <- deparse1(substitute(max)) + } + else { + .tmp <- suppressWarnings(try(force(max), silent = TRUE)) + .max <- .uiArg(.max, .tmp) + } list(replace = paste0("rxunif(", .min, ", ", .max, ")")) } @@ -60,11 +90,21 @@ rxUdfUi.rxunif <- rxUdfUi.rxpois ## nocov start .rxweibull <- function(shape, scale = 1) { .shape <- as.character(substitute(shape)) - .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - .shape <- .uiArg(.shape, .tmp) + if (is.character(.shape) && length(.shape) > 1) { + .shape <- deparse1(substitute(shape)) + } + else { + .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) + .shape <- .uiArg(.shape, .tmp) + } .scale <- as.character(substitute(scale)) - .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - .scale <- .uiArg(.scale, .tmp) + if (is.character(.scale) && length(.scale) > 1) { + .scale <- deparse1(substitute(scale)) + } + else { + .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) + .scale <- .uiArg(.scale, .tmp) + } list(replace = paste0("rxweibull(", .shape, ", ", .scale, ")")) } @@ -77,8 +117,13 @@ rxUdfUi.rxweibull <- rxUdfUi.rxpois ## nocov start .rxgeom <- function(prob) { .prob <- as.character(substitute(prob)) - .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - .prob <- .uiArg(.prob, .tmp) + if (is.character(.prob) && length(.prob) > 1) { + .prob <- deparse1(substitute(prob)) + } + else { + .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) + .prob <- .uiArg(.prob, .tmp) + } list(replace = paste0("rxgeom(", .prob, ")")) } @@ -90,11 +135,21 @@ rxUdfUi.rxgeom <- rxUdfUi.rxpois ## nocov start .rxbeta <- function(shape1, shape2) { .shape1 <- as.character(substitute(shape1)) - .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) - .shape1 <- .uiArg(.shape1, .tmp) + if (is.character(.shape1) && length(.shape1) > 1) { + .shape1 <- deparse1(substitute(shape1)) + } + else { + .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) + .shape1 <- .uiArg(.shape1, .tmp) + } .shape2 <- as.character(substitute(shape2)) - .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) - .shape2 <- .uiArg(.shape2, .tmp) + if (is.character(.shape2) && length(.shape2) > 1) { + .shape2 <- deparse1(substitute(shape2)) + } + else { + .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) + .shape2 <- .uiArg(.shape2, .tmp) + } list(replace = paste0("rxbeta(", .shape1, ", ", .shape2, ")")) } @@ -107,11 +162,21 @@ rxUdfUi.rxbeta <- rxUdfUi.rxpois ## nocov start .rxgamma <- function(shape, rate = 1) { .shape <- as.character(substitute(shape)) - .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - .shape <- .uiArg(.shape, .tmp) + if (is.character(.shape) && length(.shape) > 1) { + .shape <- deparse1(substitute(shape)) + } + else { + .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) + .shape <- .uiArg(.shape, .tmp) + } .rate <- as.character(substitute(rate)) - .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - .rate <- .uiArg(.rate, .tmp) + if (is.character(.rate) && length(.rate) > 1) { + .rate <- deparse1(substitute(rate)) + } + else { + .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) + .rate <- .uiArg(.rate, .tmp) + } list(replace = paste0("rxgamma(", .shape, ", ", .rate, ")")) } @@ -123,11 +188,21 @@ rxUdfUi.rxgamma <- rxUdfUi.rxpois ## nocov start .rxf <- function(df1, df2) { .df1 <- as.character(substitute(df1)) - .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) - .df1 <- .uiArg(.df1, .tmp) + if (is.character(.df1) && length(.df1) > 1) { + .df1 <- deparse1(substitute(df1)) + } + else { + .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) + .df1 <- .uiArg(.df1, .tmp) + } .df2 <- as.character(substitute(df2)) - .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) - .df2 <- .uiArg(.df2, .tmp) + if (is.character(.df2) && length(.df2) > 1) { + .df2 <- deparse1(substitute(df2)) + } + else { + .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) + .df2 <- .uiArg(.df2, .tmp) + } list(replace = paste0("rxf(", .df1, ", ", .df2, ")")) } @@ -139,8 +214,13 @@ rxUdfUi.rxf <- rxUdfUi.rxpois ## nocov start .rxexp <- function(rate) { .rate <- as.character(substitute(rate)) - .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - .rate <- .uiArg(.rate, .tmp) + if (is.character(.rate) && length(.rate) > 1) { + .rate <- deparse1(substitute(rate)) + } + else { + .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) + .rate <- .uiArg(.rate, .tmp) + } list(replace = paste0("rxexp(", .rate, ")")) } @@ -152,8 +232,13 @@ rxUdfUi.rxexp <- rxUdfUi.rxpois ## nocov start .rxchisq <- function(df) { .df <- as.character(substitute(df)) - .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - .df <- .uiArg(.df, .tmp) + if (is.character(.df) && length(.df) > 1) { + .df <- deparse1(substitute(df)) + } + else { + .tmp <- suppressWarnings(try(force(df), silent = TRUE)) + .df <- .uiArg(.df, .tmp) + } list(replace = paste0("rxchisq(", .df, ")")) } @@ -165,11 +250,21 @@ rxUdfUi.rxchisq <- rxUdfUi.rxpois ## nocov start .rxcauchy <- function(location = 0, scale = 1) { .location <- as.character(substitute(location)) - .tmp <- suppressWarnings(try(force(location), silent = TRUE)) - .location <- .uiArg(.location, .tmp) + if (is.character(.location) && length(.location) > 1) { + .location <- deparse1(substitute(location)) + } + else { + .tmp <- suppressWarnings(try(force(location), silent = TRUE)) + .location <- .uiArg(.location, .tmp) + } .scale <- as.character(substitute(scale)) - .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - .scale <- .uiArg(.scale, .tmp) + if (is.character(.scale) && length(.scale) > 1) { + .scale <- deparse1(substitute(scale)) + } + else { + .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) + .scale <- .uiArg(.scale, .tmp) + } list(replace = paste0("rxcauchy(", .location, ", ", .scale, ")")) } @@ -182,11 +277,21 @@ rxUdfUi.rxcauchy <- rxUdfUi.rxpois ## nocov start .rxbinom <- function(size, prob) { .size <- as.character(substitute(size)) - .tmp <- suppressWarnings(try(force(size), silent = TRUE)) - .size <- .uiArg(.size, .tmp) + if (is.character(.size) && length(.size) > 1) { + .size <- deparse1(substitute(size)) + } + else { + .tmp <- suppressWarnings(try(force(size), silent = TRUE)) + .size <- .uiArg(.size, .tmp) + } .prob <- as.character(substitute(prob)) - .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - .prob <- .uiArg(.prob, .tmp) + if (is.character(.prob) && length(.prob) > 1) { + .prob <- deparse1(substitute(prob)) + } + else { + .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) + .prob <- .uiArg(.prob, .tmp) + } list(replace = paste0("rxbinom(", .size, ", ", .prob, ")")) } @@ -198,11 +303,21 @@ rxUdfUi.rxbinom <- rxUdfUi.rxpois ## nocov start .rinorm <- function(mean = 0, sd = 1) { .mean <- as.character(substitute(mean)) - .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) - .mean <- .uiArg(.mean, .tmp) + if (is.character(.mean) && length(.mean) > 1) { + .mean <- deparse1(substitute(mean)) + } + else { + .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) + .mean <- .uiArg(.mean, .tmp) + } .sd <- as.character(substitute(sd)) - .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) - .sd <- .uiArg(.sd, .tmp) + if (is.character(.sd) && length(.sd) > 1) { + .sd <- deparse1(substitute(sd)) + } + else { + .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) + .sd <- .uiArg(.sd, .tmp) + } list(replace = paste0("rinorm(", .mean, ", ", .sd, ")")) } @@ -214,8 +329,13 @@ rxUdfUi.rinorm <- rxUdfUi.rxpois ## nocov start .ripois <- function(lambda) { .lambda <- as.character(substitute(lambda)) - .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) - .lambda <- .uiArg(.lambda, .tmp) + if (is.character(.lambda) && length(.lambda) > 1) { + .lambda <- deparse1(substitute(lambda)) + } + else { + .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) + .lambda <- .uiArg(.lambda, .tmp) + } list(replace = paste0("ripois(", .lambda, ")")) } @@ -227,8 +347,13 @@ rxUdfUi.ripois <- rxUdfUi.rxpois ## nocov start .rit <- function(df) { .df <- as.character(substitute(df)) - .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - .df <- .uiArg(.df, .tmp) + if (is.character(.df) && length(.df) > 1) { + .df <- deparse1(substitute(df)) + } + else { + .tmp <- suppressWarnings(try(force(df), silent = TRUE)) + .df <- .uiArg(.df, .tmp) + } list(replace = paste0("rit(", .df, ")")) } @@ -240,11 +365,21 @@ rxUdfUi.rit <- rxUdfUi.rxpois ## nocov start .riunif <- function(min = 0, max = 1) { .min <- as.character(substitute(min)) - .tmp <- suppressWarnings(try(force(min), silent = TRUE)) - .min <- .uiArg(.min, .tmp) + if (is.character(.min) && length(.min) > 1) { + .min <- deparse1(substitute(min)) + } + else { + .tmp <- suppressWarnings(try(force(min), silent = TRUE)) + .min <- .uiArg(.min, .tmp) + } .max <- as.character(substitute(max)) - .tmp <- suppressWarnings(try(force(max), silent = TRUE)) - .max <- .uiArg(.max, .tmp) + if (is.character(.max) && length(.max) > 1) { + .max <- deparse1(substitute(max)) + } + else { + .tmp <- suppressWarnings(try(force(max), silent = TRUE)) + .max <- .uiArg(.max, .tmp) + } list(replace = paste0("riunif(", .min, ", ", .max, ")")) } @@ -256,11 +391,21 @@ rxUdfUi.riunif <- rxUdfUi.rxpois ## nocov start .riweibull <- function(shape, scale = 1) { .shape <- as.character(substitute(shape)) - .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - .shape <- .uiArg(.shape, .tmp) + if (is.character(.shape) && length(.shape) > 1) { + .shape <- deparse1(substitute(shape)) + } + else { + .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) + .shape <- .uiArg(.shape, .tmp) + } .scale <- as.character(substitute(scale)) - .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - .scale <- .uiArg(.scale, .tmp) + if (is.character(.scale) && length(.scale) > 1) { + .scale <- deparse1(substitute(scale)) + } + else { + .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) + .scale <- .uiArg(.scale, .tmp) + } list(replace = paste0("riweibull(", .shape, ", ", .scale, ")")) } @@ -273,8 +418,13 @@ rxUdfUi.riweibull <- rxUdfUi.rxpois ## nocov start .rigeom <- function(prob) { .prob <- as.character(substitute(prob)) - .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - .prob <- .uiArg(.prob, .tmp) + if (is.character(.prob) && length(.prob) > 1) { + .prob <- deparse1(substitute(prob)) + } + else { + .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) + .prob <- .uiArg(.prob, .tmp) + } list(replace = paste0("rigeom(", .prob, ")")) } @@ -286,11 +436,21 @@ rxUdfUi.rigeom <- rxUdfUi.rxpois ## nocov start .ribeta <- function(shape1, shape2) { .shape1 <- as.character(substitute(shape1)) - .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) - .shape1 <- .uiArg(.shape1, .tmp) + if (is.character(.shape1) && length(.shape1) > 1) { + .shape1 <- deparse1(substitute(shape1)) + } + else { + .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) + .shape1 <- .uiArg(.shape1, .tmp) + } .shape2 <- as.character(substitute(shape2)) - .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) - .shape2 <- .uiArg(.shape2, .tmp) + if (is.character(.shape2) && length(.shape2) > 1) { + .shape2 <- deparse1(substitute(shape2)) + } + else { + .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) + .shape2 <- .uiArg(.shape2, .tmp) + } list(replace = paste0("ribeta(", .shape1, ", ", .shape2, ")")) } @@ -303,11 +463,21 @@ rxUdfUi.ribeta <- rxUdfUi.rxpois ## nocov start .rigamma <- function(shape, rate = 1) { .shape <- as.character(substitute(shape)) - .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - .shape <- .uiArg(.shape, .tmp) + if (is.character(.shape) && length(.shape) > 1) { + .shape <- deparse1(substitute(shape)) + } + else { + .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) + .shape <- .uiArg(.shape, .tmp) + } .rate <- as.character(substitute(rate)) - .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - .rate <- .uiArg(.rate, .tmp) + if (is.character(.rate) && length(.rate) > 1) { + .rate <- deparse1(substitute(rate)) + } + else { + .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) + .rate <- .uiArg(.rate, .tmp) + } list(replace = paste0("rigamma(", .shape, ", ", .rate, ")")) } @@ -319,11 +489,21 @@ rxUdfUi.rigamma <- rxUdfUi.rxpois ## nocov start .rif <- function(df1, df2) { .df1 <- as.character(substitute(df1)) - .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) - .df1 <- .uiArg(.df1, .tmp) + if (is.character(.df1) && length(.df1) > 1) { + .df1 <- deparse1(substitute(df1)) + } + else { + .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) + .df1 <- .uiArg(.df1, .tmp) + } .df2 <- as.character(substitute(df2)) - .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) - .df2 <- .uiArg(.df2, .tmp) + if (is.character(.df2) && length(.df2) > 1) { + .df2 <- deparse1(substitute(df2)) + } + else { + .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) + .df2 <- .uiArg(.df2, .tmp) + } list(replace = paste0("rif(", .df1, ", ", .df2, ")")) } @@ -335,8 +515,13 @@ rxUdfUi.rif <- rxUdfUi.rxpois ## nocov start .riexp <- function(rate) { .rate <- as.character(substitute(rate)) - .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - .rate <- .uiArg(.rate, .tmp) + if (is.character(.rate) && length(.rate) > 1) { + .rate <- deparse1(substitute(rate)) + } + else { + .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) + .rate <- .uiArg(.rate, .tmp) + } list(replace = paste0("riexp(", .rate, ")")) } @@ -348,8 +533,13 @@ rxUdfUi.riexp <- rxUdfUi.rxpois ## nocov start .richisq <- function(df) { .df <- as.character(substitute(df)) - .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - .df <- .uiArg(.df, .tmp) + if (is.character(.df) && length(.df) > 1) { + .df <- deparse1(substitute(df)) + } + else { + .tmp <- suppressWarnings(try(force(df), silent = TRUE)) + .df <- .uiArg(.df, .tmp) + } list(replace = paste0("richisq(", .df, ")")) } @@ -361,11 +551,21 @@ rxUdfUi.richisq <- rxUdfUi.rxpois ## nocov start .ricauchy <- function(location = 0, scale = 1) { .location <- as.character(substitute(location)) - .tmp <- suppressWarnings(try(force(location), silent = TRUE)) - .location <- .uiArg(.location, .tmp) + if (is.character(.location) && length(.location) > 1) { + .location <- deparse1(substitute(location)) + } + else { + .tmp <- suppressWarnings(try(force(location), silent = TRUE)) + .location <- .uiArg(.location, .tmp) + } .scale <- as.character(substitute(scale)) - .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - .scale <- .uiArg(.scale, .tmp) + if (is.character(.scale) && length(.scale) > 1) { + .scale <- deparse1(substitute(scale)) + } + else { + .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) + .scale <- .uiArg(.scale, .tmp) + } list(replace = paste0("ricauchy(", .location, ", ", .scale, ")")) } @@ -378,11 +578,21 @@ rxUdfUi.ricauchy <- rxUdfUi.rxpois ## nocov start .ribinom <- function(size, prob) { .size <- as.character(substitute(size)) - .tmp <- suppressWarnings(try(force(size), silent = TRUE)) - .size <- .uiArg(.size, .tmp) + if (is.character(.size) && length(.size) > 1) { + .size <- deparse1(substitute(size)) + } + else { + .tmp <- suppressWarnings(try(force(size), silent = TRUE)) + .size <- .uiArg(.size, .tmp) + } .prob <- as.character(substitute(prob)) - .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - .prob <- .uiArg(.prob, .tmp) + if (is.character(.prob) && length(.prob) > 1) { + .prob <- deparse1(substitute(prob)) + } + else { + .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) + .prob <- .uiArg(.prob, .tmp) + } list(replace = paste0("ribinom(", .size, ", ", .prob, ")")) } @@ -394,14 +604,29 @@ rxUdfUi.ribinom <- rxUdfUi.rxpois ## nocov start .logit <- function(x, low = 0, high = 1) { .x <- as.character(substitute(x)) - .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - .x <- .uiArg(.x, .tmp) + if (is.character(.x) && length(.x) > 1) { + .x <- deparse1(substitute(x)) + } + else { + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + .x <- .uiArg(.x, .tmp) + } .low <- as.character(substitute(low)) - .tmp <- suppressWarnings(try(force(low), silent = TRUE)) - .low <- .uiArg(.low, .tmp) + if (is.character(.low) && length(.low) > 1) { + .low <- deparse1(substitute(low)) + } + else { + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + .low <- .uiArg(.low, .tmp) + } .high <- as.character(substitute(high)) - .tmp <- suppressWarnings(try(force(high), silent = TRUE)) - .high <- .uiArg(.high, .tmp) + if (is.character(.high) && length(.high) > 1) { + .high <- deparse1(substitute(high)) + } + else { + .tmp <- suppressWarnings(try(force(high), silent = TRUE)) + .high <- .uiArg(.high, .tmp) + } list(replace = paste0("logit(", .x, ", ", .low, ", ", .high, ")")) } @@ -414,14 +639,29 @@ rxUdfUi.logit <- rxUdfUi.rxpois ## nocov start .expit <- function(x, low = 0, high = 1) { .x <- as.character(substitute(x)) - .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - .x <- .uiArg(.x, .tmp) + if (is.character(.x) && length(.x) > 1) { + .x <- deparse1(substitute(x)) + } + else { + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + .x <- .uiArg(.x, .tmp) + } .low <- as.character(substitute(low)) - .tmp <- suppressWarnings(try(force(low), silent = TRUE)) - .low <- .uiArg(.low, .tmp) + if (is.character(.low) && length(.low) > 1) { + .low <- deparse1(substitute(low)) + } + else { + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + .low <- .uiArg(.low, .tmp) + } .high <- as.character(substitute(high)) - .tmp <- suppressWarnings(try(force(high), silent = TRUE)) - .high <- .uiArg(.high, .tmp) + if (is.character(.high) && length(.high) > 1) { + .high <- deparse1(substitute(high)) + } + else { + .tmp <- suppressWarnings(try(force(high), silent = TRUE)) + .high <- .uiArg(.high, .tmp) + } list(replace = paste0("expit(", .x, ", ", .low, ", ", .high, ")")) } diff --git a/man/dot-uiArg.Rd b/man/dot-uiArg.Rd new file mode 100644 index 000000000..382fe6c47 --- /dev/null +++ b/man/dot-uiArg.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{.uiArg} +\alias{.uiArg} +\title{Handle arguments for ui functions} +\usage{ +.uiArg(char, f) +} +\arguments{ +\item{char}{This is the character equivalent of the argument} + +\item{f}{This is the forced equivalent of the argument} +} +\value{ +character representing the underlying rxode2 code for the argument +} +\description{ +Note this is an internal function but it is exported in case it is +useful. +} +\examples{ + +.uiArg("1.0", 1.0) +} +\author{ +Matthew L. Fidler +} +\keyword{internal} From b46e150ca4d767ce862d1d86c9bc0954f85dd72f Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Fri, 13 Dec 2024 18:55:11 -0600 Subject: [PATCH 10/11] Update build and uiArg --- R/build.R | 10 +- R/rxrandomui.R | 480 ++++++++++++++--------------------------------- R/utils.R | 11 +- man/dot-uiArg.Rd | 6 +- 4 files changed, 159 insertions(+), 348 deletions(-) diff --git a/R/build.R b/R/build.R index 98b84e671..1e0f8a5af 100644 --- a/R/build.R +++ b/R/build.R @@ -68,13 +68,9 @@ d/dt(blood) = a*intestine - b*blood .arg <- str2lang(arg) .ret <- bquote({ .(.dotArg) <- as.character(substitute(.(.arg))) - if (is.character(.(.dotArg)) && - length(.(.dotArg)) > 1) { - .(.dotArg) <- deparse1(substitute(.(.arg))) - } else { - .tmp <- suppressWarnings(try(force(.(.arg)), silent=TRUE)) - .(.dotArg) <- .uiArg(.(.dotArg), .tmp) - } + .dp <- deparse1(substitute(.(.arg))) + .tmp <- suppressWarnings(try(force(.(.arg)), silent=TRUE)) + .(.dotArg) <- .uiArg(.(.dotArg), .tmp, .dp) }) lapply(seq_along(.ret)[-1], function(i) { .ret[[i]] diff --git a/R/rxrandomui.R b/R/rxrandomui.R index e4748e7bf..b01315e59 100644 --- a/R/rxrandomui.R +++ b/R/rxrandomui.R @@ -2,21 +2,13 @@ ## nocov start .rxnorm <- function(mean = 0, sd = 1) { .mean <- as.character(substitute(mean)) - if (is.character(.mean) && length(.mean) > 1) { - .mean <- deparse1(substitute(mean)) - } - else { - .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) - .mean <- .uiArg(.mean, .tmp) - } + .dp <- deparse1(substitute(mean)) + .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) + .mean <- .uiArg(.mean, .tmp, .dp) .sd <- as.character(substitute(sd)) - if (is.character(.sd) && length(.sd) > 1) { - .sd <- deparse1(substitute(sd)) - } - else { - .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) - .sd <- .uiArg(.sd, .tmp) - } + .dp <- deparse1(substitute(sd)) + .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) + .sd <- .uiArg(.sd, .tmp, .dp) list(replace = paste0("rxnorm(", .mean, ", ", .sd, ")")) } @@ -28,13 +20,9 @@ rxUdfUi.rxnorm <- rxUdfUi.rxpois ## nocov start .rxpois <- function(lambda) { .lambda <- as.character(substitute(lambda)) - if (is.character(.lambda) && length(.lambda) > 1) { - .lambda <- deparse1(substitute(lambda)) - } - else { - .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) - .lambda <- .uiArg(.lambda, .tmp) - } + .dp <- deparse1(substitute(lambda)) + .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) + .lambda <- .uiArg(.lambda, .tmp, .dp) list(replace = paste0("rxpois(", .lambda, ")")) } @@ -46,13 +34,9 @@ rxUdfUi.rxpois <- rxUdfUi.rxpois ## nocov start .rxt <- function(df) { .df <- as.character(substitute(df)) - if (is.character(.df) && length(.df) > 1) { - .df <- deparse1(substitute(df)) - } - else { - .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - .df <- .uiArg(.df, .tmp) - } + .dp <- deparse1(substitute(df)) + .tmp <- suppressWarnings(try(force(df), silent = TRUE)) + .df <- .uiArg(.df, .tmp, .dp) list(replace = paste0("rxt(", .df, ")")) } @@ -64,21 +48,13 @@ rxUdfUi.rxt <- rxUdfUi.rxpois ## nocov start .rxunif <- function(min = 0, max = 1) { .min <- as.character(substitute(min)) - if (is.character(.min) && length(.min) > 1) { - .min <- deparse1(substitute(min)) - } - else { - .tmp <- suppressWarnings(try(force(min), silent = TRUE)) - .min <- .uiArg(.min, .tmp) - } + .dp <- deparse1(substitute(min)) + .tmp <- suppressWarnings(try(force(min), silent = TRUE)) + .min <- .uiArg(.min, .tmp, .dp) .max <- as.character(substitute(max)) - if (is.character(.max) && length(.max) > 1) { - .max <- deparse1(substitute(max)) - } - else { - .tmp <- suppressWarnings(try(force(max), silent = TRUE)) - .max <- .uiArg(.max, .tmp) - } + .dp <- deparse1(substitute(max)) + .tmp <- suppressWarnings(try(force(max), silent = TRUE)) + .max <- .uiArg(.max, .tmp, .dp) list(replace = paste0("rxunif(", .min, ", ", .max, ")")) } @@ -90,21 +66,13 @@ rxUdfUi.rxunif <- rxUdfUi.rxpois ## nocov start .rxweibull <- function(shape, scale = 1) { .shape <- as.character(substitute(shape)) - if (is.character(.shape) && length(.shape) > 1) { - .shape <- deparse1(substitute(shape)) - } - else { - .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - .shape <- .uiArg(.shape, .tmp) - } + .dp <- deparse1(substitute(shape)) + .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) + .shape <- .uiArg(.shape, .tmp, .dp) .scale <- as.character(substitute(scale)) - if (is.character(.scale) && length(.scale) > 1) { - .scale <- deparse1(substitute(scale)) - } - else { - .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - .scale <- .uiArg(.scale, .tmp) - } + .dp <- deparse1(substitute(scale)) + .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) + .scale <- .uiArg(.scale, .tmp, .dp) list(replace = paste0("rxweibull(", .shape, ", ", .scale, ")")) } @@ -117,13 +85,9 @@ rxUdfUi.rxweibull <- rxUdfUi.rxpois ## nocov start .rxgeom <- function(prob) { .prob <- as.character(substitute(prob)) - if (is.character(.prob) && length(.prob) > 1) { - .prob <- deparse1(substitute(prob)) - } - else { - .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - .prob <- .uiArg(.prob, .tmp) - } + .dp <- deparse1(substitute(prob)) + .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) + .prob <- .uiArg(.prob, .tmp, .dp) list(replace = paste0("rxgeom(", .prob, ")")) } @@ -135,21 +99,13 @@ rxUdfUi.rxgeom <- rxUdfUi.rxpois ## nocov start .rxbeta <- function(shape1, shape2) { .shape1 <- as.character(substitute(shape1)) - if (is.character(.shape1) && length(.shape1) > 1) { - .shape1 <- deparse1(substitute(shape1)) - } - else { - .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) - .shape1 <- .uiArg(.shape1, .tmp) - } + .dp <- deparse1(substitute(shape1)) + .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) + .shape1 <- .uiArg(.shape1, .tmp, .dp) .shape2 <- as.character(substitute(shape2)) - if (is.character(.shape2) && length(.shape2) > 1) { - .shape2 <- deparse1(substitute(shape2)) - } - else { - .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) - .shape2 <- .uiArg(.shape2, .tmp) - } + .dp <- deparse1(substitute(shape2)) + .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) + .shape2 <- .uiArg(.shape2, .tmp, .dp) list(replace = paste0("rxbeta(", .shape1, ", ", .shape2, ")")) } @@ -162,21 +118,13 @@ rxUdfUi.rxbeta <- rxUdfUi.rxpois ## nocov start .rxgamma <- function(shape, rate = 1) { .shape <- as.character(substitute(shape)) - if (is.character(.shape) && length(.shape) > 1) { - .shape <- deparse1(substitute(shape)) - } - else { - .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - .shape <- .uiArg(.shape, .tmp) - } + .dp <- deparse1(substitute(shape)) + .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) + .shape <- .uiArg(.shape, .tmp, .dp) .rate <- as.character(substitute(rate)) - if (is.character(.rate) && length(.rate) > 1) { - .rate <- deparse1(substitute(rate)) - } - else { - .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - .rate <- .uiArg(.rate, .tmp) - } + .dp <- deparse1(substitute(rate)) + .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) + .rate <- .uiArg(.rate, .tmp, .dp) list(replace = paste0("rxgamma(", .shape, ", ", .rate, ")")) } @@ -188,21 +136,13 @@ rxUdfUi.rxgamma <- rxUdfUi.rxpois ## nocov start .rxf <- function(df1, df2) { .df1 <- as.character(substitute(df1)) - if (is.character(.df1) && length(.df1) > 1) { - .df1 <- deparse1(substitute(df1)) - } - else { - .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) - .df1 <- .uiArg(.df1, .tmp) - } + .dp <- deparse1(substitute(df1)) + .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) + .df1 <- .uiArg(.df1, .tmp, .dp) .df2 <- as.character(substitute(df2)) - if (is.character(.df2) && length(.df2) > 1) { - .df2 <- deparse1(substitute(df2)) - } - else { - .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) - .df2 <- .uiArg(.df2, .tmp) - } + .dp <- deparse1(substitute(df2)) + .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) + .df2 <- .uiArg(.df2, .tmp, .dp) list(replace = paste0("rxf(", .df1, ", ", .df2, ")")) } @@ -214,13 +154,9 @@ rxUdfUi.rxf <- rxUdfUi.rxpois ## nocov start .rxexp <- function(rate) { .rate <- as.character(substitute(rate)) - if (is.character(.rate) && length(.rate) > 1) { - .rate <- deparse1(substitute(rate)) - } - else { - .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - .rate <- .uiArg(.rate, .tmp) - } + .dp <- deparse1(substitute(rate)) + .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) + .rate <- .uiArg(.rate, .tmp, .dp) list(replace = paste0("rxexp(", .rate, ")")) } @@ -232,13 +168,9 @@ rxUdfUi.rxexp <- rxUdfUi.rxpois ## nocov start .rxchisq <- function(df) { .df <- as.character(substitute(df)) - if (is.character(.df) && length(.df) > 1) { - .df <- deparse1(substitute(df)) - } - else { - .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - .df <- .uiArg(.df, .tmp) - } + .dp <- deparse1(substitute(df)) + .tmp <- suppressWarnings(try(force(df), silent = TRUE)) + .df <- .uiArg(.df, .tmp, .dp) list(replace = paste0("rxchisq(", .df, ")")) } @@ -250,21 +182,13 @@ rxUdfUi.rxchisq <- rxUdfUi.rxpois ## nocov start .rxcauchy <- function(location = 0, scale = 1) { .location <- as.character(substitute(location)) - if (is.character(.location) && length(.location) > 1) { - .location <- deparse1(substitute(location)) - } - else { - .tmp <- suppressWarnings(try(force(location), silent = TRUE)) - .location <- .uiArg(.location, .tmp) - } + .dp <- deparse1(substitute(location)) + .tmp <- suppressWarnings(try(force(location), silent = TRUE)) + .location <- .uiArg(.location, .tmp, .dp) .scale <- as.character(substitute(scale)) - if (is.character(.scale) && length(.scale) > 1) { - .scale <- deparse1(substitute(scale)) - } - else { - .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - .scale <- .uiArg(.scale, .tmp) - } + .dp <- deparse1(substitute(scale)) + .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) + .scale <- .uiArg(.scale, .tmp, .dp) list(replace = paste0("rxcauchy(", .location, ", ", .scale, ")")) } @@ -277,21 +201,13 @@ rxUdfUi.rxcauchy <- rxUdfUi.rxpois ## nocov start .rxbinom <- function(size, prob) { .size <- as.character(substitute(size)) - if (is.character(.size) && length(.size) > 1) { - .size <- deparse1(substitute(size)) - } - else { - .tmp <- suppressWarnings(try(force(size), silent = TRUE)) - .size <- .uiArg(.size, .tmp) - } + .dp <- deparse1(substitute(size)) + .tmp <- suppressWarnings(try(force(size), silent = TRUE)) + .size <- .uiArg(.size, .tmp, .dp) .prob <- as.character(substitute(prob)) - if (is.character(.prob) && length(.prob) > 1) { - .prob <- deparse1(substitute(prob)) - } - else { - .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - .prob <- .uiArg(.prob, .tmp) - } + .dp <- deparse1(substitute(prob)) + .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) + .prob <- .uiArg(.prob, .tmp, .dp) list(replace = paste0("rxbinom(", .size, ", ", .prob, ")")) } @@ -303,21 +219,13 @@ rxUdfUi.rxbinom <- rxUdfUi.rxpois ## nocov start .rinorm <- function(mean = 0, sd = 1) { .mean <- as.character(substitute(mean)) - if (is.character(.mean) && length(.mean) > 1) { - .mean <- deparse1(substitute(mean)) - } - else { - .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) - .mean <- .uiArg(.mean, .tmp) - } + .dp <- deparse1(substitute(mean)) + .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) + .mean <- .uiArg(.mean, .tmp, .dp) .sd <- as.character(substitute(sd)) - if (is.character(.sd) && length(.sd) > 1) { - .sd <- deparse1(substitute(sd)) - } - else { - .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) - .sd <- .uiArg(.sd, .tmp) - } + .dp <- deparse1(substitute(sd)) + .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) + .sd <- .uiArg(.sd, .tmp, .dp) list(replace = paste0("rinorm(", .mean, ", ", .sd, ")")) } @@ -329,13 +237,9 @@ rxUdfUi.rinorm <- rxUdfUi.rxpois ## nocov start .ripois <- function(lambda) { .lambda <- as.character(substitute(lambda)) - if (is.character(.lambda) && length(.lambda) > 1) { - .lambda <- deparse1(substitute(lambda)) - } - else { - .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) - .lambda <- .uiArg(.lambda, .tmp) - } + .dp <- deparse1(substitute(lambda)) + .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) + .lambda <- .uiArg(.lambda, .tmp, .dp) list(replace = paste0("ripois(", .lambda, ")")) } @@ -347,13 +251,9 @@ rxUdfUi.ripois <- rxUdfUi.rxpois ## nocov start .rit <- function(df) { .df <- as.character(substitute(df)) - if (is.character(.df) && length(.df) > 1) { - .df <- deparse1(substitute(df)) - } - else { - .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - .df <- .uiArg(.df, .tmp) - } + .dp <- deparse1(substitute(df)) + .tmp <- suppressWarnings(try(force(df), silent = TRUE)) + .df <- .uiArg(.df, .tmp, .dp) list(replace = paste0("rit(", .df, ")")) } @@ -365,21 +265,13 @@ rxUdfUi.rit <- rxUdfUi.rxpois ## nocov start .riunif <- function(min = 0, max = 1) { .min <- as.character(substitute(min)) - if (is.character(.min) && length(.min) > 1) { - .min <- deparse1(substitute(min)) - } - else { - .tmp <- suppressWarnings(try(force(min), silent = TRUE)) - .min <- .uiArg(.min, .tmp) - } + .dp <- deparse1(substitute(min)) + .tmp <- suppressWarnings(try(force(min), silent = TRUE)) + .min <- .uiArg(.min, .tmp, .dp) .max <- as.character(substitute(max)) - if (is.character(.max) && length(.max) > 1) { - .max <- deparse1(substitute(max)) - } - else { - .tmp <- suppressWarnings(try(force(max), silent = TRUE)) - .max <- .uiArg(.max, .tmp) - } + .dp <- deparse1(substitute(max)) + .tmp <- suppressWarnings(try(force(max), silent = TRUE)) + .max <- .uiArg(.max, .tmp, .dp) list(replace = paste0("riunif(", .min, ", ", .max, ")")) } @@ -391,21 +283,13 @@ rxUdfUi.riunif <- rxUdfUi.rxpois ## nocov start .riweibull <- function(shape, scale = 1) { .shape <- as.character(substitute(shape)) - if (is.character(.shape) && length(.shape) > 1) { - .shape <- deparse1(substitute(shape)) - } - else { - .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - .shape <- .uiArg(.shape, .tmp) - } + .dp <- deparse1(substitute(shape)) + .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) + .shape <- .uiArg(.shape, .tmp, .dp) .scale <- as.character(substitute(scale)) - if (is.character(.scale) && length(.scale) > 1) { - .scale <- deparse1(substitute(scale)) - } - else { - .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - .scale <- .uiArg(.scale, .tmp) - } + .dp <- deparse1(substitute(scale)) + .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) + .scale <- .uiArg(.scale, .tmp, .dp) list(replace = paste0("riweibull(", .shape, ", ", .scale, ")")) } @@ -418,13 +302,9 @@ rxUdfUi.riweibull <- rxUdfUi.rxpois ## nocov start .rigeom <- function(prob) { .prob <- as.character(substitute(prob)) - if (is.character(.prob) && length(.prob) > 1) { - .prob <- deparse1(substitute(prob)) - } - else { - .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - .prob <- .uiArg(.prob, .tmp) - } + .dp <- deparse1(substitute(prob)) + .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) + .prob <- .uiArg(.prob, .tmp, .dp) list(replace = paste0("rigeom(", .prob, ")")) } @@ -436,21 +316,13 @@ rxUdfUi.rigeom <- rxUdfUi.rxpois ## nocov start .ribeta <- function(shape1, shape2) { .shape1 <- as.character(substitute(shape1)) - if (is.character(.shape1) && length(.shape1) > 1) { - .shape1 <- deparse1(substitute(shape1)) - } - else { - .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) - .shape1 <- .uiArg(.shape1, .tmp) - } + .dp <- deparse1(substitute(shape1)) + .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) + .shape1 <- .uiArg(.shape1, .tmp, .dp) .shape2 <- as.character(substitute(shape2)) - if (is.character(.shape2) && length(.shape2) > 1) { - .shape2 <- deparse1(substitute(shape2)) - } - else { - .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) - .shape2 <- .uiArg(.shape2, .tmp) - } + .dp <- deparse1(substitute(shape2)) + .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) + .shape2 <- .uiArg(.shape2, .tmp, .dp) list(replace = paste0("ribeta(", .shape1, ", ", .shape2, ")")) } @@ -463,21 +335,13 @@ rxUdfUi.ribeta <- rxUdfUi.rxpois ## nocov start .rigamma <- function(shape, rate = 1) { .shape <- as.character(substitute(shape)) - if (is.character(.shape) && length(.shape) > 1) { - .shape <- deparse1(substitute(shape)) - } - else { - .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - .shape <- .uiArg(.shape, .tmp) - } + .dp <- deparse1(substitute(shape)) + .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) + .shape <- .uiArg(.shape, .tmp, .dp) .rate <- as.character(substitute(rate)) - if (is.character(.rate) && length(.rate) > 1) { - .rate <- deparse1(substitute(rate)) - } - else { - .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - .rate <- .uiArg(.rate, .tmp) - } + .dp <- deparse1(substitute(rate)) + .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) + .rate <- .uiArg(.rate, .tmp, .dp) list(replace = paste0("rigamma(", .shape, ", ", .rate, ")")) } @@ -489,21 +353,13 @@ rxUdfUi.rigamma <- rxUdfUi.rxpois ## nocov start .rif <- function(df1, df2) { .df1 <- as.character(substitute(df1)) - if (is.character(.df1) && length(.df1) > 1) { - .df1 <- deparse1(substitute(df1)) - } - else { - .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) - .df1 <- .uiArg(.df1, .tmp) - } + .dp <- deparse1(substitute(df1)) + .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) + .df1 <- .uiArg(.df1, .tmp, .dp) .df2 <- as.character(substitute(df2)) - if (is.character(.df2) && length(.df2) > 1) { - .df2 <- deparse1(substitute(df2)) - } - else { - .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) - .df2 <- .uiArg(.df2, .tmp) - } + .dp <- deparse1(substitute(df2)) + .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) + .df2 <- .uiArg(.df2, .tmp, .dp) list(replace = paste0("rif(", .df1, ", ", .df2, ")")) } @@ -515,13 +371,9 @@ rxUdfUi.rif <- rxUdfUi.rxpois ## nocov start .riexp <- function(rate) { .rate <- as.character(substitute(rate)) - if (is.character(.rate) && length(.rate) > 1) { - .rate <- deparse1(substitute(rate)) - } - else { - .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - .rate <- .uiArg(.rate, .tmp) - } + .dp <- deparse1(substitute(rate)) + .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) + .rate <- .uiArg(.rate, .tmp, .dp) list(replace = paste0("riexp(", .rate, ")")) } @@ -533,13 +385,9 @@ rxUdfUi.riexp <- rxUdfUi.rxpois ## nocov start .richisq <- function(df) { .df <- as.character(substitute(df)) - if (is.character(.df) && length(.df) > 1) { - .df <- deparse1(substitute(df)) - } - else { - .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - .df <- .uiArg(.df, .tmp) - } + .dp <- deparse1(substitute(df)) + .tmp <- suppressWarnings(try(force(df), silent = TRUE)) + .df <- .uiArg(.df, .tmp, .dp) list(replace = paste0("richisq(", .df, ")")) } @@ -551,21 +399,13 @@ rxUdfUi.richisq <- rxUdfUi.rxpois ## nocov start .ricauchy <- function(location = 0, scale = 1) { .location <- as.character(substitute(location)) - if (is.character(.location) && length(.location) > 1) { - .location <- deparse1(substitute(location)) - } - else { - .tmp <- suppressWarnings(try(force(location), silent = TRUE)) - .location <- .uiArg(.location, .tmp) - } + .dp <- deparse1(substitute(location)) + .tmp <- suppressWarnings(try(force(location), silent = TRUE)) + .location <- .uiArg(.location, .tmp, .dp) .scale <- as.character(substitute(scale)) - if (is.character(.scale) && length(.scale) > 1) { - .scale <- deparse1(substitute(scale)) - } - else { - .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - .scale <- .uiArg(.scale, .tmp) - } + .dp <- deparse1(substitute(scale)) + .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) + .scale <- .uiArg(.scale, .tmp, .dp) list(replace = paste0("ricauchy(", .location, ", ", .scale, ")")) } @@ -578,21 +418,13 @@ rxUdfUi.ricauchy <- rxUdfUi.rxpois ## nocov start .ribinom <- function(size, prob) { .size <- as.character(substitute(size)) - if (is.character(.size) && length(.size) > 1) { - .size <- deparse1(substitute(size)) - } - else { - .tmp <- suppressWarnings(try(force(size), silent = TRUE)) - .size <- .uiArg(.size, .tmp) - } + .dp <- deparse1(substitute(size)) + .tmp <- suppressWarnings(try(force(size), silent = TRUE)) + .size <- .uiArg(.size, .tmp, .dp) .prob <- as.character(substitute(prob)) - if (is.character(.prob) && length(.prob) > 1) { - .prob <- deparse1(substitute(prob)) - } - else { - .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - .prob <- .uiArg(.prob, .tmp) - } + .dp <- deparse1(substitute(prob)) + .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) + .prob <- .uiArg(.prob, .tmp, .dp) list(replace = paste0("ribinom(", .size, ", ", .prob, ")")) } @@ -604,29 +436,17 @@ rxUdfUi.ribinom <- rxUdfUi.rxpois ## nocov start .logit <- function(x, low = 0, high = 1) { .x <- as.character(substitute(x)) - if (is.character(.x) && length(.x) > 1) { - .x <- deparse1(substitute(x)) - } - else { - .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - .x <- .uiArg(.x, .tmp) - } + .dp <- deparse1(substitute(x)) + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + .x <- .uiArg(.x, .tmp, .dp) .low <- as.character(substitute(low)) - if (is.character(.low) && length(.low) > 1) { - .low <- deparse1(substitute(low)) - } - else { - .tmp <- suppressWarnings(try(force(low), silent = TRUE)) - .low <- .uiArg(.low, .tmp) - } + .dp <- deparse1(substitute(low)) + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + .low <- .uiArg(.low, .tmp, .dp) .high <- as.character(substitute(high)) - if (is.character(.high) && length(.high) > 1) { - .high <- deparse1(substitute(high)) - } - else { - .tmp <- suppressWarnings(try(force(high), silent = TRUE)) - .high <- .uiArg(.high, .tmp) - } + .dp <- deparse1(substitute(high)) + .tmp <- suppressWarnings(try(force(high), silent = TRUE)) + .high <- .uiArg(.high, .tmp, .dp) list(replace = paste0("logit(", .x, ", ", .low, ", ", .high, ")")) } @@ -639,29 +459,17 @@ rxUdfUi.logit <- rxUdfUi.rxpois ## nocov start .expit <- function(x, low = 0, high = 1) { .x <- as.character(substitute(x)) - if (is.character(.x) && length(.x) > 1) { - .x <- deparse1(substitute(x)) - } - else { - .tmp <- suppressWarnings(try(force(x), silent = TRUE)) - .x <- .uiArg(.x, .tmp) - } + .dp <- deparse1(substitute(x)) + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + .x <- .uiArg(.x, .tmp, .dp) .low <- as.character(substitute(low)) - if (is.character(.low) && length(.low) > 1) { - .low <- deparse1(substitute(low)) - } - else { - .tmp <- suppressWarnings(try(force(low), silent = TRUE)) - .low <- .uiArg(.low, .tmp) - } + .dp <- deparse1(substitute(low)) + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + .low <- .uiArg(.low, .tmp, .dp) .high <- as.character(substitute(high)) - if (is.character(.high) && length(.high) > 1) { - .high <- deparse1(substitute(high)) - } - else { - .tmp <- suppressWarnings(try(force(high), silent = TRUE)) - .high <- .uiArg(.high, .tmp) - } + .dp <- deparse1(substitute(high)) + .tmp <- suppressWarnings(try(force(high), silent = TRUE)) + .high <- .uiArg(.high, .tmp, .dp) list(replace = paste0("expit(", .x, ", ", .low, ", ", .high, ")")) } diff --git a/R/utils.R b/R/utils.R index 4291904fb..05684e946 100644 --- a/R/utils.R +++ b/R/utils.R @@ -514,14 +514,15 @@ expit <- function(alpha, low = 0, high = 1) { #' #' @param char This is the character equivalent of the argument #' @param f This is the forced equivalent of the argument +#' @param dp This is deparsed expression #' @return character representing the underlying rxode2 code for the argument #' @export #' @author Matthew L. Fidler #' @keywords internal #' @examples #' -#' .uiArg("1.0", 1.0) -.uiArg <- function(char, f) { +#' .uiArg("1.0", 1.0, "1.0") +.uiArg <- function(char, f, dp) { if (!inherits(f, "try-error")) { if (is.numeric(f)) { return(as.character(f)) @@ -530,7 +531,11 @@ expit <- function(alpha, low = 0, high = 1) { return(f) } } - char + if (length(char) > 1) { + dp + } else { + char + } } #' @rdname logit diff --git a/man/dot-uiArg.Rd b/man/dot-uiArg.Rd index 382fe6c47..c12288d5c 100644 --- a/man/dot-uiArg.Rd +++ b/man/dot-uiArg.Rd @@ -4,12 +4,14 @@ \alias{.uiArg} \title{Handle arguments for ui functions} \usage{ -.uiArg(char, f) +.uiArg(char, f, dp) } \arguments{ \item{char}{This is the character equivalent of the argument} \item{f}{This is the forced equivalent of the argument} + +\item{dp}{This is deparsed expression} } \value{ character representing the underlying rxode2 code for the argument @@ -20,7 +22,7 @@ useful. } \examples{ -.uiArg("1.0", 1.0) +.uiArg("1.0", 1.0, "1.0") } \author{ Matthew L. Fidler From 0841f5150d4094f51031269bdd06c648e2c16f1d Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Fri, 13 Dec 2024 18:56:20 -0600 Subject: [PATCH 11/11] Add to news --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 1252515e4..fd41347c0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,9 @@ - Updated random ui models like `rxnorm(sd=10)` to accept complex numeric expressions like `rxnorm(sd=10+1)`. +- Updated random ui models to accept complex non-numeric expressions + like `rxnorm(sd=a+b)` + - Rework the `tad()` and related functions so they use the same interface as compartments (this way they do not depend on the order of compartments); See #815. For mu-referencing, Also allow dummy