闪亮表中的数学模式 [英] Math mode in shiny table

查看:69
本文介绍了闪亮表中的数学模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用withMathJax,我想使用具有一些数学表达式的行名来呈现表.这是一个基本示例:

Using withMathJax, I would like to render a table with rownames with some math expressions. Here is a basic example:

library(shiny)

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  mainPanel(withMathJax(),
  tableOutput(outputId = "table"))
)

server <- function(input, output) {

  output$table <- renderTable({
    x <- rnorm(2)
    y <- rnorm(2, 1)
    tab <- data.frame(x = x, y = y)
    withMathJax()
    rownames(tab) <- c("\\(\\alpha\\)", 
                      "\\(\\beta\\)")

    tab
  },
  include.rownames = T,
  include.colnames = T)

}

shinyApp(ui, server)

不幸的是,这不起作用.我也尝试过:

This unfortunately does not work. I also tried:

rownames(tab) <- c(withMathJax("\\(\\alpha\\)"), 
                   withMathJax("\\(\\beta\\)"))

rownames(tab) <- c(paste(withMathJax("\\(\\alpha\\)")), 
                   paste(withMathJax("\\(\\beta\\)")))

,但没有成功.在后一种情况下,我可以正确渲染alpha和beta,但是 <script>if (window.MathJax) MathJax.Hub.Queue(["Typeset", MathJax.Hub]);</script>

but without any success. In latter case I got alpha and beta correctly rendered, however with also <script>if (window.MathJax) MathJax.Hub.Queue(["Typeset", MathJax.Hub]);</script>

即使重新渲染表,该方法也应该最好工作.使用@StéphaneLaurent的建议,我更新了代码:

The approach should preferably work even in case when table is re-rendered. Using suggestion by @Stéphane Laurent, I updated the code:

library(shiny)

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  mainPanel(
    numericInput("mean", label = "mean", value = 1),
    withMathJax(tableOutput("table"))
  )
)

server <- function(input, output) {

  output$table <- renderTable({
    x <- rnorm(2)
    y <- rnorm(2, input$mean)
    tab <- data.frame(x = x, y = y)
    rownames(tab) <- c("\\(\\alpha\\)", 
                       "\\(\\beta\\)")
    tab
  },
  include.rownames = TRUE,
  include.colnames = TRUE)

}

shinyApp(ui, server)

推荐答案

您可以使用xtable生成LaTeX表:

You can use xtable to generate a LaTeX table:

library(shiny)
library(xtable)

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  mainPanel(
    uiOutput("table")
  )
)

server <- function(input, output) {

  output$table <- renderUI({
    x <- rnorm(2)
    y <- rnorm(2, 1)
    tab <- data.frame(x = x, y = y)
    rownames(tab) <- c("\\alpha", 
                       "\\beta")
    LaTeXtab <- print(xtable(tab, align=rep("c", ncol(tab)+1)), 
                      floating=FALSE, tabular.environment="array", comment=FALSE, 
                      print.results=FALSE, 
                      sanitize.rownames.function = function(x) x)
    tagList(
      withMathJax(),
      HTML(paste0("$$", LaTeXtab, "$$"))
    )
  })

}

shinyApp(ui, server)

如果您不想使用xtable,则可以执行以下操作:

If you don't want to use xtable, you can do:

library(shiny)

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  mainPanel(
    withMathJax(tableOutput("table"))
  )
)

server <- function(input, output) {

  output$table <- renderTable({
    x <- rnorm(2)
    y <- rnorm(2, 1)
    tab <- data.frame(x = x, y = y)
    rownames(tab) <- c("\\(\\alpha\\)", 
                       "\\(\\beta\\)")
    tab
  },
  include.rownames = TRUE,
  include.colnames = TRUE)

}

shinyApp(ui, server)

如OP所述,当重新渲染表时,此功能不起作用.这是一个可行的解决方案:

As noted by the OP, this doesn't work when the table is re-rendered. Here is a working solution:

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  mainPanel(
    numericInput("mean", label = "mean", value = 1),
    uiOutput("tableUI")
  )
)

server <- function(input, output) {

  output$table <- renderTable({
    x <- rnorm(2)
    y <- rnorm(2, input$mean)
    tab <- data.frame(x = x, y = y)
    rownames(tab) <- c("\\(\\alpha\\)", 
                       "\\(\\beta\\)")
    tab
  },
  include.rownames = TRUE,
  include.colnames = TRUE)

  output$tableUI <- renderUI({
    input$mean # in order to re-render when input$mean changes
    tagList(
      withMathJax(),
      withMathJax(tableOutput("table"))
    )
  })

}


编辑2

先前的解决方案有效,但有一些跳跃,并且不方便,因为它需要在renderUI中包括反应性依赖项.以下是使用 katex 代替MathJax的解决方案.没有跳跃,也没有renderUI.


EDIT 2

The previous solution works but there are some jumps, and it is not convenient because it requires to include the reactive dependencies in the renderUI. Below is a solution which uses katex instead of MathJax. No jumps, and no renderUI.

library(shiny)

js <- " 
$(document).on('shiny:value', function(event) {
  if(event.name === 'table'){
    var matches = event.value.match(/(%%+[^%]+%%)/g);
    var newvalue = event.value;
    for(var i=0; i<matches.length; i++){
      var code = '\\\\' + matches[i].slice(2,-2);
      newvalue = newvalue.replace(matches[i], katex.renderToString(code));
    }
    event.value = newvalue;
  }
})
" 

ui <- fluidPage(
  tags$head(
    tags$link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css", integrity="sha384-9tPv11A+glH/on/wEu99NVwDPwkMQESOocs/ZGXPoIiLE8MU/qkqUcZ3zzL+6DuH", crossorigin="anonymous"),
    tags$script(src="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.js", integrity="sha384-U8Vrjwb8fuHMt6ewaCy8uqeUXv4oitYACKdB0VziCerzt011iQ/0TqlSlv8MReCm", crossorigin="anonymous"),
    tags$script(HTML(js))
  ),
  titlePanel("Hello Shiny!"),
  mainPanel(
    numericInput("mean", "Enter mean", value = 1),
    tableOutput("table")
  )
)

server <- function(input, output) {

  output$table <- renderTable({
    x <- rnorm(2)
    y <- rnorm(2, input$mean)
    tab <- data.frame(x = x, y = y, z = c("hello", "%%gamma%%%%delta%%"))
    rownames(tab) <- c("%%alpha%%", "%%beta%%")
    tab
  }, rownames = TRUE)

}

shinyApp(ui, server)

%%string%%这样的所有出现都被\\string替换,然后用数学表示.

Every occurrence like %%string%% is replaced by \\string and then rendered in math.

这篇关于闪亮表中的数学模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆