Hi,
If you look at render_gt from gt, or renderText, and renderTable from shiny you can see a general pattern with
installExprFunction to install expr as a function in the env and then
createRenderFunction to process the value
In your example, everything is already processed thanks to withMathJax(helpText(equatiomatic:::format.equation(x)))
So if you use dedicated functions installExprFunction and createRenderFunction and combine it with the transform you use you can create your own rendering function,
library(shiny)
library(shinyWidgets)
library(ggplot2)
library(equatiomatic)
library(gtsummary)
library(gt)
renderEq <- function(expr, env = parent.frame(), quoted = FALSE, outputArgs = list()) {
shiny::installExprFunction(expr = expr, name = "func", eval.env = env, quoted = quoted)
shiny::createRenderFunction(func = func, function(value, session, name, ...) {
as.character(withMathJax(helpText(equatiomatic:::format.equation(x = value))))
}, eqOutput, outputArgs)
}
eqOutput <- function(outputId) {
shiny::htmlOutput(outputId = outputId)
}
ui <- fluidPage(
titlePanel("equatiomatic w/Shiny"),
sidebarLayout(
sidebarPanel(
multiInput(
inputId = "xvars", label = "Select predictor variables :",
choices = names(mpg)[-8],
selected = "displ"
)
),
mainPanel(
eqOutput("eq_new"),
uiOutput("eq"),
gt_output("tbl")
)
)
)
server <- function(input, output) {
model <- reactive({
form <- paste("hwy ~ ", paste(input$xvars, collapse = " + "))
lm(as.formula(form), mpg)
})
output$eq_new <- renderEq(expr = extract_eq(model(), wrap = TRUE, terms_per_line = 3))
output$eq <- renderUI(
withMathJax(
helpText(
equatiomatic:::format.equation(
extract_eq(model(), wrap = TRUE, terms_per_line = 3)
)
)
)
)
output$tbl <- render_gt({
as_gt(tbl_regression(model()))
})
}
shinyApp(ui = ui, server = server)
However, it still remains kind of a glitch where the equation is displayed but not correctly formatted ....