Chapter 7. Advanced Functions in Shiny

At this stage, it is supposed that you already know how to code web applications in Shiny with certain complexity. In this chapter, three main topics will be covered that will help you to expand the possibilities of your applications a step further. They are mainly four functions that operate on the server side (that is, they are used in the server.R script):

  • validate(): This validates the inputs passed according to some condition
  • isolate(): This prevents the update of a piece of code given a change in a reactive value
  • observe(): This provides a reactive context but generates no displayable output
  • reactiveValues(): This creates a list object whose element can deal with reactive values
  • Input updates: These are a group of functions that change one or more characteristics of a specific input (for example, sliderInput would require updateSliderInput).

The validate() function

The name of this function is definitely self-explanatory. In almost all the cases and except for extremely rare occasions, validate() is used along with need(), which is an easy wrapper that evaluates an expression in an extended way and produces an error message.

The expression evaluation performed by need() is extended with respect to normal condition evaluation in R, because it evaluates to False (and displays an error message) if the evaluation returns NA or the passed variable is empty. need() has two arguments for the error message: message and label. The first one is simply the text to be displayed and the second one is a character that can be used to be inserted dynamically into the error message. By default, message is paste(label, "must be provided"), so it expects the label argument. However, if message is passed, label is ignored. It is normally recommended to directly specify message.

Looking at Example 1 again, this could be a good need() statement to include:

need(input$number > 0, "Provide a number greater than 0")

In this case, if no number or any number smaller than or equal to zero is passed, need() will pass to validate() a warning that the number provided is not valid. validate() supports multiple need() calls and displays the error messages of the need() calls whose expressions were evaluated as False.

validate() is used inside a render expression such as renderPlot() or renderText(). This prints an error message (which can be the concatenation of various error messages) and stops the execution of any output object that is rendered afterwards. As validate() basically prints an error message, it is always advisable to place it before the sentence that effectively produces the output (for example, plot() in renderPlot()). Although there might not be any obvious difference for the end user, placing validate() before the plot call will prevent the program from making the plot. If this is placed afterwards, the application will create the plot but will put the error message above instead.

Again, taking the first example provided in this book, the following code shows clearly how this function is used and the results it produces in server.R (UI.R remains exactly as the first example):

library(shiny)

#initialization of server.R
shinyServer(function(input, output) {

  #Plot generation
  output$plot <- renderPlot({
    validate(
      need(input$number > 0, "Provide a number greater than 0"),
      need(input$number < 100, "Provide a number smaller than 100"))
    plot(1/1:input$number)
    
  })

})

You can see for yourself how the error messages are displayed. As it was explained previously, due to the evaluation mechanism of need() if no number is passed, both messages are displayed. This is a very good outcome in terms of usability purposes as it provides the end user all the necessary information of the input value expected for that field.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset