little detail about a shiny app example

hi, I was browsing trough the shiny gallery and this example cought my eyes : shiny-examples/060-retirement-simulation at main · rstudio/shiny-examples · GitHub
How does the ui code and the server code are assembled to display the app here :
Shiny - Retirement simulation

I am asking cause I thought you need

server<- function(input, output, session){}

but in the server's code the function bit come later would be weird to have something like

server<-[...]
[...]
function(input,output,session){...}

Please see this related article:

For applications defined this way, the server.R file must return the server function, and the ui.R file must return the UI object (in this case, the UI object is created by fluidPage() ). In other words, if the files contained other code (like utility functions) you must make sure that the last expression in the file is the server function or UI object.

The above is also fulfilled if server.R returns a anonymous function (as long as it is the last expression in the file).

nice thank you !
Is it an issue to put the utilities functions in the anonymous function ?

For sure you can put utility functions inside your (anonymous or named) server function. However, they are than only available in the scope of the server function and will be re-instantiated for each shiny-session. If you place them outside (above) the server function or in the global.R file they are instantiated once on app start and are globally available (which reduces time to spin up a new shiny-session).

Please see this related article:

https://shiny.rstudio.com/articles/scoping.html

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.