乳胶方程无法在闪亮中正确呈现
Latex equation not rendering correctly in shiny
我想在闪亮的应用程序中显示乳胶方程。我看到了一个例子,其中 mathjax 用于显示乳胶方程。但由于某种原因,从 equatiomatic
包生成的方程无法正确显示。以下是一个可重现的例子:
library(shiny)
library(tidyverse)
library(broom)
library(equatiomatic)
## Loading Dataset
data(mpg)
mpg2 <- mpg %>%
select(where(is.numeric))
## User Interface
ui <- fluidPage(
withMathJax(),
sidebarPanel(
selectInput(inputId = "xcol",
label = "Select an explanatory variable",
choices = colnames(mpg2),
selected = "cty"),
selectInput(inputId = "ycol",
label = "Select a response variable",
choices = colnames(mpg2),
selected = "hwy")
),
mainPanel(
plotOutput('plot'),
br(),
uiOutput('eq'),
br(),
tableOutput('table1'),
br(),
tableOutput('table2')
)
)
## Server
server <- function(input, output, session) {
data <- reactive({
mpg2 %>%
select(col1 = input$xcol, col2 = input$ycol)
})
output$plot <- renderPlot({
ggplot(data()) +
geom_point(aes(col1, col2)) +
labs(x = input$xcol, y = input$ycol)
})
output$table1 <- renderTable({
m1 <- lm(reformulate(input$xcol, input$ycol), data = mpg2)
broom::tidy(m1)
})
output$table2 <- renderTable({
m1 <- lm(reformulate(input$xcol, input$ycol), data = mpg2)
broom::glance(m1)[, 1:2]
})
output$eq <- renderUI({
m1 <- lm(reformulate(input$xcol, input$ycol), data = mpg2)
withMathJax(helpText(extract_eq(m1)))
})
}
shinyApp(ui, server)
有趣的是,shiny 似乎期望方程式是原始字符串。因此,它缺少所有 Latex 运算符的 \
。
一种(不是很漂亮)的修复方法是:
output$eq <- renderUI({
m1 <- lm(reformulate(input$xcol, input$ycol), data = mpg2)
withMathJax(helpText(
paste0("$$", as.character(extract_eq(m1)), "$$")
)
)
})
但是我不太熟悉这个包,所以不确定这是有意为之还是错误...您是否考虑过在 equatiomatic GitHub 上提出问题?
我想在闪亮的应用程序中显示乳胶方程。我看到了一个例子,其中 mathjax 用于显示乳胶方程。但由于某种原因,从 equatiomatic
包生成的方程无法正确显示。以下是一个可重现的例子:
library(shiny)
library(tidyverse)
library(broom)
library(equatiomatic)
## Loading Dataset
data(mpg)
mpg2 <- mpg %>%
select(where(is.numeric))
## User Interface
ui <- fluidPage(
withMathJax(),
sidebarPanel(
selectInput(inputId = "xcol",
label = "Select an explanatory variable",
choices = colnames(mpg2),
selected = "cty"),
selectInput(inputId = "ycol",
label = "Select a response variable",
choices = colnames(mpg2),
selected = "hwy")
),
mainPanel(
plotOutput('plot'),
br(),
uiOutput('eq'),
br(),
tableOutput('table1'),
br(),
tableOutput('table2')
)
)
## Server
server <- function(input, output, session) {
data <- reactive({
mpg2 %>%
select(col1 = input$xcol, col2 = input$ycol)
})
output$plot <- renderPlot({
ggplot(data()) +
geom_point(aes(col1, col2)) +
labs(x = input$xcol, y = input$ycol)
})
output$table1 <- renderTable({
m1 <- lm(reformulate(input$xcol, input$ycol), data = mpg2)
broom::tidy(m1)
})
output$table2 <- renderTable({
m1 <- lm(reformulate(input$xcol, input$ycol), data = mpg2)
broom::glance(m1)[, 1:2]
})
output$eq <- renderUI({
m1 <- lm(reformulate(input$xcol, input$ycol), data = mpg2)
withMathJax(helpText(extract_eq(m1)))
})
}
shinyApp(ui, server)
有趣的是,shiny 似乎期望方程式是原始字符串。因此,它缺少所有 Latex 运算符的 \
。
一种(不是很漂亮)的修复方法是:
output$eq <- renderUI({
m1 <- lm(reformulate(input$xcol, input$ycol), data = mpg2)
withMathJax(helpText(
paste0("$$", as.character(extract_eq(m1)), "$$")
)
)
})
但是我不太熟悉这个包,所以不确定这是有意为之还是错误...您是否考虑过在 equatiomatic GitHub 上提出问题?