I am trying to create a dynamic Shiny app that uses the yacas Computer Algebra System to process entered functions. As a first step, I want the UI to confirm what it understands has been typed in. However, the following Shiny code is not displaying the entered function in Latex format.
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)
When I remove the 'withMathJax' commands from the above code, it behaves exactly the same way, so it's as if the 'withMathJax' command is not having any effect on the output.
By way of a simple example, I'm looking for the user to enter 'x^2' and it should displays
x²
I welcome any help that anyone can offer.
I'm running this on latest RStudio 2022.02.1 Build 461, with R4.1.3, Shiny 1.7.1 and MathJax 1.6-0
CodePudding user response:
You can do as follows:
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)
The mathjaxr package is not for Shiny, it is used for help files (Rd).
CodePudding user response:
Following on from Stephane's suggestion, I re-looked at my code and this version now works, as intended:
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)
The inclusion of withMathJax
inside mainPanel in the ui seemed to make the difference. It also seems that the "\\("
strings that are concatenated to the string inside the server are critical to its success.