Steven Ouellette
Steven Ouellette

Reputation: 131

Is there a way to get mathematical notation in a shiny input widget label?

I would like to get mathematical notation into the label for an input widget. Let's say I would like x-bar to be the label for a numeric input. I have tried bquote and expression, and neither seems to work.

label=expression(bar(x) just shows "bar(x)" as the label.

label=bquote(bar(x)) throws an error about it needing to be length-one character vector.

Is there a way to do this? Thanks!

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Math Notation"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            numericInput("input",
                        label = expression(bar(x)),
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
}

# Run the application 
shinyApp(ui = ui, server = server)

Upvotes: 3

Views: 684

Answers (1)

ViviG
ViviG

Reputation: 1726

You can use the function withMathJax to achieve that by changing your label to label = withMathJax("$$\\bar{x}$$")

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Math Notation"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            numericInput("input",
                        label = withMathJax("$$\\bar{x}$$"),
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
}

# Run the application 
shinyApp(ui = ui, server = server)

More about the function in Shiny here

enter image description here

Upvotes: 2

Related Questions