Conversion tool with Shiny

B. Schwenk
18-10-2018

Introduction

This presentation was made as a final assignment in the Developing Data Products class. It describes how the shiny of UI/Server principles works by showing coding examples from the energy conversion tool I created. For the presentation I used different font types and changed some text sizes and colors.

The app itself is available via this link: https://swenki.shinyapps.io/conversion/.
Please try the app and follow the instructions inside the Shiny app!

The presentation contains the following slides about the shiny concepts used:

  • Overview of the application workings
  • Defining inputs in ui.R
  • Server calculations in server.R

Overview of the application

The application is made with R Shiny. This enables server side processing. There are two files:

  • ui.R
  • server.R

The ui.R contains the user interface definitions.
The server.R contains the server processing.

When the calculate button is pressed, a signal is sent to the server.R script which processes the inputs (selected units and amount). The outputs are referenced in ui.R to show the results.

Defining inputs in ui.R

Below a part of the shiny code is shown. We can show inputbox for selecting a unit and number on the screen with code below. It enables the user to choose a unit to convert from and to set the amount interactivly.

ui<-shinyUI(fluidPage(
      titlePanel("Conversion of natural gas energy units"),
      selectInput(inputId = "fromUnit",
                                  label = "From unit:",
                                  choices = c("MJ", "m3", "kWh")),
      numericInput(inputId = "fromAmount",label="Amount:",value=100,step=1))
    )
)

The inputs fromUnit and fromAmount are referenced in the server.R file to do some processing. Let say, we choose in this example the conversion: 100MJ to m3 and load these into R. In the app this assignment is done on the fly by the shiny code. Also the outputs from the server processing are defined in ui.R (not shown in this presentation).

Server calculations in server.R

When the inputs change these can be referenced in the server.ui by e.g. input$fromUnit. In the following example I calculate the results using RenderText() (note: this is only part a of the code to demonstrate the concept).

    output$resultingAmount <- renderText({

    #do the conversions from --> to
    if(fromUnit=="MJ" and toUnit=="m3"){toAmount <- amount/35.17} 
    toAmount <- round(toAmount, digits=3)
    paste(toAmount,toUnit)
    })
[1] "2.843 m3"

This value is transmitted back to ui.R via the output$resultingAmount value and displayed via textOutput() function.