我正在尝试创建一个动态闪亮的应用程序,使用yacas计算机代数系统来处理输入的函数。作为第一步,我希望UI确认它所理解的输入内容。但是,下面的Shiny代码不会以Latex格式显示输入的函数。
library(shiny)
library(Ryacas) # for the TeXForm command
library(Ryacas0)
library(mathjaxr) # for rendering Latex expressions in Shiny
ui <- fluidPage(
sidebarPanel(
textInput(
inputId = "ui_function",
label = 'f(x) = ',
value = "x^2",
placeholder = "Enter function here"),
),
mainPanel(
uiOutput("entered")
)
)
server <- function(input, output) {
output$entered = renderUI({
withMathJax(
helpText(yac_str(paste0("TeXForm(",
input$ui_function,
")")
)
)
)
})
} # end server function
shinyApp(ui = ui, server = server)
当我从上面的代码中删除'withMathJax'命令时,它的行为完全相同,所以好像'withMathJax'命令对输出没有任何影响。
通过一个简单的例子,我正在寻找用户输入'x^2',它应该显示
x²
我欢迎任何人提供的任何帮助。
我在最新的RStudio 2022.02.1 Build 461上运行这个,带有R4.1.3, Shiny 1.7.1和MathJax 1.6-0
您可以这样做:
library(shiny)
library(Ryacas) # for the TeXForm command
ui <- fluidPage(
sidebarPanel(
textInput(
inputId = "ui_function",
label = 'f(x) = ',
value = "x^2",
placeholder = "Enter function here"),
),
mainPanel(
helpblock(withMathJax(uiOutput("entered", inline = TRUE)))
)
)
server <- function(input, output) {
output$entered = renderUI({
paste0(
"\(",
yac_str(paste0("TeXForm(", input$ui_function, ")")),
"\)"
)
})
} # end server function
shinyApp(ui = ui, server = server)
mathjaxr package不是用于Shiny的,它用于帮助文件(Rd)。
根据Stephane的建议,我重新查看了我的代码,现在这个版本可以正常工作了:
library(shiny)
library(Ryacas)
library(Ryacas0)
library(mathjaxr) # for rendering Latex expressions in Shiny
ui <- fluidPage(
sidebarPanel(
textInput(
inputId = "ui_function",
label = 'f(x) = ',
value = "x^2",
placeholder = "Enter function here"),
),
mainPanel(
withMathJax(uiOutput("entered"))
)
)
server <- function(input, output) {
output$entered = renderUI({
withMathJax(
helpText(
paste0(
"\(",
yac_str(paste0("TeXForm(", input$ui_function, ")")),
"\)"
)
)
)
})
} # end server function
shinyApp(ui = ui, server = server)
在ui的mainPanel中包含withMathJax
似乎会产生差异。似乎连接到服务器内部字符串的"\("
字符串对其成功至关重要。