Complex calculations in server shiny app

shiny
shiny-server

#1

Hello,

I have made a R script that reads in some data, does a lot of calculations on it, and then makes a map of it.
I want to make a Shiny app that does it automatically, where the user can load some csv-files and the calculations are done automatically. I am a newby, so I don't have a lot of experience with it

My problem here is that I am not sure how I have to update my data so everything works:

  • I can read my data in, so that is not a problem
  • in the server file, I am not sure how I should work. I need a reactive() code, because I use the data that has been put to the input. But should I put everything into the same reactive()? And how can I access the final data.frame that I need to make a map out? Or should I put everything in an output()?
migration_calc <- reactive ({
  migration <- migration()   #data that has been read 
  population= population() #data that has been read 
  
  # A lot of code to get to the final point
  Local_scenario #endproduct
})

When I want to make an output map after this code (by applying output$map...), I get an error that Local_scenario does not exist.


#2

Did you use renderPlot() to create your plot and assign it to your output$map?


#3

Based on that code, you’d use migration_calc()(not Local_scenario) to refer to the result of this reactive expression in other code. There’s a simple example of this design pattern here: https://shiny.rstudio.com/articles/tabsets.html (see the second example). The Reactive Expressions tutorial also goes into the philosophy in detail.

It’s not always clear when you’re starting out what the most efficient organization of your app is, so that’s a great question to ask. However, I think it will be hard for helpers to give much more advice without seeing more of the code (or at least a simplified outline of the code).