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.
We love it when R users help each other, but RStudio does not monitor or answer the comments in this thread. If you'd like to get specific help, we recommend the RStudio Community as well as the Shiny Discussion Forum for in depth discussion of Shiny related questions and How to get help article for a list of the best ways to get help with R code.comments powered by Disqus