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.

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