【问题标题】:LaTeX formula in Shiny panel闪亮面板中的 LaTeX 公式
【发布时间】:2015-07-22 01:00:23
【问题描述】:

我想在 Shiny 面板中显示 -LaTeX 格式化公式,但我找不到将 textOutputwithMathJax 结合使用的方法。我尝试了以下但没有奏效。任何帮助将不胜感激。

--ui.r

...
    tabPanel("Diagnostics", h4(textOutput("diagTitle")),
withMathJax(textOutput("formula")),
),
...

--server.r

...
output$formula <- renderText({
    print(paste0("Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", my_calculated_value,"$$"))
})
...

【问题讨论】:

  • 奇怪,它在我的机器上运行良好(我刚刚删除了print,但应该不会有太大变化)。不过我有shiny_0.11.1。此脚本文件:&lt;script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"... 是否已加载到您的页面上?看起来该链接已修复,所以也许这就是问题
  • 是的,在页面源代码中我看到它已加载。 src="cdn.mathjax.org/mathjax/latest/…" type="text/javascript"

标签: r shiny mathjax


【解决方案1】:

在 UI 端使用 uiOutput,在服务器端使用 renderUI 获取动态内容。

ui <- fluidPage(
  withMathJax(),
  tabPanel(
    title = "Diagnostics", 
    h4(textOutput("diagTitle")),
    uiOutput("formula")
  )
)

server <- function(input, output, session){
  output$formula <- renderUI({
    my_calculated_value <- 5
    withMathJax(paste0("Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", my_calculated_value,"$$"))
  })
}

shinyApp(ui, server)

更多示例:http://shiny.leg.ufpr.br/daniel/019-mathjax/

【讨论】:

  • TeX 需要 Internet 环境。
【解决方案2】:

ui.R

tabPanel("Diagnostics", h4(textOutput("diagTitle")),
    withMathJax(uiOutput("formula")),
)

服务器.R

output$formula <- renderUI({
    return(HTML(paste0("<p>", "Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", my_calculated_value,"$$","</p>")))
})

【讨论】:

  • 输出还是这样:使用这个公式:$$\hat{A}_{\small{\textrm{M€}}} =1.69$$
【解决方案3】:

renderPrint()怎么样?

最小的工作示例:

library(shiny)

server <- function(input, output, session) {

 output$formula <- renderPrint({
     print(paste0("Use this formula: $$\\hat{A}_{\\small{\\textrm{M€}}} =", 1,"$$"))
})

}

ui <- fluidPage(


  titlePanel("Hello Shiny!"),


  sidebarLayout(
    sidebarPanel(

    ),

    mainPanel(
      withMathJax(textOutput("formula"))
    )
    )
)

shinyApp(ui = ui, server = server)

编辑: 对我来说,它看起来像这样:

【讨论】:

  • 对我不起作用(R 版本 3.1.1,平台:i386-w64-mingw32/i386(32 位),shiny_0.10.2.2)
  • \hat{A} 应该如下所示: Â
猜你喜欢
  • 2017-04-29
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多