From 3f177b1624d7b26535489ad8afb457696de60e31 Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Tue, 11 Apr 2023 15:52:14 -0400 Subject: [PATCH 1/2] switch extract_eq to knit_print --- DESCRIPTION | 3 +- NAMESPACE | 8 +- NEWS.md | 4 +- R/{extract_eq.R => knit_printEquation.R} | 99 +++++-------------- man/knit_print.nlmixr2FitCore.Rd | 23 +++++ man/reexports.Rd | 6 +- ...extract_eq.R => test-knit_printEquation.R} | 14 +-- 7 files changed, 62 insertions(+), 95 deletions(-) rename R/{extract_eq.R => knit_printEquation.R} (72%) create mode 100644 man/knit_print.nlmixr2FitCore.Rd rename tests/testthat/{test-extract_eq.R => test-knit_printEquation.R} (97%) diff --git a/DESCRIPTION b/DESCRIPTION index a0ea04a..0e85bca 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,9 +31,9 @@ Imports: crayon, data.table, digest, - equatiomatic, ggplot2, ggtext, + knitr, lotri, methods, nlme, @@ -45,7 +45,6 @@ Imports: utils Suggests: brms, - knitr, nlmixr2data, testthat (>= 3.0.0), withr, diff --git a/NAMESPACE b/NAMESPACE index e013be9..c7b34a7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,8 +1,8 @@ # Generated by roxygen2: do not edit by hand S3method(bootplot,nlmixr2FitCore) -S3method(extract_eq,nlmixr2FitCore) -S3method(extract_eq,rxUi) +S3method(knit_print,nlmixr2FitCore) +S3method(knit_print,rxUi) S3method(print,nlmixr2BoostrapSummary) export(adaptivelassoCoefficients) export(addCatCovariates) @@ -14,11 +14,11 @@ export(bootstrapFit) export(buildcovInfo) export(buildupatedUI) export(covarSearchAuto) -export(extract_eq) export(foldgen) export(forwardSearch) export(horseshoeSummardf) export(ini) +export(knit_print) export(lassoCoefficients) export(lassoSummardf) export(model) @@ -32,8 +32,8 @@ export(regularmodel) import(lotri) import(utils) importFrom(Rcpp,evalCpp) -importFrom(equatiomatic,extract_eq) importFrom(ggplot2,.data) +importFrom(knitr,knit_print) importFrom(nlmixr2est,nlmixr) importFrom(nlmixr2est,nlmixr2) importFrom(nlmixr2est,nlmixrWithTiming) diff --git a/NEWS.md b/NEWS.md index 67d7327..c0a9ef8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,7 @@ # nlmixr2extra 2.0.9 -* New function `extract_eq()` will generate model equations for LaTeX reporting - automatically. +* New method for `knit_print()` will generate model equations for LaTeX + reporting automatically. # nlmixr2extra 2.0.8 diff --git a/R/extract_eq.R b/R/knit_printEquation.R similarity index 72% rename from R/extract_eq.R rename to R/knit_printEquation.R index a0398a3..88bfbe7 100644 --- a/R/extract_eq.R +++ b/R/knit_printEquation.R @@ -1,90 +1,34 @@ -#' @importFrom equatiomatic extract_eq +#' @importFrom knitr knit_print #' @export -equatiomatic::extract_eq +knitr::knit_print #' Extract the equations from an nlmixr2/rxode2 model to produce a 'LaTeX' #' equation. #' -#' @param model The model to extract equations from -#' @param intercept,greek,greek_colors,subscript_colors,var_colors,var_subscript_colors,raw_tex,swap_var_names,swap_subscript_names,ital_vars,label,index_factors,show_distribution,wrap,terms_per_line,operator_location,align_env,use_coefs,coef_digits,fix_signs,font_size,mean_separate,return_variances,se_subscripts Ignored +#' @param x The model to extract equations from +#' @param ... Ignored +#' @param output The type of output to request (currently, just "equations") #' @export -extract_eq.nlmixr2FitCore <- function(model, intercept = "alpha", greek = "beta", - greek_colors = NULL, subscript_colors = NULL, - var_colors = NULL, var_subscript_colors = NULL, - raw_tex = FALSE, - swap_var_names = NULL, swap_subscript_names = NULL, - ital_vars = FALSE, label = NULL, - index_factors = FALSE, show_distribution = FALSE, - wrap = FALSE, terms_per_line = 4, - operator_location = "end", align_env = "aligned", - use_coefs = FALSE, coef_digits = 2, - fix_signs = TRUE, font_size = NULL, - mean_separate, return_variances = FALSE, - se_subscripts = FALSE, ...) { - stopifnot(identical(intercept, "alpha")) - stopifnot(identical(greek, "beta")) - stopifnot(is.null(greek_colors)) - stopifnot(is.null(subscript_colors)) - stopifnot(is.null(var_colors)) - stopifnot(is.null(var_subscript_colors)) - stopifnot(identical(raw_tex, FALSE)) - stopifnot(is.null(swap_var_names)) - stopifnot(is.null(swap_subscript_names)) - stopifnot(identical(ital_vars, FALSE)) - stopifnot(is.null(label)) - stopifnot(identical(index_factors, FALSE)) - stopifnot(identical(show_distribution, FALSE)) - stopifnot(identical(wrap, FALSE)) - stopifnot(identical(terms_per_line, 4)) - stopifnot(identical(operator_location, "end")) - stopifnot(identical(align_env, "aligned")) - stopifnot(identical(use_coefs, FALSE)) - stopifnot(identical(coef_digits, 2)) - stopifnot(identical(fix_signs, TRUE)) - stopifnot(is.null(font_size)) - stopifnot(missing(mean_separate)) - stopifnot(identical(return_variances, FALSE)) - stopifnot(identical(se_subscripts, FALSE)) - ret <- - paste0( - "\\begin{align*}\n", - paste( - extractEqHelper(x = model, ...), - collapse = " \\\\\n" - ), - "\n\\end{align*}\n" - ) +knit_print.nlmixr2FitCore <- function(x, ..., output = "equations") { + output <- match.arg(output) + if ("equations" %in% output) { + ret <- + paste0( + "\\begin{align*}\n", + paste( + extractEqHelper(x = x, ...), + collapse = " \\\\\n" + ), + "\n\\end{align*}\n" + ) + } knitr::asis_output(ret) } -#' @rdname extract_eq.nlmixr2FitCore +#' @rdname knit_print.nlmixr2FitCore #' @export -extract_eq.rxUi <- function(model, intercept = "alpha", greek = "beta", - greek_colors = NULL, subscript_colors = NULL, - var_colors = NULL, var_subscript_colors = NULL, - raw_tex = FALSE, - swap_var_names = NULL, swap_subscript_names = NULL, - ital_vars = FALSE, label = NULL, - index_factors = FALSE, show_distribution = FALSE, - wrap = FALSE, terms_per_line = 4, - operator_location = "end", align_env = "aligned", - use_coefs = FALSE, coef_digits = 2, - fix_signs = TRUE, font_size = NULL, - mean_separate, return_variances = FALSE, - se_subscripts = FALSE, ...) { - extract_eq.nlmixr2FitCore(model, intercept = intercept, greek = greek, - greek_colors = greek_colors, subscript_colors = subscript_colors, - var_colors = var_colors, var_subscript_colors = var_colors, - raw_tex = raw_tex, - swap_var_names = swap_var_names, swap_subscript_names = swap_subscript_names, - ital_vars = ital_vars, label = label, - index_factors = index_factors, show_distribution = show_distribution, - wrap = wrap, terms_per_line = terms_per_line, - operator_location = operator_location, align_env = align_env, - use_coefs = use_coefs, coef_digits = coef_digits, - fix_signs = fix_signs, font_size = font_size, - mean_separate = mean_separate, return_variances = return_variances, - se_subscripts = se_subscripts, ...) +knit_print.rxUi <- function(x, ...) { + knit_print.nlmixr2FitCore(x, ...) } extractEqHelper <- function(x, ..., inModel) { @@ -456,6 +400,7 @@ extractEqHelper.default <- function(x, ..., inModel) { # hand side must be named 'value'. ret <- extractEqHelperAssign(x, ..., inModel = inModel) } else { + browser() stop("cannot handle class, please report a bug: ", class(x)[1]) # nocov } ret diff --git a/man/knit_print.nlmixr2FitCore.Rd b/man/knit_print.nlmixr2FitCore.Rd new file mode 100644 index 0000000..e2cb043 --- /dev/null +++ b/man/knit_print.nlmixr2FitCore.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/knit_printEquation.R +\name{knit_print.nlmixr2FitCore} +\alias{knit_print.nlmixr2FitCore} +\alias{knit_print.rxUi} +\title{Extract the equations from an nlmixr2/rxode2 model to produce a 'LaTeX' +equation.} +\usage{ +\method{knit_print}{nlmixr2FitCore}(x, ..., output = "equations") + +\method{knit_print}{rxUi}(x, ...) +} +\arguments{ +\item{x}{The model to extract equations from} + +\item{...}{Ignored} + +\item{output}{The type of output to request (currently, just "equations")} +} +\description{ +Extract the equations from an nlmixr2/rxode2 model to produce a 'LaTeX' +equation. +} diff --git a/man/reexports.Rd b/man/reexports.Rd index 91782ea..78e68d7 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -1,9 +1,9 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/extract_eq.R, R/reexport.R +% Please edit documentation in R/knit_printEquation.R, R/reexport.R \docType{import} \name{reexports} \alias{reexports} -\alias{extract_eq} +\alias{knit_print} \alias{ini} \alias{model} \alias{nlmixrWithTiming} @@ -17,7 +17,7 @@ These objects are imported from other packages. Follow the links below to see their documentation. \describe{ - \item{equatiomatic}{\code{\link[equatiomatic]{extract_eq}}} + \item{knitr}{\code{\link[knitr]{knit_print}}} \item{nlmixr2est}{\code{\link[nlmixr2est:nlmixr2]{nlmixr}}, \code{\link[nlmixr2est]{nlmixr2}}, \code{\link[nlmixr2est]{nlmixrWithTiming}}, \code{\link[nlmixr2est]{setCov}}} diff --git a/tests/testthat/test-extract_eq.R b/tests/testthat/test-knit_printEquation.R similarity index 97% rename from tests/testthat/test-extract_eq.R rename to tests/testthat/test-knit_printEquation.R index 7244e65..81e59e2 100644 --- a/tests/testthat/test-extract_eq.R +++ b/tests/testthat/test-knit_printEquation.R @@ -1,4 +1,4 @@ -test_that("extract_eq, simple version", { +test_that("knit_print, simple version", { mod <- function() { ini({ lka <- 0.45 @@ -17,7 +17,7 @@ test_that("extract_eq, simple version", { } ui <- rxode2::rxode(mod) expect_equal( - extract_eq(ui), + knit_print(ui), knitr::asis_output("\\begin{align*}\nka & = \\exp\\left(lka\\right) \\\\\ncl & = \\exp\\left(lcl\\right) \\\\\nvc & = \\exp\\left(lvc\\right) \\\\\ncp & = linCmt() \\\\\ncp & \\sim prop(propSd)\n\\end{align*}\n") ) @@ -25,12 +25,12 @@ test_that("extract_eq, simple version", { fit <- nlmixr2est::nlmixr(mod, data = nlmixr2data::theo_sd, est = "focei", control = nlmixr2est::foceiControl(eval.max = 1, print = 0)) ) expect_equal( - extract_eq(fit), + knit_print(fit), knitr::asis_output("\\begin{align*}\nka & = \\exp\\left(lka\\right) \\\\\ncl & = \\exp\\left(lcl\\right) \\\\\nvc & = \\exp\\left(lvc\\right) \\\\\ncp & = linCmt() \\\\\ncp & \\sim prop(propSd)\n\\end{align*}\n") ) }) -test_that("extract_eq, less common models", { +test_that("knit_print, less common models", { mod <- function() { ini({ lka <- 0.45 @@ -65,12 +65,12 @@ test_that("extract_eq, less common models", { } ui <- rxode2::rxode(mod) expect_equal( - extract_eq(ui), + knit_print(ui), knitr::asis_output("\\begin{align*}\nka & = \\exp\\left(lka\\right)<1 \\\\\ncl & = \\exp\\left(lcl\\right)\\leq2 \\\\\nvc & = \\exp\\left(lvc\\right)\\equiv3 \\\\\nvc4 & = vc\\geq4 \\\\\nvc5 & = vc>5 \\\\\nvc6 & = vc\\land6 \\\\\nvc7 & = vc\\land7 \\\\\nvc8 & = vc\\lor8 \\\\\nvc9 & = vc\\lor9 \\\\\nvc10 & = vc\\ne10 \\\\\nvc11 & = \\lnot vc \\\\\n\\mathrm{if} & \\left(vc>11\\right) \\{ \\\\\n & cl = 12 \\\\\n\\} \\quad & \\mathrm{else} \\: \\mathrm{if} \\left(vc>13\\right) \\{ \\\\\n & cl = 14 \\\\\n & cl = 15 \\\\\n\\} \\quad & \\mathrm{else} \\: cl = 16 \\\\\ncp & = linCmt() \\\\\ncp & \\sim c(p0=0, p1=1, p2=2, 3)\n\\end{align*}\n") ) }) -test_that("extract_eq, model with 'if' and a character string", { +test_that("knit_print, model with 'if' and a character string", { mod <- function() { ini({ lka <- 0.45 @@ -94,7 +94,7 @@ test_that("extract_eq, model with 'if' and a character string", { } ui <- rxode2::rxode(mod) expect_equal( - extract_eq(ui), + knit_print(ui), knitr::asis_output("\\begin{align*}\nka & = \\exp\\left(lka\\right)<1 \\\\\ncl & = \\exp\\left(lcl\\right)\\leq2 \\\\\nvc & = \\exp\\left(lvc\\right)\\equiv3 \\\\\n\\mathrm{if} & \\left(vc\\equiv\\text{\"a\"}\\right) \\{ \\\\\n & cl = 12 \\\\\n\\} \\quad & \\mathrm{else} \\: cl = 16 \\\\\ncp & = linCmt() \\\\\ncp & \\sim c(p0=0, p1=1, p2=2, 3)\n\\end{align*}\n") ) }) From 67ca0f703ca20f86a565ae758bb3a0b41101acba Mon Sep 17 00:00:00 2001 From: Bill Denney Date: Tue, 11 Apr 2023 16:07:24 -0400 Subject: [PATCH 2/2] Fix codefactor issue --- R/knit_printEquation.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/knit_printEquation.R b/R/knit_printEquation.R index 88bfbe7..2832ad2 100644 --- a/R/knit_printEquation.R +++ b/R/knit_printEquation.R @@ -400,7 +400,6 @@ extractEqHelper.default <- function(x, ..., inModel) { # hand side must be named 'value'. ret <- extractEqHelperAssign(x, ..., inModel = inModel) } else { - browser() stop("cannot handle class, please report a bug: ", class(x)[1]) # nocov } ret