Загрузка страницы

R Tutorial: Reactivity 101

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 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 observe 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 observe, think of it as 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 re-run every time any of its dependencies are updated. This means that when either num1 or num2 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 define a 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 so.

When you want to access a reactive variable that you created with the reactive function, you need to add parentheses 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 values does not.

Now that you reviewed the basics of reactivity, let's try some exercises.
#DataCamp #RTutorial #Building #Web #Applications #Shiny

Видео R Tutorial: Reactivity 101 канала DataCamp
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
18 апреля 2020 г. 7:00:10
00:05:30
Яндекс.Метрика