An introduction to server.R and UI.R

One of the special requisites to build web applications with Shiny is either the inclusion of, at least, the server.R and UI.R files or the inclusion of app.R, which should contain the equivalent codes of both UI.R and server.R. Shiny searches for these files to run the application.

UI.R stands for User Interface. In fact, UI.R is the file where the different parts of the application's frontend (that is, what the end users see) is defined. server.R, on the contrary, is the backend or the engine of the application, that is, where the data is processed.

As in any other web application, the Shiny-based applications have an underlying input-output concept where the user inserts information (input) and gets something back (output). However, in most cases, the input and output are displayed in the same window. In this sense, Shiny is very intuitive as its UI/server structure responds somehow to this natural idea: everything that the user sees goes under UI.R, and everything that the user does not see goes under server.R.

In a schematic view, the information flow could be summarized as follows:

An introduction to server.R and UI.R

In depth, the following are the steps:

  1. The user gives an input to the interface generated by UI.R.
  2. The input is passed to server.R. This step does not need to be programmed. Shiny provides the connectivity between the created interface and the R engine.
  3. The data is processed in server.R.
  4. server.R returns an output.
  5. The output is passed to the browser. As in step 2, this is done by the Shiny framework and does not need extra programming.
  6. The output is displayed as programmed in UI.R.

UI.R as a JavaScript/HTML wrapper

UI.R is basically a JavaScript/HTML wrapper. This means that some functions in the Shiny package transform the R code to HTML/JavaScript, readable by a web browser. These functions are mostly called within other functions, and there is no need for the programmer to explicitly call them. In fact, as it will be seen afterwards, UI.R can be replaced by an HTML document.

Including HTML within UI.R

Apart from the possibility of replacing UI.R with an HTML document, Shiny also enables the programmer to insert an HTML code within UI.R. As a result, images, different fonts, and more could be included without any need to write an HTML code. Normally, the tags are enclosed in a function.

For example, to insert an image instead of <img>...</img>, there is an img() function. The next chapter will cover in depth the tagging possibilities within Shiny.

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

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