R Tutorial: Reactivity 101
Key Takeaways
This video tutorial covers the basics of reactivity in Shiny, a powerful concept that enables outputs to automatically react to changes in inputs, and provides hands-on coding examples using R.
Full Transcript
one of the most important concepts in SHINee is the concept of reactivity reactivity or reactive programming is a key part what makes SHINee so powerful reactivity is what enables outputs to automatically react to changes in inputs reactivity in SHINee can seem intimidating but at a basic level it means that when the value of a variable X changes then anything that relies on X gets reacts acute 'add this is very different from what you're used to in our consider this code x equals 5 y equals x plus 1 then X gets 10 normally you would say that Y has the value of 6 but if x and y are both reactive variables then Y would actually be 11 because Y would update anytime that exchanges this is a very powerful concept that is very useful for the responsiveness and interactivity of SHINee but it may seem confusing at first only reactive variables behave this way and in SHINee all inputs are automatically reactive that is why you can always use input dollar sign input ID inside render functions and you can be sure that whenever output depends on this input we'll use the most up-to-date value whenever the input changes for example in this code the my plot outputs depends on the Nam input this means that whenever the Nam input changes my plot will react and reevaluate one important thing to remember about reactive variables is that they can only be used inside reactive contexts any render function is a reactive context so you don't need to worry about using reactive values inside render functions if you try accessing your reactive value outside or reactive context you will get an error for example suppose you try to print the value of input dollar sign numb like this SHINee will give you a helpful error message telling you that this cannot be done outside of a reactive context so how would you access a reactive value that's what the observed function is for observe is another reactive context that you use to access a reactive variable this code is the correct way to print the value of an input all we did is wrap the print statement inside an observe function this pattern is actually a very common use of observed think of it is a simple debugging tool to trace the different values of an input note that if you use two reactive values inside observe like in this code then each of these variables becomes a dependency for the observer the entire observe code chunk will get rerun every time any of its dependencies are updated this means that when either num1 or nom to update this whole code will run and both values will get printed you can create your own reactive variables using the reactive function which is another reactive context suppose you wanted to variable X to be one larger than input num you might be tempted to write this code this will result in the same error as before since input num is being accessed outside a reactive context the correct way to define a reactive variable is using the reactive function like this when you want to access a reactive variable that you created with the reactive function you need to add parenthesis to the variable name as if you were calling a function notice how in the observer in this code accessing x requires parentheses but accessing the input does not now that you reviewed the basics of reactivity let's try some exercises
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/case-studies-building-web-applications-with-shiny-in-r at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
One of the most important concepts in Shiny is the concept of reactivity.
Reactivity, or reactive programming, is a key part of what makes Shiny so powerful. Reactivity is what enables outputs to automatically *react* to changes in inputs. Reactivity in Shiny can seem intimidating, but at a basic level it means that when the value of a variable x changes, then anything that relies on x gets re-evaluated.
This is very different from what you're used to in R. Consider this code: x equals 5. y equals x plus 1. then x gets 10.
Normally, you would say that y has the value of 6. But if x and y are both reactive variables, then y would actually be 11 because y would update any time that x changes.
This is a very powerful concept that is very useful for the responsiveness and interactivity of Shiny, but it may seem confusing at first.
Only reactive variables behave this way, and in Shiny all inputs are automatically reactive. That is why you can always use input$ less than inputId greater than inside render functions, and you can be sure that whatever output depends on this input will use the most up-to-date value whenever the input changes.
For example, in this code, the my_plot output depends on the num input. This means that whenever the num input changes, my_plot will react and re-evaluate.
One important thing to remember about reactive variables is that they can only be used inside reactive contexts. Any render function is a reactive context, so you don't need to worry about using reactive values inside render functions.
If you try accessing a reactive value outside a reactive context, you will get an error. For example, suppose you tried to print the value of input dollar sign num like this. Shiny will give you a hel
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
🎓
Tutor Explanation
DeepCamp AI