The ui.R of the minimal example

The ui.R file is a description of the UI, and is often the shortest and simplest part of a Shiny application. In the following code, note the use of the # character, which marks lines of code as comments that will not be run, but which are for the benefit of the humans producing the code:

fluidPage(                                 # Line 1 
  titlePanel("Minimal application"),       # Line 2 
  sidebarLayout(                           # Line 3 
    sidebarPanel(                          # Line 4 
      textInput(inputId = "comment",       # Line 5 
                label = "Say something?",  # Line 6 
                value = ""                 # Line 7 
                )),                        # Line 8 
    mainPanel(                             # Line 9 
      h3("This is you saying it"),         # Line 10 
      textOutput("textDisplay")            # Line 11 
    ) 
  ) 
) 

The following list is an explanation of each line:

  • Line 1: Flexible layout function
  • Line 2: Title
  • Line 3: Standard inputs on the sidebar; outputs in the main area layout
  • Line 4: The sidebar layout function
  • Line 5: Give the name of the input element; this will be passed to server.R
  • Line 6: The display label for the variable
  • Line 7: The initial value
  • Line 9: The output panel
  • Line 10: The title drawn with the HTML helper function
  • Line 11: The output text with the ID, textDisplay, as defined in server.R

To run a Shiny program on your local machine, you just need to do the following:

  1. Make sure that server.R and ui.R are in the same folder
  2. Make this folder R's working directory (using the setwd() command—for example, setwd("~/shinyFiles/minimalExample"), or with the Session > Set working directory menu option)
  3. Load the Shiny package with the library(shiny)command
  4. Type runApp() in the console (or, in Rstudio, click Run app just above the code window)

Using runApp() with the name of a directory within works just as well—for example, runApp("~/shinyFiles/minimalExample"). So instead of setting the working directory to the location of your application and then using runApp() separately, the whole thing can simply be carried out in one instruction, passing runApp() in the name of the directory directly. Just remember that it is a directory and not a file that you need to point to.

The first instruction, fluidPage(..., tells Shiny that we are using a fluid page layout. This is a very flexible layout function whose functionality we will explore further in Chapter 4, Mastering Shiny's UI Functions. Next, the title of the application is defined very simply using the titlePanel() function. Then follows the main layout instruction; in this case, we are going to use the simplest UI layout, sidebarLayout(), which places inputs on the left (or right, optionally) and the main output section in the middle. All of the UI elements are defined within the sidebarLayout() function.

The next two instructions perform the main UI setup, with sidebarPanel() setting up the application controls and mainPanel() setting up the output area. The sidebarPanel() phrase will usually contain all of the input widgets; in this case, there is only one: textInput(). The textInput() widget is a simple widget that collects text from a textbox that users can interact with using the keyboard. The arguments are pretty typical among most of the widgets, and are as follows:

  • inputId: This argument names the variable, so it can be referred to in the server.R file
  • label: This argument gives a label to attach to the input, so users know what it does
  • value: This argument gives the initial value to the widget when it is set up; all the widgets have sensible defaults for this argument—in this case, it is a blank string, ""

When you start out, it can be a good idea to spell out the default arguments in your code until you get used to which function contains which arguments. It also makes your code more readable and reminds you what the return value of the function is (for example, value = TRUE would suggest a Boolean return).

The final function is mainPanel(), which sets up the output window. You can see that I used one of the HTML helper functions to make a little title, h3("..."). There are many of these helper functions included, and they are incredibly useful for situations where you either don't want to do too much styling with HTML and CSS yourself or don't know how. Let's just stop very quickly to look at a few examples.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset