The concept of reactivity

Reactivity is the main concept that underlies the Shiny structure. Basically, this means that an object changes depending on the changes of another object. This concept is intimately related to the input/output relationship. In other words, the output object reacts (changes) whenever the input changes.

For example, let's think of an application that counts the number of words in the phrase that has been passed. It would look similar to this:

The concept of reactivity

In this case, whenever the phrase passed to the textbox changes, the text below the textbox changes. The reaction process automatically and instantly occurs by default. This means that, unless explicitly told otherwise, the re-execution will take place immediately. For example, if the phrases change, an order is automatically triggered to re-execute the script in server.R and consequently, to change the text (that is, the output). To sum it up, the reactive process starts when the input changes and finishes when the output is updated.

Of course, this instant re-execution might not be desired in many cases. For instance, if the execution of server.R requires a high consumption of resources and/or the execution times are very long. As it will be seen in Chapter 7, Advanced Functions in Shiny, a button that delays the execution of server.R script until the button is hit can be included. In this case, for instance, the user could change multiple inputs. This will cause a sole server.R re-execution and consequently, a sole output change.

In practice, most of the applications tend to have multiple outputs. Sometimes, the processes to generate some of them have certain things in common. Back to the previous example, imagine that, apart from an output that shows the number of words, we have a second output that shows which is the longest word and how many characters it has. The following screenshot shows how this would look like:

The concept of reactivity

Both the outputs take the same input but process it differently. Let's see what each of them does:

  • Word counter:
    • Split the passed string (that is, the input) by spaces (we assume that a single space is the word delimiter)
    • Count how many elements were obtained
  • Longest word and amount of characters:
    • Split the passed string (that is, the input) by spaces
    • Calculate how many characters each of the obtained elements have
    • Select the highest value in the preceding step
    • Select the element that has the highest value

Although being different, both output generation processes have the first step in common. In this case, instead of repeating the same process for both outputs, Shiny provides the possibility of executing this process only once by generating intermediate objects between the input (the passed phrase) and the outputs. These objects are reactive, but they also cause reaction for the objects that depend on them, which can be the outputs (that is, the final stages of reactivity) or other intermediate objects.

Generating intermediate objects has two main advantages. Firstly, the object is reprocessed only once and used by all the elements that depend on it instead of having the same piece of code executed multiple times. Secondly, this avoids code repetition, which makes it much easier to maintain.

With this same idea of avoiding unnecessary process repetitions in our application, the next section will explain other scenarios where this can happen and how it can be prevented.

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

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