The reactiveValues() function

reactiveValues() creates a list of objects that can operate with reactive values but are not reactive values themselves. The main difference is that reactiveValues (unlike reactive objects) are not re-executed whenever an input value changes.

Due to this, reactive values are an optimal tool whenever certain register of previous inputs is needed. A good example of this could be counting the number of times an input value changes. In this case, a code is needed where the value of the counter depends on the input value change and on the counter's own previous value. As there is no possibility of operating with reactive values outside a reactive context, this should be inside a reactive context. In this case, as the result will not be displayed inside the application, the code will be inserted inside observe().

The first idea would be to place a counter inside observe(). With the first example, the code should look like this in server.R:

library(shiny)

#initialization of server.R
shinyServer(function(input, output) {

  observe({
    counter <- 0
    if(!is.na(input$number)){
      counter <- counter + 1
      cat(counter, sep ="
")}
    })

  
  #Plot generation
  output$plot <- renderPlot({
    plot(1/1:input$number)
  })
})

The counter value would be normally stored frequently, but in this example, in order to make it easier to understand the idea of reactive values, the counter value will be just printed in the console. However, in this example, 1 is constantly printed. This is due to the fact that the observe clause is re-executed whenever the input changes. This means that the counter is reset whenever the value is changed. It is in these kinds of cases where a reactive value is needed, that is, an object that is created outside a reactive expression is updated depending on inputs but is not re-executed whenever an input changes.

Now that we know that we need a reactive value, a second approach in server.R would be something like this:

library(shiny)

#initialization of server.R
shinyServer(function(input, output) {

  reactiveval.list <- reactiveValues(counter = 0)


  observe({
    if(!is.na(input$number)){
      reactiveval.list$counter <- reactiveval.list$counter + 1
      cat(reactiveval.list$counter, sep ="
")}
    })

  
    #Plot generation
    output$plot <- renderPlot({
    plot(1/1:input$number)
  })
})

Tip

Run this code for a couple of seconds only as it can crash your computer.

In this case, numbers are printed endlessly and independently of a user's input. This is due to the fact that although the reactiveval.list$counter counter is inside an observe() clause, in this case, the update condition (the dependency) relies only on the fact that input$number is not NA. So, unless input$number is NA, the piece of code will be re-executed. The underlying problem here is that the code should be re-executed based upon two conditions: input$number is not NA and the value of input$number has changed.

For this reason it is necessary in this case to isolate the counter. The reactive value that will trigger the execution of the counter in this case will be, of course, input$number. Take a look at the following code for server.R:

library(shiny)

#initialization of server.R
shinyServer(function(input, output) {

  reactiveval.list <- reactiveValues(counter = 0)

  observe({

    input$number
    isolate({
       (!is.na(input$number)){
        reactiveval.list$counter <- reactiveval.list$counter + 1
        cat(reactiveval.list$counter, sep ="
")}
    })
  })


  #Plot generation
  output$plot <- renderPlot({
    plot(1/1:input$number)
  })
})

As it can be seen in this example, the combination of the three methods introduced, isolate(), observe(), and reactiveValues(), is perfectly possible. In fact, this is sometimes the only way to develop certain functionalities in Shiny. For this reason, it is very important to have a clear understanding of how each of them works and what can be done with them. This is can be only achieved by using them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset