在 Shiny 中从 renderUI 生成的动态文本的格式化颜色

Formatting colour of dynamic text generated from renderUI in Shiny

我希望为来自渲染的动态生成的文本着色UI。 我的代码的最小摘录在这里:

library(shiny)
library(Ryacas)
library(Ryacas0)
library(mathjaxr) # for rendering Latex expressions in Shiny

# Define UI for application that draws a histogram
ui <- fluidPage(
  withMathJax(uiOutput("entered1")),
  withMathJax(uiOutput("entered2"))
)

server <- function(input, output) {
  output$entered1 = renderUI({
    withMathJax(
      helpText(
        paste0("f(x) = \(", yac_str(paste0("TeXForm(Sin(x))")), "\)")
      )
    )
  })
  
  output$entered2 = renderUI({
    withMathJax(
      helpText(
        paste0("f(x) = \(", yac_str(paste0("TeXForm(Cos(x))")), "\)")
      )
    )
  })
  
}
  # Run the application 
  shinyApp(ui = ui, server = server)

在完整代码中,函数 'sin(x)' 和 'cos(x)' 是动态变化的,但在这个帮助请求中,它们是静态的。

我希望'f(x) = sin(x)'以红色文本显示,'f(x) = cos(x)'以蓝色文本显示。

如果可以的话,我不想使用 CSS。我想知道在我的代码中将 'style = color:red' (等)标签放在哪里,或者任何等效的标签。我到处寻找简单的解释,但没有发现任何可以轻松使用的东西。

不知道是要在UI里面放一些标签,还是要在服务器端包含样式信息。我查看了 renderUI 的 outputArgs 语法,但几乎没有像样的示例可以帮助我。

我欢迎任何人的帮助。

您可以使用 MathJax 命令\color:

library(shiny)
library(Ryacas)

ui <- fluidPage(
  helpText(withMathJax(uiOutput("entered1", inline = TRUE))),
  helpText(withMathJax(uiOutput("entered2", inline = TRUE)))
)

server <- function(input, output) {
  output$entered1 = renderUI({
    paste0("\( \color{red}{f(x) = ", yac_str("TeXForm(Sin(x))"), "}\)")
  })
  
  output$entered2 = renderUI({
    paste0("\( \color{blue}{f(x) = ", yac_str("TeXForm(Cos(x))"), "}\)")
  })
  
}
# Run the application 
shinyApp(ui = ui, server = server)