R-script in shiny evaluated stepwise?


#1

Sorry I don't have a reproducible example yet, as my script is already quite complex, but here is a description what it is doing: Basically it it imports some files, does some calculations and rearrangements on the data and generates some tables that are exported. In the end it will generate some plots.

Step 1:

   input1 = read.csv(rawfile1)
   table1 = reactive(input1) %>% some calculations %>% write.csv(export1)              
   table2 = reactive(Input1) %>% some other calculations %>% write.csv(export2)

Step 2:

   input2 = read.csv(rawfile2)
   table3 =  reactive(input2) %>% some calculations --> write.csv(export3) 
   table4 = reactive(input1) + some calculations with reactive(input2)
   table5 = reactive(table4)
               %>% some transformations --> write.csv(export4)
                %>% some more transformations --> write.csv(export5)
 plot1 = reactive(table5)

Step3....

Then I have an UI with a navbar panel for Step1, Step2, etc, in each I have some tabsets to see the different outputs (renderdataTable, renderPlot, etc). The main purpose is to show the intermediate steps, to see that the processing went well and no problems occured.

So far everthing is working fine. But then I noticed that some of the intermediate steps are only called when I check the results in the UI. So for example when I click on the tab that renders the table2 - the table2 seems to be calculated and export2.csv is found in the results folder, but not when I jump directly to Step2 (because I know it works well as I loaded exactly the same file earlier)
Similarily export4 and 5 are created when I click on the tab that shows table4 or the one showing plot1, but then I may miss export3.csv

I started to work with shiny very recently, I can see that this might be not a bug, but a feature to distribute the calculation load to the time when the tables are needed.
But I expected the calculation in the background to be independent from showing the results in the UI?

Especially in this case I want the tables to be calculated in the background even if I don't click on each and every tab. Showing the intermediate steps is meant here for validation purposes.

Can I force to run all calculations at once?

Or can I define blocks of code that should be evaluated together, e.g. everything in step1 and everything in step2? But I think I need to generate different intermediate dataframes, as parts of these dataframes (e.g. numbers of entries in col. x, list of entries in col y) are used and combined in other parts of the script, so I need to be able to adress them independently.

Thanks in advance!
Matthias


#2

Take a look at the outputOptions() documentation — as you suspected, the default is for outputs to be suspended when hidden for the sake of performance, but you can change that (and change the calculation priority).

If you decide you want a completely stepwise presentation, you could also try something like this: Shiny app with sequence of pages
(From Dean Attali’s Shiny Tips and Tricks, a great thing to know about!)


#3

Thanks this may help!
Do you have any idea where to put this call?
It should probably go into the ui.R but I tested different locations and it doesn't work at all, all I get is a "ERROR: object 'output' not found. I also tried output$name_of_table instead but this is working even less.


#4

Okay I have it!
It has to go in the server.R, I put it now (for convenience) behind the render___ calls and then it works.
Still have to figure out what this means for the intermediate dataframes that are not rendered dicrectly, but probably this is done when the next table accessing this dataframe is created.

PS: I know Dean Attalis Tips and already included the loading animation.


#5

Yes, you'll notice that output is one of the arguments to server(), so the output object exists within the server() function. You can see an example of using this output option here (second code listing):
https://shiny.rstudio.com/articles/dynamic-ui.html

If you need help understanding how reactive execution is happening in your app (this can be difficult to reason about, because of shiny's non-linearity!), tools like Showcase Mode and the Reactive Log can help. See: https://shiny.rstudio.com/articles/debugging.html