See a working application here
The equatiomatic package exports renderEq()
and eqOutput()
functions to make it’s usage with shiny more straightforward.
Imagine, for example, that we wanted to have an app that showed us the coefficients of a model in a nice table. We could do that with something like
library(equatiomatic)
library(shiny)
library(shinyWidgets)
library(gtsummary)
library(gt)
<- fluidPage(
ui titlePanel("equatiomatic w/Shiny"),
sidebarLayout(
sidebarPanel(
multiInput(
inputId = "xvars", label = "Select predictor variables :",
choices = names(penguins)[-3],
selected = "island"
)
),mainPanel(
gt_output("tbl")
)
)
)
<- function(input, output) {
server <- reactive({
model <- paste("bill_length_mm ~ ", paste(input$xvars, collapse = " + "))
form lm(as.formula(form), penguins)
})
$tbl <- render_gt({
outputas_gt(tbl_regression(model()))
})
}
shinyApp(ui = ui, server = server)
But, naturally, we’d probably also want the equation rendered too. We can extend The above with equatiomatic as follows.
library(equatiomatic)
library(shiny)
library(shinyWidgets)
library(gtsummary)
library(gt)
<- fluidPage(
ui titlePanel("equatiomatic w/Shiny"),
withMathJax(), # Initialize mathJax so the equation renders properly
sidebarLayout(
sidebarPanel(
multiInput(
inputId = "xvars", label = "Select predictor variables :",
choices = names(penguins)[-3],
selected = "island"
)
),mainPanel(
eqOutput("equation"),
gt_output("tbl")
)
)
)
<- function(input, output) {
server <- reactive({
model <- paste("bill_length_mm ~ ", paste(input$xvars, collapse = " + "))
form lm(as.formula(form), penguins)
})
$equation <- renderEq(
outputextract_eq(model())
)
$tbl <- render_gt({
outputas_gt(tbl_regression(model()))
})
}
shinyApp(ui = ui, server = server)
Note that, beyond the renderEq()
and eqOutput()
function, we also have to initialize mathJax with the shiny::withMathJax()
function somewhere in the UI. Without this line, you’ll get the text version of the equation.
Finally, we can of course add additional arguments extract_eq()
, and it all works as expected.
library(equatiomatic)
library(shiny)
library(shinyWidgets)
library(gtsummary)
library(gt)
<- fluidPage(
ui titlePanel("equatiomatic w/Shiny"),
withMathJax(),
sidebarLayout(
sidebarPanel(
multiInput(
inputId = "xvars",
label = "Select predictor variables :",
choices = names(penguins)[-3],
selected = "island"
)
),mainPanel(
eqOutput("eq2"),
eqOutput("equation"),
gt_output("tbl")
)
)
)
<- function(input, output) {
server <- reactive({
model <- paste("bill_length_mm ~ ", paste(input$xvars, collapse = " + "))
form lm(as.formula(form), penguins)
})
$equation <- renderEq(
outputextract_eq(
model(),
wrap = TRUE,
terms_per_line = 2,
use_coefs = TRUE,
font_size = "Huge"
)
)
$tbl <- render_gt({
outputas_gt(tbl_regression(model()))
})
}
shinyApp(ui = ui, server = server)