Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny Apps: is it possible to render HTML in validation messages?

Tags:

r

shiny

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()))

    }

  )
)
like image 629
sebkopf Avatar asked Apr 11 '19 18:04

sebkopf


People also ask

Does Shiny use HTML?

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.

How do you validate user input in shiny Pokemon?

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.


1 Answers

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()))
    }

  )
)
like image 82
Geovany Avatar answered Oct 18 '22 01:10

Geovany