It’s easy to get down the basics of R and Shiny, but the learning curve becomes significantly steeper after a point. Reactive values? Reactive events? Reactive observers? Let’s face it – it’s easy to get lost. We’re here to help.
Today you’ll learn all about the Observe function in R Shiny – what it is, what it does, and how to use it in practice with two hands-on examples. We’ll kick things off with a bit of theory, just so you can understand why reactive observers are useful.
The expression passed into the function is triggered every time one of the inputs changes. If you remember only a single sentence from this article, that should be the one.
So, what makes the function different from regular reactive expressions? Well, yields no output and can’t be used as an input to other reactive expressions. Reactive observers are only useful for their “side effects”, such as I/O, triggering a pop-up, and so on.
As mentioned before, observers re-execute as soon as their dependencies change, making them use a concept known as eager evaluation. On the other end, reactive expressions are lazy-evaluated, meaning they have to be called by someone else to re-execute.
You can check all the parameters the function can accept in the official documentation, but we won’t go over that here. Instead, we’ll dive into two hands-on examples.
To demonstrate how the Observe function in R Shiny works, we’ll do something you’d never do in a real life. But there’s a good reason why we’re doing it. It perfectly demonstrates what the function does, and how a reactive observer works.
We already mentioned that the function is triggered every time one of the inputs changes. So, we can declare input and attach it to an observer to monitor what happens as we mess around with it.
That’s exactly what the code snippet below does. There are two UI elements – and . Inside the function, we have attached a reactive observer to the , and each time it changes, we update the text contents of the .
There are easier and more intuitive ways to implement the same behavior, but you get the gist – each time the input changes we trigger some code. It’s irrelevant which code we trigger, but in this case, the code just changes the text:
Below you’ll find a GIF demonstrating how the R Shiny app works:
That was easy, right? Next, we’ll go over a slightly more complicated example.
All dashboards have dropdown menus, or select inputs, as R Shiny calls them. The premise is simple – you select a value, and then charts/tables on a dashboard are re-drawn.
But how can you introduce dependencies between dropdown menus? In other words, how can you base what’s shown as options for the second dropdown menu based on the value selected from the first menu?
That’s where the Observe function in R Shiny comes in.
If you want to follow along, copy the following dataset and save it as :
The example Shiny dashboard below will read the dataset and declare two ‘s in the UI. The second depends on the first. For example, if the user selects “A” in the first dropdown menu, only options of “A1” to “A5” should be shown in the second menu. The same thing goes for the other letters.
Inside the function, we’ll use the package to filter out the records we don’t need, and then use the function to “re-write” the choices for the second dropdown menu.
That’s it! Let’s see the code:
Below is a GIF showing you how the app works:
Works like a charm. Let’s make a short recap and go over the homework assignment.
Today you’ve learned the basics of the Observe function in R Shiny (reactive observer) both through theory and practical examples. The moral of the story is as follows – use the function every time you want your Shiny app to react to a change of the input. Common examples are conditionally rendered UI elements, such as options of a dropdown menu.
For a homework assignment, we encourage you to explore the reactive observer further on your own. You could try working with radio buttons, and conditionally change the values of a subsequent group based on what the user previously selected. Give it a go, and make sure to share your results with us on Twitter – @appsilon. We’d love to see what you come up with.
The post Observe Function in R Shiny – How to Implement a Reactive Observer appeared first on Appsilon | Enterprise R Shiny Dashboards.