diff --git a/NAMESPACE b/NAMESPACE index acec56761..934de1e63 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) @@ -341,6 +343,7 @@ export(.udfEnvSet) export(.udfEnvSetUdf) export(.udfExists) export(.udfMd5Info) +export(.uiArg) export(.useUtf) export(.vecDf) export(ELU) diff --git a/NEWS.md b/NEWS.md index dc9722cc0..fd41347c0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,14 @@ # 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)`. + +- 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 diff --git a/R/build.R b/R/build.R index 8ca93c905..1e0f8a5af 100644 --- a/R/build.R +++ b/R/build.R @@ -68,12 +68,9 @@ d/dt(blood) = a*intestine - b*blood .arg <- str2lang(arg) .ret <- bquote({ .(.dotArg) <- as.character(substitute(.(.arg))) + .dp <- deparse1(substitute(.(.arg))) .tmp <- suppressWarnings(try(force(.(.arg)), silent=TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .(.dotArg) <- .(.arg) - } - } + .(.dotArg) <- .uiArg(.(.dotArg), .tmp, .dp) }) lapply(seq_along(.ret)[-1], function(i) { .ret[[i]] @@ -111,7 +108,9 @@ d/dt(blood) = a*intestine - b*blood "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/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 5c1b5da80..b01315e59 100644 --- a/R/rxrandomui.R +++ b/R/rxrandomui.R @@ -2,19 +2,13 @@ ## nocov start .rxnorm <- function(mean = 0, sd = 1) { .mean <- as.character(substitute(mean)) + .dp <- deparse1(substitute(mean)) .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .mean <- mean - } - } + .mean <- .uiArg(.mean, .tmp, .dp) .sd <- as.character(substitute(sd)) + .dp <- deparse1(substitute(sd)) .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .sd <- sd - } - } + .sd <- .uiArg(.sd, .tmp, .dp) list(replace = paste0("rxnorm(", .mean, ", ", .sd, ")")) } @@ -26,12 +20,9 @@ rxUdfUi.rxnorm <- rxUdfUi.rxpois ## nocov start .rxpois <- function(lambda) { .lambda <- as.character(substitute(lambda)) + .dp <- deparse1(substitute(lambda)) .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .lambda <- lambda - } - } + .lambda <- .uiArg(.lambda, .tmp, .dp) list(replace = paste0("rxpois(", .lambda, ")")) } @@ -43,12 +34,9 @@ rxUdfUi.rxpois <- rxUdfUi.rxpois ## nocov start .rxt <- function(df) { .df <- as.character(substitute(df)) + .dp <- deparse1(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .df <- df - } - } + .df <- .uiArg(.df, .tmp, .dp) list(replace = paste0("rxt(", .df, ")")) } @@ -60,19 +48,13 @@ rxUdfUi.rxt <- rxUdfUi.rxpois ## nocov start .rxunif <- function(min = 0, max = 1) { .min <- as.character(substitute(min)) + .dp <- deparse1(substitute(min)) .tmp <- suppressWarnings(try(force(min), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .min <- min - } - } + .min <- .uiArg(.min, .tmp, .dp) .max <- as.character(substitute(max)) + .dp <- deparse1(substitute(max)) .tmp <- suppressWarnings(try(force(max), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .max <- max - } - } + .max <- .uiArg(.max, .tmp, .dp) list(replace = paste0("rxunif(", .min, ", ", .max, ")")) } @@ -84,19 +66,13 @@ rxUdfUi.rxunif <- rxUdfUi.rxpois ## nocov start .rxweibull <- function(shape, scale = 1) { .shape <- as.character(substitute(shape)) + .dp <- deparse1(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .shape <- shape - } - } + .shape <- .uiArg(.shape, .tmp, .dp) .scale <- as.character(substitute(scale)) + .dp <- deparse1(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .scale <- scale - } - } + .scale <- .uiArg(.scale, .tmp, .dp) list(replace = paste0("rxweibull(", .shape, ", ", .scale, ")")) } @@ -109,12 +85,9 @@ rxUdfUi.rxweibull <- rxUdfUi.rxpois ## nocov start .rxgeom <- function(prob) { .prob <- as.character(substitute(prob)) + .dp <- deparse1(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .prob <- prob - } - } + .prob <- .uiArg(.prob, .tmp, .dp) list(replace = paste0("rxgeom(", .prob, ")")) } @@ -126,19 +99,13 @@ rxUdfUi.rxgeom <- rxUdfUi.rxpois ## nocov start .rxbeta <- function(shape1, shape2) { .shape1 <- as.character(substitute(shape1)) + .dp <- deparse1(substitute(shape1)) .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .shape1 <- shape1 - } - } + .shape1 <- .uiArg(.shape1, .tmp, .dp) .shape2 <- as.character(substitute(shape2)) + .dp <- deparse1(substitute(shape2)) .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .shape2 <- shape2 - } - } + .shape2 <- .uiArg(.shape2, .tmp, .dp) list(replace = paste0("rxbeta(", .shape1, ", ", .shape2, ")")) } @@ -151,19 +118,13 @@ rxUdfUi.rxbeta <- rxUdfUi.rxpois ## nocov start .rxgamma <- function(shape, rate = 1) { .shape <- as.character(substitute(shape)) + .dp <- deparse1(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .shape <- shape - } - } + .shape <- .uiArg(.shape, .tmp, .dp) .rate <- as.character(substitute(rate)) + .dp <- deparse1(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .rate <- rate - } - } + .rate <- .uiArg(.rate, .tmp, .dp) list(replace = paste0("rxgamma(", .shape, ", ", .rate, ")")) } @@ -175,19 +136,13 @@ rxUdfUi.rxgamma <- rxUdfUi.rxpois ## nocov start .rxf <- function(df1, df2) { .df1 <- as.character(substitute(df1)) + .dp <- deparse1(substitute(df1)) .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .df1 <- df1 - } - } + .df1 <- .uiArg(.df1, .tmp, .dp) .df2 <- as.character(substitute(df2)) + .dp <- deparse1(substitute(df2)) .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .df2 <- df2 - } - } + .df2 <- .uiArg(.df2, .tmp, .dp) list(replace = paste0("rxf(", .df1, ", ", .df2, ")")) } @@ -199,12 +154,9 @@ rxUdfUi.rxf <- rxUdfUi.rxpois ## nocov start .rxexp <- function(rate) { .rate <- as.character(substitute(rate)) + .dp <- deparse1(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .rate <- rate - } - } + .rate <- .uiArg(.rate, .tmp, .dp) list(replace = paste0("rxexp(", .rate, ")")) } @@ -216,12 +168,9 @@ rxUdfUi.rxexp <- rxUdfUi.rxpois ## nocov start .rxchisq <- function(df) { .df <- as.character(substitute(df)) + .dp <- deparse1(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .df <- df - } - } + .df <- .uiArg(.df, .tmp, .dp) list(replace = paste0("rxchisq(", .df, ")")) } @@ -233,19 +182,13 @@ rxUdfUi.rxchisq <- rxUdfUi.rxpois ## nocov start .rxcauchy <- function(location = 0, scale = 1) { .location <- as.character(substitute(location)) + .dp <- deparse1(substitute(location)) .tmp <- suppressWarnings(try(force(location), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .location <- location - } - } + .location <- .uiArg(.location, .tmp, .dp) .scale <- as.character(substitute(scale)) + .dp <- deparse1(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .scale <- scale - } - } + .scale <- .uiArg(.scale, .tmp, .dp) list(replace = paste0("rxcauchy(", .location, ", ", .scale, ")")) } @@ -258,19 +201,13 @@ rxUdfUi.rxcauchy <- rxUdfUi.rxpois ## nocov start .rxbinom <- function(size, prob) { .size <- as.character(substitute(size)) + .dp <- deparse1(substitute(size)) .tmp <- suppressWarnings(try(force(size), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .size <- size - } - } + .size <- .uiArg(.size, .tmp, .dp) .prob <- as.character(substitute(prob)) + .dp <- deparse1(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .prob <- prob - } - } + .prob <- .uiArg(.prob, .tmp, .dp) list(replace = paste0("rxbinom(", .size, ", ", .prob, ")")) } @@ -282,19 +219,13 @@ rxUdfUi.rxbinom <- rxUdfUi.rxpois ## nocov start .rinorm <- function(mean = 0, sd = 1) { .mean <- as.character(substitute(mean)) + .dp <- deparse1(substitute(mean)) .tmp <- suppressWarnings(try(force(mean), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .mean <- mean - } - } + .mean <- .uiArg(.mean, .tmp, .dp) .sd <- as.character(substitute(sd)) + .dp <- deparse1(substitute(sd)) .tmp <- suppressWarnings(try(force(sd), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .sd <- sd - } - } + .sd <- .uiArg(.sd, .tmp, .dp) list(replace = paste0("rinorm(", .mean, ", ", .sd, ")")) } @@ -306,12 +237,9 @@ rxUdfUi.rinorm <- rxUdfUi.rxpois ## nocov start .ripois <- function(lambda) { .lambda <- as.character(substitute(lambda)) + .dp <- deparse1(substitute(lambda)) .tmp <- suppressWarnings(try(force(lambda), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .lambda <- lambda - } - } + .lambda <- .uiArg(.lambda, .tmp, .dp) list(replace = paste0("ripois(", .lambda, ")")) } @@ -323,12 +251,9 @@ rxUdfUi.ripois <- rxUdfUi.rxpois ## nocov start .rit <- function(df) { .df <- as.character(substitute(df)) + .dp <- deparse1(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .df <- df - } - } + .df <- .uiArg(.df, .tmp, .dp) list(replace = paste0("rit(", .df, ")")) } @@ -340,19 +265,13 @@ rxUdfUi.rit <- rxUdfUi.rxpois ## nocov start .riunif <- function(min = 0, max = 1) { .min <- as.character(substitute(min)) + .dp <- deparse1(substitute(min)) .tmp <- suppressWarnings(try(force(min), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .min <- min - } - } + .min <- .uiArg(.min, .tmp, .dp) .max <- as.character(substitute(max)) + .dp <- deparse1(substitute(max)) .tmp <- suppressWarnings(try(force(max), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .max <- max - } - } + .max <- .uiArg(.max, .tmp, .dp) list(replace = paste0("riunif(", .min, ", ", .max, ")")) } @@ -364,19 +283,13 @@ rxUdfUi.riunif <- rxUdfUi.rxpois ## nocov start .riweibull <- function(shape, scale = 1) { .shape <- as.character(substitute(shape)) + .dp <- deparse1(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .shape <- shape - } - } + .shape <- .uiArg(.shape, .tmp, .dp) .scale <- as.character(substitute(scale)) + .dp <- deparse1(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .scale <- scale - } - } + .scale <- .uiArg(.scale, .tmp, .dp) list(replace = paste0("riweibull(", .shape, ", ", .scale, ")")) } @@ -389,12 +302,9 @@ rxUdfUi.riweibull <- rxUdfUi.rxpois ## nocov start .rigeom <- function(prob) { .prob <- as.character(substitute(prob)) + .dp <- deparse1(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .prob <- prob - } - } + .prob <- .uiArg(.prob, .tmp, .dp) list(replace = paste0("rigeom(", .prob, ")")) } @@ -406,19 +316,13 @@ rxUdfUi.rigeom <- rxUdfUi.rxpois ## nocov start .ribeta <- function(shape1, shape2) { .shape1 <- as.character(substitute(shape1)) + .dp <- deparse1(substitute(shape1)) .tmp <- suppressWarnings(try(force(shape1), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .shape1 <- shape1 - } - } + .shape1 <- .uiArg(.shape1, .tmp, .dp) .shape2 <- as.character(substitute(shape2)) + .dp <- deparse1(substitute(shape2)) .tmp <- suppressWarnings(try(force(shape2), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .shape2 <- shape2 - } - } + .shape2 <- .uiArg(.shape2, .tmp, .dp) list(replace = paste0("ribeta(", .shape1, ", ", .shape2, ")")) } @@ -431,19 +335,13 @@ rxUdfUi.ribeta <- rxUdfUi.rxpois ## nocov start .rigamma <- function(shape, rate = 1) { .shape <- as.character(substitute(shape)) + .dp <- deparse1(substitute(shape)) .tmp <- suppressWarnings(try(force(shape), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .shape <- shape - } - } + .shape <- .uiArg(.shape, .tmp, .dp) .rate <- as.character(substitute(rate)) + .dp <- deparse1(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .rate <- rate - } - } + .rate <- .uiArg(.rate, .tmp, .dp) list(replace = paste0("rigamma(", .shape, ", ", .rate, ")")) } @@ -455,19 +353,13 @@ rxUdfUi.rigamma <- rxUdfUi.rxpois ## nocov start .rif <- function(df1, df2) { .df1 <- as.character(substitute(df1)) + .dp <- deparse1(substitute(df1)) .tmp <- suppressWarnings(try(force(df1), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .df1 <- df1 - } - } + .df1 <- .uiArg(.df1, .tmp, .dp) .df2 <- as.character(substitute(df2)) + .dp <- deparse1(substitute(df2)) .tmp <- suppressWarnings(try(force(df2), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .df2 <- df2 - } - } + .df2 <- .uiArg(.df2, .tmp, .dp) list(replace = paste0("rif(", .df1, ", ", .df2, ")")) } @@ -479,12 +371,9 @@ rxUdfUi.rif <- rxUdfUi.rxpois ## nocov start .riexp <- function(rate) { .rate <- as.character(substitute(rate)) + .dp <- deparse1(substitute(rate)) .tmp <- suppressWarnings(try(force(rate), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .rate <- rate - } - } + .rate <- .uiArg(.rate, .tmp, .dp) list(replace = paste0("riexp(", .rate, ")")) } @@ -496,12 +385,9 @@ rxUdfUi.riexp <- rxUdfUi.rxpois ## nocov start .richisq <- function(df) { .df <- as.character(substitute(df)) + .dp <- deparse1(substitute(df)) .tmp <- suppressWarnings(try(force(df), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .df <- df - } - } + .df <- .uiArg(.df, .tmp, .dp) list(replace = paste0("richisq(", .df, ")")) } @@ -513,19 +399,13 @@ rxUdfUi.richisq <- rxUdfUi.rxpois ## nocov start .ricauchy <- function(location = 0, scale = 1) { .location <- as.character(substitute(location)) + .dp <- deparse1(substitute(location)) .tmp <- suppressWarnings(try(force(location), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .location <- location - } - } + .location <- .uiArg(.location, .tmp, .dp) .scale <- as.character(substitute(scale)) + .dp <- deparse1(substitute(scale)) .tmp <- suppressWarnings(try(force(scale), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .scale <- scale - } - } + .scale <- .uiArg(.scale, .tmp, .dp) list(replace = paste0("ricauchy(", .location, ", ", .scale, ")")) } @@ -538,19 +418,13 @@ rxUdfUi.ricauchy <- rxUdfUi.rxpois ## nocov start .ribinom <- function(size, prob) { .size <- as.character(substitute(size)) + .dp <- deparse1(substitute(size)) .tmp <- suppressWarnings(try(force(size), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .size <- size - } - } + .size <- .uiArg(.size, .tmp, .dp) .prob <- as.character(substitute(prob)) + .dp <- deparse1(substitute(prob)) .tmp <- suppressWarnings(try(force(prob), silent = TRUE)) - if (!inherits(.tmp, "try-error")) { - if (is.character(.tmp)) { - .prob <- prob - } - } + .prob <- .uiArg(.prob, .tmp, .dp) list(replace = paste0("ribinom(", .size, ", ", .prob, ")")) } @@ -558,3 +432,49 @@ 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, high = 1) { + .x <- as.character(substitute(x)) + .dp <- deparse1(substitute(x)) + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + .x <- .uiArg(.x, .tmp, .dp) + .low <- as.character(substitute(low)) + .dp <- deparse1(substitute(low)) + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + .low <- .uiArg(.low, .tmp, .dp) + .high <- as.character(substitute(high)) + .dp <- deparse1(substitute(high)) + .tmp <- suppressWarnings(try(force(high), silent = TRUE)) + .high <- .uiArg(.high, .tmp, .dp) + list(replace = paste0("logit(", .x, ", ", .low, ", ", .high, + ")")) +} + +#'@export +rxUdfUi.logit <- rxUdfUi.rxpois + +## nocov end +# This file is generated by .generateRandomUiFuns() in build.R +## nocov start +.expit <- function(x, low = 0, high = 1) { + .x <- as.character(substitute(x)) + .dp <- deparse1(substitute(x)) + .tmp <- suppressWarnings(try(force(x), silent = TRUE)) + .x <- .uiArg(.x, .tmp, .dp) + .low <- as.character(substitute(low)) + .dp <- deparse1(substitute(low)) + .tmp <- suppressWarnings(try(force(low), silent = TRUE)) + .low <- .uiArg(.low, .tmp, .dp) + .high <- as.character(substitute(high)) + .dp <- deparse1(substitute(high)) + .tmp <- suppressWarnings(try(force(high), silent = TRUE)) + .high <- .uiArg(.high, .tmp, .dp) + list(replace = paste0("expit(", .x, ", ", .low, ", ", .high, + ")")) +} + +#'@export +rxUdfUi.expit <- rxUdfUi.rxpois + +## nocov end diff --git a/R/utils.R b/R/utils.R index 4e8090347..05684e946 100644 --- a/R/utils.R +++ b/R/utils.R @@ -507,6 +507,37 @@ 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 +#' @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, "1.0") +.uiArg <- function(char, f, dp) { + if (!inherits(f, "try-error")) { + if (is.numeric(f)) { + return(as.character(f)) + } + if (is.character(f)) { + return(f) + } + } + if (length(char) > 1) { + dp + } else { + char + } +} + #' @rdname logit #' @export logitNormInfo <- function(mean = 0, sd = 1, low = 0, high = 1, abs.tol = 1e-6, ...) { diff --git a/man/dot-uiArg.Rd b/man/dot-uiArg.Rd new file mode 100644 index 000000000..c12288d5c --- /dev/null +++ b/man/dot-uiArg.Rd @@ -0,0 +1,30 @@ +% 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, 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 +} +\description{ +Note this is an internal function but it is exported in case it is +useful. +} +\examples{ + +.uiArg("1.0", 1.0, "1.0") +} +\author{ +Matthew L. Fidler +} +\keyword{internal} diff --git a/tests/testthat/test-logit.R b/tests/testthat/test-logit.R index 0b3573985..9e6277338 100644 --- a/tests/testthat/test-logit.R +++ b/tests/testthat/test-logit.R @@ -1,4 +1,37 @@ rxTest({ + + test_that("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)"),