[英]Latex equation not rendering correctly in shiny
我想在 shiny 應用程序中顯示 latex 方程。 我看到一個例子,其中 mathjax 用於顯示 latex 方程。 但由於某種原因,從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)), "$$")
)
)
})
但是我對 package 不太熟悉,所以不確定這是有意的還是錯誤的……您是否考慮過在等式 GitHub 上打開問題?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.