R application coding

When you start RStudio you will see the (hopefully) familiar display as follows. There are four main windows in the display (going clockwise from the upper left):

  • Source code window. In this window you enter the script for a particular R script file. Saving a file in the source code window will update the file stored on disk (and it is viewable in the files window).
  • Environment/History window. In this window, any datasets that have been loaded or variables that have been established are displayed.
  • Files window. In this window, the current sets of files are displayed (and are accessible by clicking on a file to pull it into the source code window).
  • The execution window. In this window, actual commands are made to the R server.

For the simple example we will use server.R and io.R files very similar to the files on the Shiny web pages, for example.

The app.R file has the following commands:

## app.R ##
# load the shiny dashboard library
library(shinydashboard)
# create a dashboard with title, sidebar and body
ui <- dashboardPage(
  dashboardHeader(title = "Shiny dashboard"),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(plotOutput("plot1", height = 250)), 
      box(
        title = "Controls",
        sliderInput("slider", "Number of observations:", 1, 100, 50)
      )
    )
  )
)
# when the server is called upon by system, will generate normalized data and plot it
# further, every interaction with the user will rerun this set of code generating new data
server <- function(input, output) {
  set.seed(122)
  histdata <- rnorm(500)
  output$plot1 <- renderPlot({
    data <- histdata[seq_len(input$slider)]
    hist(data)
  })
}
# start the application - this calls upon the browser to get output for display
shinyApp(ui, server)  

The UI file only establishes the layout of the page:

library(shinydashboard)
# only calls upon shiny to create a dashboard page with header, sidebar and body
# each of these components is defined in the preceding app.R code.
dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody()
)  

If we run the app.R program in RStudio we end up with a page that looks like the following. There is a header section, sidebar, and body. The body contains the display and the slider control. We can run the application by selecting the app.R file in the source window and then clicking on the Run App button at the top of the source window. Clicking on the Run App button has the effect of entering the command runApp('shiny') in the console window (so you could have done that yourself instead). Once the application is running you will see a message in the console window such as Listening on http://127.0.0.1:6142 where R decided to deploy your application on your machine.

In the resultant new browser window we can see our Shiny application displayed:

Note that the Shiny display is responsive. Responsive web applications will adjust the layout of components to accommodate changes in the physical size of the window. As in the previous display, you would expect the display to have the components laid out horizontally as they occur. As I changed the dimensions of the display to fit the printed page the layout got reorganized.

The page is fully active. As you move the slider the graphic gets regenerated.

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

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