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\)"))
and
rownames(tab) <- c(paste(withMathJax("\(\alpha\)")),
paste(withMathJax("\(\beta\)")))
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>
EDIT:
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)
See Question&Answers more detail:os