Is there any way to render HTML in shiny's validation messages? I tried different approaches using the HTML
wrapper, the tags$...
functions, as well as a separate htmlOutput
for the validation message, but could not get any of them to work. Here is a simple example app that shows this issue - the select
should be bold in the validation message but the HTML tags are escaped (contrived example, I know, but hopefully conveys the idea, I would primarily like to use this to include fa icons in the messages):
runApp(
list(
ui = fluidPage(
titlePanel("Validation App"),
sidebarLayout(
sidebarPanel(
selectInput("data", label = "Data set", choices = c("", "mtcars"))
),
mainPanel(tableOutput("table"))
)
),
server = function(input, output) {
data <- reactive({
# validate test
validate(
need(input$data != "", HTML("Please <strong>select</strong> a data set"))
)
get(input$data, 'package:datasets')
})
output$table <- renderTable(head(data()))
}
)
)
Every Shiny app is built on an HTML document that creates the apps' user interface. Usually, Shiny developers create this document by building the ui object with R functions that build HTML output.
An easy way to provide arguments to validate is to use the need function, which takes an expression and a string; if the expression is considered a failure, then the string will be used as the error message. The need function considers its expression to be a failure if it is any of the following: FALSE. NULL.
The simplest solution is to use a uiOutput
and inside the renderUI
function put an if
to validate the input. In the code below is an example using HTML
and tags$...
functions. You can can also put an icon.
library(shiny)
runApp(
list(
ui = fluidPage(
titlePanel("Validation App"),
sidebarLayout(
sidebarPanel(
selectInput("data", label = "Data set", choices = c("", "mtcars"))
),
mainPanel(uiOutput("tableUI"))
)
),
server = function(input, output) {
data <- reactive({
get(input$data, 'package:datasets')
})
output$tableUI <- renderUI({
if (input$data == "") {
div(
HTML("Please <strong>select</strong> a data set"),
tags$p(icon("exclamation"), "Please",tags$strong("select"), "a data set")
)
} else {
tableOutput("table")
}
})
output$table <- renderTable(head(data()))
}
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With