Reactivity 101
1. Reactivity 101
In this video, we will discuss the basics of reactive programming, the fundamental framework that powers the magic behind Shiny!2. Fundamentals of reactivity
Consider this simple Shiny app, that displays a greeting when a user enters a name. Notice how whenever the name is changed, the line of code in the server turns yellow. This is reactivity at its best! Essentially, the output is notified whenever any of its dependencies change, and it updates in response to that.3. Reactive source
The two fundamental components in reactive programming are reactive sources, and endpoints. A reactive source is typically a user input that comes through a browser interface. A reactive source can be connected to multiple endpoints, and vice versa. For example we might have a UI input widget to make a selection of our data, and the selected data can be used in multiple outputs like plots and summaries.4. Reactive endpoint
A reactive endpoint is something that appears in browser window, such as a plot or a table. Endpoints are notified when the underlying value of one of its source dependencies change, and it updates in response to this signal. An example of reactive endpoints is observers. For example, an output object is a reactive observer. Under the hood a render function returns a reactive expression, and when you assign this reactive expression to an output dollar value, Shiny automatically creates an observer that uses the reactive expression.5. Reactive conductor
There is a third type of reactive component called reactive conductor, that is dependent on one or more reactive sources, and is also a dependency of one or more reactive endpoints. It is often used to encapsulate repeated computations, that can be expensive. In this app, notice how we filter the babynames data twice, once in the plot output, and again in the table output. There are two issues at hand here. First, the code is repeated twice. Second, the code is evaluated twice.6. Reactive expressions
We can solve both issues using an implementation of reactive conductors, called reactive expressions, to compute the filtered data. A reactive expression behaves just like a function, but with two key differences: 1. It is lazy, meaning that it is evaluated only when a reactive endpoint calls it. 2. It is cached, meaning that it is evaluated only when the value of one of its underlying reactive sources changes. Note that code inside a reactive needs to be wrapped inside curly braces.7. Let's practice!
Now that you have understood the basics of reactivity, and the three types of reactive components, let us practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.