The shinyApp function is run once, when you launch your app.
The server function is run once each time a user visits your app.
The R expressions inside render* functions are run many times. Shiny runs them once each time a user change the value of a widget.
Source scripts, load libraries, and read data sets at the beginning of app.R outside of the server function. Shiny will only run this code once, which is all you need to set your server up to run the R expressions contained in server.
Define user specific objects inside server function, but outside of any render* calls. These would be objects that you think each user will need their own personal copy of. For example, an object that records the user’s session information. This code will be run once per user.
output$plot <-renderPlot({ data <-getSymbols(input$symb, src ="yahoo",from = input$dates[1],to = input$dates[2],auto.assign =FALSE)chartSeries(data, theme =chartTheme("white"),type ="line", log.scale = input$log, TA =NULL)})
Each time renderPlot re-runs:
1. it re-fetches the data from Yahoo Finance with getSymbols, and 2. it re-draws the chart with the correct axis.
With reactive expression:
dataInput <-reactive({getSymbols(input$symb, src ="yahoo", from = input$dates[1],to = input$dates[2],auto.assign =FALSE)})output$plot <-renderPlot({chartSeries(dataInput(), theme =chartTheme("white"), type ="line", log.scale = input$log, TA =NULL)})
A reactive expression saves its result the first time you run it.
The next time the reactive expression is called, it checks if the saved value has become out of date (i.e., whether the widgets it depends on have changed).
If the value is out of date, the reactive object will recalculate it (and then save the new result).
If the value is up-to-date, the reactive expression will return the saved value without doing any computation.