3.1 Structure of a Shiny App

3.1.1 Two files Shiny app

A simple shiny app is a directory containing two R scripts, one is ui.R, which controls the layout and appearance of your app, the other is server.R, which contains the instructions that your computer needs to build your app. Note that the names for the scripts are fixed, you should NOT use other names.

Exercise 1: App template (Manually)

Let’s create a new directory named 01-hello (or whatever you like) and then create two empty ui.R and server.R files within it.

Open ui.R with any editor you want and put the following code in it:


Then copy the following code to server.R. Note that the server.R contains one single unnamed function.

function(input, output) {

For historical reasons, usually shinyUI and shinyServer functions are used to wrap the UI and server side scripts, but it is no longer required as of Shiny 0.10.

To run the app, open an R session and load the shiny package, then run the runApp function by

  1. giving the path to the app (change path/to/01-hello accordingly):

    runApp('path/to/01-hello') # or use 'path\\to\\01-hello'
  2. or you can switch the R working directory into the app folder (by using R command setwd("path/to/01-hello")) and run


Exercise 1: App template (Using Rstudio)

It is much easier to develop a Shiny app using Rstudio:

  1. (Optional) In Rstudio, File -> New Project…
  2. New file -> Shiny Web App…
  3. Modify the default template
  4. Click ‘Run App’.

After running the app, you’ll get an empty web page.

3.1.2 Single-file Shiny App

As the name implied, a single-file shiny app consists of a single file called app.R which contains both the server and UI components. As an example, we can rewrite Exercise 1 into a single-file shiny app with the following code in the app.R file:

ui <- fluidPage()
server <- function(input, output){}
shinyApp(ui = ui, server = server)