The server.R of the minimal example

Let's now look at server.R, where it should all become clear. Look at the following code:

function(input, output) { # server is define    d here output$textDisplay = renderText({ # assign to output$textDisplay

paste0("You said '", input$comment, # from text input
"'.There are ", nchar(input$comment),
" characters in this.")
})
}

We define the reactive components of the application within function(input, output) {...}. On the whole, two types of things go in here. Reactive objects (for example, data) are defined, which are then passed around as needed (for example, to different output instructions), and outputs are defined, such as graphs. This simple example contains only the latter. We'll see an example of the first type in the next example.

An output element is defined next with output$textDsiplay = renderText({..}). This instruction does two basic things. First, it gives the output a name (textDisplay) so that it can be referenced in ui.R (you can see it in the last part of ui.R). Second, it tells Shiny that the content contained within is reactive (that is, it will be updated when its inputs change) and it takes the form of text. We will cover advanced concepts in reactive programming with Shiny in a later chapter. There are many excellent illustrations of reactive programming at the Shiny tutorial pages, available at rstudio.github.io/shiny/tutorial/#reactivity-overview.

The actual processing is very simple in this example. Inputs are read from ui.R by the use of input$..., so the element named in ui.R as comment (go and have a look at ui.R now to find it) is referenced with input$comment.

The whole command uses paste0() to link strings with no spaces (equivalent to paste(..., sep = "")), picks up the text the user inputted with input$comment, and prints it, along with the number of characters within it (nchar()) and some explanatory text.

That's it! Your first Shiny application is ready. The full code can be found here, https://gist.github.com/ChrisBeeley/4202605cf2e64b4f609e. Using these very simple building blocks, you can actually make some really useful and engaging applications.

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

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