The making of a shiny mauc: chapter 2

[This article was first published on triKnowBits, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This continues last week’s post The making of a shiny mauc, based on Greg McNulty’s mauc blog. It utilizes the RStudio interface to R, Desktop version.

Recall, the goal is to make an online shiny app that will run Greg’s code using his data, all supplied in his post. Today we will address what modifications are necessary to show his first plot (below) on a web page. In a subsequent post we will see how to display all Greg’s plots. After that we will see how
to upload the app to an online server so anyone can run it in a browser, even if you don’t have R.

mauc’s first plot: let’s name this baby copula_data_plot

A shiny app essentially consists of two files
  • ui.r: defines the user-interface (UI), i.e., the “look” of the web page
  • server.r: the set of instructions that “services” the UI, i.e, takes input from the user (there is none in Greg’s paper) and generates the output. In our case, all servicing instructions are contained in Greg’s R script, “R code CAS blog v2.R”.
As long as those two files are contained in your working directory, and are well-formed, RStudio will be able to create your shiny app.

In step 1 we see how to define a UI that displays a plot named “copula_data_plot” (indeed, any ol’ plot named “copula_data_plot”). In step 2 we see where to insert Greg’s code and where to add two new lines so that the copula_data_plot above is served up specifically.

Step 1: ui.r – contains and defines the function ‘shinyUI’

Like a painter, we first envision what objects we are going to place on our canvas and where. This is called the “layout.” Since our “painting” will be of a single plot, our layout can be very simple. What do we put into shinyUI so as to define a simple, one plot layout?

The shiny Application layout guide discusses some of the basic layouts. Let’s use the first one there, the Sidebar Layout, which happens to display a plot in the Main Area. We currently have no inputs to receive from the Sidebar, so we will adopt the “# Inputs excluded for brevity” technique (from the second Sidebar Layout example on that page). Here is our ui.r file:

shinyUI(fluidPage(
  titlePanel(“Shiny mauc: Version 1”),
  sidebarLayout(
    sidebarPanel(
      # Inputs excluded for brevity
    ),
    mainPanel(
      plotOutput(“copula_data_plot”)
    )
  )
))

All this says is that our “canvas” consists of an (empty) area on the left called the sidebarPanel and an area to its right called the mainPanel. The only thing in the mainPanel will be the output of a plot. In fact, that plot must be named “copula_data_plot” or it won’t show. Put another way, whenever ui.r comes across a plot named “copula_data_plot”, then no matter what that plot looks like, it will display that plot right there in the mainPanel. Oh, and there’s a title at the top.

Step 2: server.r – contains and defines the function ‘shinyServer’

The point of most shiny apps is to “react to” user input, so in the shiny tutorial and elsewhere you will see lots of talk about your shiny app being “reactive.” Beh… not every app has to be “reactive.” Ours certainly won’t be — it’s simply going to take Greg’s input data and display copula_data_plot. We’re not going to make any changes whatsoever. Not change the input data, not fit a different distribution to the ALAE, not choose a different copula than the Gumbel .. Nada! There will be nothing “going on” to which server.r must react.

Therefore, per Step 2 of the shiny tutorial, when we “Provide R code to build the object” all we have to do is insert the entirety of Greg’s script, “R code CAS blog v2.R”, into server.r and that’s it! … Well, and add two small lines of code. But then, really, that’s it! ☺

First, we start off with an almost-blank server.r file that looks like this:
Open the “R code CAS blog v2.R” file, select all, copy to the clipboard, highlight the contents of line 2 above, and paste the clipboard contents into that spot.

Next, from Greg’s blog you will see that his first plot consists of these two lines
which send their output to R’s graphics device. “Painting onto a canvas” and “rendering onto a web page” mean the same thing, so to “render” copula_data_plot onto the mainPanel, we wrap those two lines within the shiny renderPlot function. As soon as that function’s result is assigned to an output object named (in R-speak, “$“) copula_data_plot

output$copula_data_plot <- renderPlot({Gregs's 2 lines go here})

ui.r will see it and recognize it as the plot to display in the mainPanel.

So place the two new lines you see below around Greg’s code:

save the server.r file, hit the “Run App” button
 and you will eventually see copula_data_plot


Notes:
  1. Close that browser window to return to RStudio, or in RStudio hit escape.
  2. The UI’s title panel is at the top, the empty sidebar panel is to the left, the main panel is to the right, and the only thing there is a plot.
  3. This app turns out to be “reactive” after all, in the sense that if you resize the window the objects magically reposition themselves. Thank you, Shiny!!
  4. The program’s remaining plots can be found in RStudio’s plot window, printed output can be found in RStudio’s console window, and the output csv files can be found in your working directory (getwd()). All those things happen because you are running the desktop version of the app through RStudio. What happens when you run the online version of the app through a browser? Stay tuned and let’s see!

That’s it for this week. Next week we will see how to display all Greg’s plots with our shiny mauc.

sessionInfo()
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Or when on my mac:
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)


locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                        
[5] LC_TIME=English_United States.1252

Or when on my mac
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods   base  

other attached packages:
[1] distr_2.5.3              SweaveListingUtils_0.6.2 sfsmisc_1.0-29        
[4] startupmsg_0.9           copula_0.999-14          actuar_1.1-10        
[7] shiny_0.12.1          

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.3       rstudioapi_0.5    ADGofTest_0.3     xtable_1.7-4  
 [5] pspline_1.0-17    lattice_0.20-33   R6_2.0.1          tools_3.2.3    
 [9] grid_3.2.3        htmltools_0.2.6   digest_0.6.9      Matrix_1.2-3  
[13] mime_0.3          gsl_1.9-10.1      stabledist_0.7-0  jsonlite_0.9.16
[17] mvtnorm_1.0-4     httpuv_1.3.2      rstudio_0.98.1103

To leave a comment for the author, please follow the link and comment on their blog: triKnowBits.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)