Simple Calculator (Developing Data Products - Week 4 Project)

Ngwoke Chukwubuikem
28 September, 2020

Overview

This is an RStudio shiny application developed as a part of final project in the Developing Data Products course in Coursera Data Science Specialization track. The application is a simple calculator

The project includes:

  • A number input to input the principal amount
  • A drop down menu to select arithmetic operation
  • An action button to run the calculation

This app is hosted at: https://chu-ngwoke.shinyapps.io/calculator/

Operations inside the Shiny App and the Output

The reactivity of the shiny application widgets is controlled by using an Action Button. Based on user inputs, and using the simple arithemtic, the application displays the result of the calculation,

To make it easy for a novice user, a Documentation tab in the Main Panel of the application lists the details of the calculation of simple interest.

Application - ui.R

library(shiny)

shinyUI(fluidPage(
    titlePanel("Simple Calculator"),
    sidebarLayout(
        sidebarPanel(
            helpText("This app is a basic arithmetic calculator."),            
            numericInput("num_1", label = h6("Enter your first number"), value = 100),
            numericInput("num_2", label = h6("Enter your second number"), value = 100),
            selectInput("select_operation", label = h6(""), choices = c("*", "/", "+", "-")),

            actionButton("action_Calc", label = "Refresh & Calculate")        
        ),

        mainPanel(
            tabsetPanel(
                tabPanel("Output",
                        h3("The result is:"),
                        textOutput("output")
                ),
                tabPanel("Documentation",
                         p(h4("Simple Calculator:")),
                         helpText("This application calculates basic arithmetic on any two numbers, i.e. division, multiplication, addition or subtraction."),

                )
            )
        )
    )
))

Application - server.R

library(shiny)

shinyServer(function(input, output) {
    observe({input$action_Calc
        if (input$action_Calc == 0) "" 
        else output$output <- renderText({
            switch(input$select_operation,
                   "*" = input$num_1 * input$num_2,
                   "/" = input$num_1 / input$num_2,
                   "+" = input$num_1 + input$num_2,
                   "-" = input$num_1 - input$num_2)   
        })

    })
}
    )