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.