BMI Calculation & Classification

Parthiban Malan
Sun May 24 17:20:53 2015

Index

  • About BMI App
  • UI Code
  • Server Code

About BMI App

This is a shiny application to calcuate the Body Mass Index (BMI). After calculating the BMI, it also returns the BMI class.

One has to input weight in Kilograms and height in Metres to get the BMI and class.

BMI is caluclated as weight / (height * height)

UI Code

# This is the user-interface definition of a Shiny web application.
# With this application one can find the Body Mass Index and the classification.
#

library(shiny)

shinyUI
(
  pageWithSidebar
  (
    headerPanel("Body Mass Index - Calculator & Classifier"),
    sidebarPanel
    (
      h4('This application calculates your Body Mass Index and show the result of the classification.'),
      h4('Please provide the following details.'),
      sliderInput("weight", 'Weight in Kilograms', 0, min=0, max=200, step=0.25),
      sliderInput("height", 'Height in Metres', 0, min=0, max=3, step=0.01),
      submitButton("Submit")
    ),
    mainPanel
    (
      h4('Your Body Mass Index is '),
      verbatimTextOutput("obmivalue"),
      h4('And you are classifed as '),
      verbatimTextOutput("obmiclass")
    )
  )
)

Server Code

# This is the user-interface definition of a Shiny web application.
# With this application one can find the Body Mass Index and the classification.
#

library(shiny)

bmi.value <- function(weight, height) 
{
  if(height > 0)
    weight / (height * height)
}

bmi.class <- function(bmi) 
{
  bmi.df <- read.csv("BMI.csv")
  bmi.df.filtered <- subset(bmi.df, Min <= bmi & Max > bmi, select = c(Classification))
  as.character(bmi.df.filtered$Classification)
}

shinyServer
(
  function(input, output)
  {
    output$obmivalue <- renderText({bmi.value(input$weight, input$height)})
    output$obmiclass <- renderText({bmi.class(bmi.value(input$weight, input$height))})
  }
)