Two-file Shiny apps

Before of version 0.10.2, Shiny applications needed to be split into two separate files, server.R and 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 ui.R and server.R, in the directory.

|-- ui.R
|-- server.R

To run it, call runApp("newdir").


We place the user interface definition in ui.R:

ui <- fluidPage(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100)


And we place the server function definition in server.R:

server <- function(input, output) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')

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 ui.R and 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.