Математический режим в блестящем столе - PullRequest
0 голосов
/ 19 февраля 2019

Используя withMathJax, я хотел бы отобразить таблицу с именами строк с некоторыми математическими выражениями.Вот базовый пример:

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)

К сожалению, это не работает.Я также попробовал:

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

и

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

, но безуспешно.В последнем случае я правильно выполнил альфа и бета, но также с <script>if (window.MathJax) MathJax.Hub.Queue(["Typeset", MathJax.Hub]);</script>

РЕДАКТИРОВАТЬ:

Подход должен работать предпочтительно даже в случае, когда таблица перерисовывается.Используя предложение @ Stéphane Laurent, я обновил код:

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)

1 Ответ

0 голосов
/ 19 февраля 2019

Вы можете использовать xtable для создания таблицы LaTeX:

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)

enter image description here


Если вы не хотите использоватьxtable, вы можете сделать:

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)

enter image description here


РЕДАКТИРОВАТЬ

Как отмечено ФП, это нене работает, когда таблица перерисована.Вот рабочее решение:

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.

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)

enter image description here

Каждый случай, например %%string%%, заменяется на \\string, а затем отображается в математике..

...