why is reactlog showing multiple copies of same output

I have started using reactlog to debug reactivity problems after I read the excellent document written by Carson Sievert, Joe Cheng, and Barret Schloerke here. I urge all new users of shiny to read this article - it clears up a lot of doubts you may have on how reactivity works.

This is my first use of reactlog so please forgive me for the stupid question.

I was horrified to see how many times by plot is getting invalidated and surely am working to fix that. But before that I am unable to get the reason why just one output has multiple copies. Pls see screenshot of the snapshot in time of my reactlog where output$totbal is the renderPlot object repeating many times - some of them invalidated.

Also adding the code block that is defining output$totbal

output$totbal <- renderPlot({
       label_infl <-  input$infl_r %>% round(2) %>% paste0("%") %>% paste("Inflation:",.)
       tdata[grepl(grep_pat(),variable)] %>% 
       ggplot(aes(age,value)) +
       geom_line(aes(color=variable)) +
       geom_point(aes(color = variable),size=3,alpha=0.8) +
       geom_text(data = data.frame(),
                  aes(label= end_bal(data[[1]])["bank_balance"] %>% round(0) %>% format(big.mark=","), x = Inf, y= Inf),
                  hjust = 1, vjust = 1,
                  size=12, color = ifelse(end_bal(data[[1]])["bank_balance"] >0,"green","maroon"), fontface = "bold",check_overlap = T) +
       geom_text(data = data.frame(),
                  aes(label= end_bal(data[[1]])["MF_bal"] %>% round(0) %>% format(big.mark=","), x = -Inf, y= Inf),
                  hjust = 0, vjust = 0,
                  size=12, color = ifelse(end_bal(data[[1]])["MF_bal"] >0,"green","maroon"), fontface = "bold",check_overlap = T) +
       geom_label(data=data.frame(),aes(label =  label_infl),x=-Inf,y=Inf,size=6,hjust=0,vjust=0.9) +
       scale_x_continuous(breaks = input$cur_age : 100) +
       theme(legend.position="top")
   })

Apologies for not creating a reprex. This is because the question is on the fundamental structure of the app. I will however add a few descriptions of the reactive expressions, in above code. Hope this is helpful.

  • tdata is a data.table and depends on (or is a subset of) data which is a reactive list.
  • age, value and variable are column names inside tdata
  • grep_pat is a reactiveVal
  • end_bal is a function

I think a reprex would be extremely useful here — by simplifying the app to a point that you can share it with others, you are likely to discover exactly why your expectations don't agree with what you see in the reactlog. (I personally would never try to debug a reactivity problem without first removing all the extraneous components of the app; otherwise it's just too hard to isolate the problem with my mental model)

Alright Hadley, I will create a reprex. So I presume you also feel multiple copies of the output does seem abnormal i.e. is not in accordance with the design principles of shiny. Let me try to get a reprex out on this quickly. Thank you very much for your advice.:blush:

@Sanjmeh

I believe you have created an "anti-pattern" (or "anti-solution"). Joe talks about this in https://www.rstudio.com/resources/videos/effective-reactive-programming/ part 1 ~16:00 and describes this "anti-solution" pattern around 19:00.

A quick check that I like to use to avoid "anti-pattern"s is to make sure all render methods in the server definition are outside of observe or reactive-like methods. The only general exception to this when you are creating dynamic content, such as dynamic tabs, and have the ability to produce unique storage names in the output variable.


Please do follow up with @hadley 's request of a reprex! It will be much easier to confirm and explain in the code.

2 Likes

Many thanks @barret .. Just watched the first part of the two part Joe Cheng session.
To be honest the whole shiny reactivity thing was confusing for me and I badly need a conceptual course like this. It is now clear I was unknowingly using anti solutions (in the words of Joe Cheng) all over my app. Surely the reactlog has exposed my bad code. To be honest I was always a little unclear on the reactivity bit. So let me first watch both parts of Joe's session and then I will redo my app to the right structure in accordance with shiny philosophy and will then update this thread.

2 Likes