Two-file Shiny apps
By: Mine Cetinkaya-Rundel
Before of version 0.10.2, Shiny applications needed to be split into two separate files,
ui.R, that include the code needed to define the UI and server components, respectively. This method is still supported in later versions of Shiny.
To create a two-file app, create a new directory (for example,
newdir/) and place two files, called
server.R, in the directory.
~/newdir |-- ui.R |-- server.R
To run it, call
We place the user interface definition in
And we place the server function definition in
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.
If you have larger apps, you may find that having separate
server.R files makes your code easier to manage.
If you have questions about this article or would like to discuss ideas presented here, please post on RStudio Community. Our developers monitor these forums and answer questions periodically. See help for more help with all things Shiny.