An optional exercise

If you want to have a practice before we move on, take the existing code and modify it so that the output is a plot of a user-defined number of observations, with the text as the title of the plot. The plot call should look like the following:

hist(rnorm(XXXX), main = "YYYY") 

In the preceding line of code, XXXX is a number taken from a function in ui.R that you will add (sliderInput() or numericInput()) and YYYY is the text output we already used in the minimal example. You will also need to make use of renderPlot(); type ?renderPlot in the console for more details.

So far in this chapter, we have looked at a minimal example and learned about the basic commands that go in the server.R and ui.R files. Thinking about what we've done in terms of reactivity, the ui.R file defines a reactive value, input$comment. The server.R file defines a reactive expression, renderText(). It depends on input$comment.

Note that this dependence is defined automatically by Shiny. The renderText() expression uses an output from input$comment, so Shiny automatically connects them. Whenever input$comment changes, renderText() will automatically run with the new value. The optional exercise gave two reactive values to the renderPlot() call, and so, whenever either changes, renderPlot() will be rerun. In the rest of this chapter, we will look at an application that uses some slightly more advanced reactivity concepts, and by the end of the book, we will have covered all the capabilities that Shiny offers and when to use them.

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

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