3 Translating R codes into Shiny

3.1 UI and Server

We will put the 3 inputs and 3 outputs above into an interactive Shiny app. Here, we create an User Interface (UI) object named as “my_ui” and a Server Logic object named as “my_server” and pass them into function shinyApp(). You can run this in R or Rstudio or deploy it as an online application.

Server Logic codes

Notes on Server

  • Shiny server logic is a function to define relationships between input and output objects (also this often includes a session object).
  • input items are accessed by input$input_item_name
  • output items are defined by output$output_item_name
  • session creates a specific instance of the application for each user
  • reactive({}): function to create a reactive object with respect to input contents
  • { } brackets are used in R to bundle multiple evaluation lines (called expression)
  • To render outputs, we use the corresponding types of rendering functions
    • rendrPrint()
    • renderText()
    • renderUI()

Live app is here.

Here is a screen shot.

3.2 Reactivity

Reactivity makes an online application interactive. As the user changes inputs, the server can return updated calculation results.

  • For most purposes, it suffices to think
    • input as a source of trigger for reactivity
    • ouput as an endpoint of the reaction
    • Learn more: reactivity-overview

In the above example, if a user changes the value of input$num_feeders, this sends a signal to update all output items (visible on the webpage) that are connected to input$num_feeders. This updates calves_total, which then updates output$calves_total and output$calves_yr.

  • Another common way to trigger reactivity is via
    • observe({...}): react to changes in any input$inpute_item in …
    • observeEvent(input$input_trigger, {...}): react to changes in input$input_trigger

For example, the above example can be modified, so that it only reacts when a button named “Update” is pressed.

  • Notes on this modification
    • reactiveValues(): creates a reactive value object (list)
    • inside observeEvent(), reactive updates are specified
    • reactive values are accessed for rendering by reactivevlaues_object$item_name

3.3 Inputs

  • Input types included in shiny package, for example
    • numericInput()
    • actionButton()
    • checkboxInput()
    • dateInput()
    • selectInput()

3.4 Outputs

  • Output types and associated rendering functions included in shiny package, for example

There are a variety of ways to include tables, figures, and interactive HTML objects. Here are some examples;

Interactive datatables via DT

Figures generated by ggplot2

HTML widgets

Various HTML widgets can be integrated in Shiny apps. Here is an example;

3.5 Layout

Once you have input and output, you can arrange them in any way. Typical layout templates include the following;

Dashboard layout

3.6 Deploy as Online Application

If you use RStudio, there is a publish button at the top-right corner of the script editor. It is possible to setup a Shiny Server Open Source for free, but you have to set up and maintain the server by yourself.

The easiest way is to use Rstudio’s cloud deployment service called Shinyapps.io. It lets you deploy your application at various scales, starting at $0 for testing purposes and $9/month for small-scale applications. Learn more.

Here is Shinyapps.io’s pricing as of May 2018;