Skip to content

Commit

Permalink
Don't assign x on printing. See Issue #29
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfidler committed Feb 6, 2018
1 parent 747ec5f commit 6308f49
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export(nlme_ode)
export(nlmixr)
export(nlmixrBounds)
export(nlmixrForget)
export(nlmixrPrint)
export(nlmixrUI)
export(nlmixrValidate)
export(nlmixrVersion)
Expand Down
14 changes: 7 additions & 7 deletions R/focei_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ print.focei.fit <- function(x, ...) {
} else if (is(x, "nlmixr.ui.saem")){
saem <- fit$saem;
uif <- env$uif;
message(sprintf("nlmixr SAEM fit (%s)\n", ifelse(is.null(uif$nmodel$lin.solved), "ODE", "Solved")))
message(sprintf("nlmixr SAEM fit (%s); OBJF based on FOCEi approximation.\n", ifelse(is.null(uif$nmodel$lin.solved), "ODE", "Solved")))
} else {
message(sprintf("nlmixr FOCEI fit (%s)\n", ifelse(fit$focei.control$grad, "with global gradient", "without global gradient")));
}
Expand All @@ -125,12 +125,12 @@ print.focei.fit <- function(x, ...) {
if (!is.null(nlme)){
message("FOCEi-based goodness of fit metrics:")
}
RxODE::rxPrint(df.objf)
nlmixrPrint(df.objf)
if (!is.null(nlme)){
message("\nnlme-based goodness of fit metrics:")
df.objf <- data.frame(AIC=AIC(as.nlme(x)), BIC=BIC(as.nlme(x)),"Log-likelihood"=as.numeric(logLik(as.nlme(x))),
row.names="", check.names=FALSE)
RxODE::rxPrint(df.objf)
nlmixrPrint(df.objf)
}
message("\nTime (sec; $time):");
print(fit$time);
Expand Down Expand Up @@ -1033,11 +1033,11 @@ focei.fit.data.frame0 <- function(data,
message("Model:")
RxODE::rxCat(model$pred.only)
message("Needed Covariates:")
RxODE::rxPrint(cov.names)
nlmixrPrint(cov.names)
stop("Not all the covariates are in the dataset.")
}
message("Needed Covariates:")
RxODE::rxPrint(cov.names)
nlmixrPrint(cov.names)
}

## RxODE(rxNorm(model$inner), modName="test");
Expand Down Expand Up @@ -1122,7 +1122,7 @@ focei.fit.data.frame0 <- function(data,
}
if (!con$NOTRUN){
message("Boundaries:");
RxODE::rxPrint(data.frame(lower,inits.vec,upper));
nlmixrPrint(data.frame(lower,inits.vec,upper));
}
names(inits.vec) = NULL
if (con$scale.to == 0){
Expand All @@ -1145,7 +1145,7 @@ focei.fit.data.frame0 <- function(data,
par.upper[w.neg] <- tmp;
if (!con$NOTRUN){
if (!is.null(con$scale.to)){
RxODE::rxPrint(data.frame(par.lower,scaled=rep(con$scale.to, length(inits.vec)),par.upper))
nlmixrPrint(data.frame(par.lower,scaled=rep(con$scale.to, length(inits.vec)),par.upper))
}
if (do.sink){
message("\nKey:")
Expand Down
12 changes: 7 additions & 5 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ nlmixrUI <- function(fun){
w <- which(regexpr(rex::rex(start, any_spaces, "#", anything), fun2) != -1);
if (length(w) > 0 && all(lhs0 != "desc")){
w2 <- w[1];
for (i in 2:length(w)){
if (w[i] - 1 == w[i - 1]){
w2[i] <- w[i];
} else {
break;
if (length(w) > 1){
for (i in 2:length(w)){
if (w[i] - 1 == w[i - 1]){
w2[i] <- w[i];
} else {
break;
}
}
}
desc <- paste(gsub(rex::rex(any_spaces, end), "", gsub(rex::rex(start, any_spaces, any_of("#"), any_spaces), "", fun2[w2])), collapse=" ");
Expand Down
18 changes: 18 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,21 @@ collectWarnings <- function(expr){
return(ret);
}

##' Print x using the message facility
##'
##' This allows the suppressMessages to work on print functions. This
##' captures the output via R.Util's captureOutput function and then
##' sends it through the message routine.
##'
##' catpureOutput was used since it is much faster than the internal
##' capture.output see https://www.r-bloggers.com/performance-captureoutput-is-much-faster-than-capture-output/
##' @param x object to print
##' @param ... Other things output
##' @author Matthew L. Fidler
##' @export
##' @keywords internal
nlmixrPrint <- function(x, ...){
this.env <- environment();
message(invisible(paste(R.utils::captureOutput(assign("x", print(x, ...), this.env)), collapse="\n")), appendLF=TRUE);
invisible(x)
}
10 changes: 8 additions & 2 deletions tests/testthat/test-ui-bad-models.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ rxPermissive({
cp ~ add(add.err)
})
}

expect_error(nlmixr(uif.ode), rex::rex("Model error: initial estimates provided without variables being used: prop.err"))

uif <- function(){
Expand Down Expand Up @@ -136,7 +137,10 @@ rxPermissive({
cp ~ add(add.err)
})
}
expect_error(nlmixr(uif), rex::rex("The following THETAs are unnamed: THETA[4]"))
##, rex::rex("The following THETAs are unnamed: THETA[4]")

expect_error(nlmixr(uif))

uif <- function(){
ini({
tka <- exp(0.5)
Expand All @@ -157,7 +161,9 @@ rxPermissive({
cp ~ add(add.err)
})
}
expect_error(nlmixr(uif), rex::rex("The following ETAs are unnamed: ETA[2]"))

## rex::rex("The following ETAs are unnamed: ETA[2]")
expect_error(nlmixr(uif))

})

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-ui-pred-err.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rxPermissive({

fn1 <- function(){
KA = KA + eta.KA
CL <- CL + eta.CL
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-ui-saem.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ rxPermissive({
expect_equal(c(200, 60, 1.5, 0.75, 1, NA), m1$saem.init.theta)
expect_equal(c("lCl", "AllomCL", "lVc", "AllomV", "lKA"), m1$saem.theta.name)
})

}, cran=TRUE)

0 comments on commit 6308f49

Please sign in to comment.