DT::datatable 中的 LaTeX

LaTeX in DT::datatable

我在 Shiny 应用程序的模态中有一个 datatable,我想在一列中呈现一些 LaTeX。我该怎么办?

library(shiny)
library(DT)

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  br(),
  actionButton("open", "Open")
)

server <- function(input, output, session){  
  
  output[["mytable"]] <- renderDT({
    datatable(
      data.frame(
        A = c("$$\alpha+\beta$$", "$$\alpha+\gamma$$", "$$\alpha+\lambda$$"),
        B = c(111111, 3333333, 3123.233)
      ) 
    )
  })
  
  observeEvent(input[["open"]], {
    showModal(modalDialog(
      DTOutput("mytable")
    ))
  })
  
}

shinyApp(ui, server)

这是一个使用 KaTeX 的解决方案。也许 withMathjax 是可能的,但恕我直言 KaTeX 更好。

library(shiny)
library(DT)

renderKaTeX <- c(
  "function(data, type, row, meta){",
  "  if(type === 'display'){",
  "    data = katex.renderToString(data);",
  "  }",
  "  return data;",
  "}"
)


ui <- fluidPage(
  tags$head(
    tags$link(
      rel = "stylesheet", 
      href = "https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.css", 
      integrity = "sha384-MlJdn/WNKDGXveldHDdyRP1R4CTHr3FeuDNfhsLPYrq2t0UBkUdK2jyTnXPEK1NQ", 
      crossorigin = "anonymous"
    ),
    tags$script(
      defer = "", 
      src = "https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.js", 
      integrity = "sha384-VQ8d8WVFw0yHhCk5E8I86oOhv48xLpnDZx5T9GogA/Y84DcCKWXDmSDfn13bzFZY", 
      crossorigin = "anonymous"
    )
  ),
  titlePanel("Hello Shiny!"),
  br(),
  actionButton("open", "Open")
)

server <- function(input, output, session){  
  
  output[["mytable"]] <- renderDT({
    datatable(
      data.frame(
        A = c("\alpha+\beta", "\alpha+\gamma", "\alpha+\lambda"),
        B = c(111111, 3333333, 3123.233)
      ),
      options = list(
        columnDefs = list(
          list(targets = 1, render = JS(renderKaTeX))
        )
      )
    )
  })
  
  
  observeEvent(input[["open"]], {
    showModal(modalDialog(
      DTOutput("mytable")
    ))
  })
  
}

shinyApp(ui, server)