Get startedGet started for free

Stop - delay - trigger

1. Stop - delay - trigger

In this video you will learn how to stop, delay, and trigger Shiny actions.

2. Isolating actions (1/3)

Shiny's reactive programming framework is designed such that any changes to inputs automatically update the outputs that depend on it. For example, notice how the server code automatically runs to update the greeting when the user inputs their name, or selects the greeting type. Suppose we don't want to trigger an automatic update of the greeting when the user selects the greeting type Hello/Bonjour.

3. Isolating actions (2/3)

We can achieve this using the function isolate. Ordinarily, the simple act of reading a reactive value is sufficient to set up a relationship, where a change to the reactive value will cause the calling expression to re-execute. The isolate function allows an expression to read a reactive value without triggering re-execution when its value changes. Notice how the greeting only updates when the user enters the name, and NOT when the greeting type is changed.

4. Isolating actions (3/3)

To recap, wrapping a reactive value inside isolate makes it read-only, and does NOT trigger re-execution when its value changes.

5. Delaying actions (1/3)

In some situations,we might want more explicit control over the trigger that causes the update. For example, suppose, we want the greeting to be displayed only when the user clicks on a button.

6. Delaying actions (2/3)

We can achieve this using eventReactive. The first argument to eventReactive is the event that should trigger the update. In this case, it is the action button named `show_greeting`. The second argument is the expression it should return when the event is triggered. Notice how, the reactive expression rv_greeting is now executed only when the user clicks on show_greeting.

7. Delaying actions (3/3)

To recap, you can delay the evaluation of a reactive expression by placing it inside eventReactive, and specifying an event in response to which it should execute the expression.

8. Triggering actions (1/2)

There are times when you want to perform an action in response to an event. For example, you might want to display a greeting as a modal dialog, in response to a click. You can achieve this using observeEvent. The syntax is similar to eventReactive, with the first argument being the event that triggers the action, and the second argument being the action.

9. Triggering actions (2/2)

In this example, a modal dialog with a greeting is displayed in response to the user clicking on the show_greeting button. Note that unlike eventReactive, observeEvent is used only for its side-effects and does NOT return any value.

10. Let's practice!

Let us practice what we have learned to control actions in Shiny apps in a more granular fashion.