a reactiveValues list item not evaluated, yet not detected by req

Hello everyone,
I'm having a bit of trouble understanding the order of evaluation of lines of code in shiny : I'm using reactiveValues() together with eventReactive to create/manipulate data in my app. Consider the following example :

library(shiny)

ui <- fluidPage(
    numericInput("num", "Enter number", value = 1, min = 0, max = 2),
    
    plotOutput("hist")
)

server <- function(input, output, session) {
  val <- reactiveValues()
  
  val$vec <- eventReactive(input$num, {
    rnorm(100, input$num)
  })
  
  output$hist <- renderPlot({
    req(val$vec)
    hist(val$vec)
  })
  }

shinyApp(ui, server)

#Output : 
# Warning: Error in hist.default: 'x' must be numeric
# 170: stop
# 169: hist.default
# 167: renderPlot [/home/paulet/Documents/TAAAAF/Stage/Tests/R_tests/testestes/testest/app.R#18]
#                  165: func
#                  125: drawPlot
#                  111: <reactive:plotObj>
#                    95: drawReactive
#                  82: renderFunc
#                  81: output$hist
#                  1: runApp

From what I gathered about reactive programming, hist should be able to be plotted :

  • num has a default value
  • so val$vec is able to be evaluated
  • then hist can be plotted.

Even if, for some reason, renderPlot is called before val$vec exists, it should return silently because of the usage of req, and come check again later, but this is not happening.
The idea that eventReactive was not executed crossed my mind, but as by default ignoreInit = FALSE, it should be.

The twist is that if I do not use reactiveValues but just val(), it works properly.

Could someone with a firmer grasp on these concepts give me a hand?
Thanks in advance.

PS : I know I could do away with reactiveValues in this short example, but in the context of my app, it is necessary.

reactives need to be accessed as though they were functions, with () after their names.
so in your case

  output$hist <- renderPlot({
    req(val$vec())
    hist(val$vec())
  })

or with minimal typing

  output$hist <- renderPlot({
    hist(req(val$vec()))
  })

Are you sure it is the case for reactive created via reactiveValues? From the reactiveValues help page, in the example section there is :

## Not run: 
# From within a reactive context, you can access values with:
values$a
values[['a']]

Edit : The proposed adding of parentheses does not fix the issue in the example code
Edit 2 : actually it did

yes, and if you set it to be the number 3, that would be fine

values[['a']] <- 3
shiny::isolate(values[['a']] )
# 3

but you have done the equivalent of setting it to a reactive wrapping 3

values[['a']] <- reactive(3)
shiny::isolate(values[['a']] )
# reactive(3)
shiny::isolate(values[['a']]())
# 3
1 Like

Oops actually it did fix the issue, thanks! Now I'll try to understand and fix my code. Thank you very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.