December 28, 2016

Introduce BMI

The Body mass index (BMI) is a measure of body fat based on height and weight that applies to adult men and women.

Regarding the BMI measure, the World Health Organization (WHO) proposes the following classification:

  • BMI <18.5 : Underweight
  • BMI [18.5-24.9] : Normal
  • BMI [25-29.9] : Overweight
  • BMI >=30 : Obese

UI code

library(shiny)
shinyUI(
    pageWithSidebar(
        headerPanel("Body Mass Index (BMI) Calculator"),
        sidebarPanel(
            numericInput('weight', 'kilograms', 70, width = 200),
            numericInput('height', 'meters', 1.70, width = 200),
            submitButton('Calculate your BMI')
        ),
        mainPanel(
            p('weight:'), verbatimTextOutput("inputweightvalue"),
            p('height:'), verbatimTextOutput("inputheightvalue"),
            p('Your BMI is:'),verbatimTextOutput("bmi"),
        )
    )

Server code

library(shiny)
BMI <- function(weight,height) {
    weight/(height^2)
}
shinyServer(
    function(input, output) {
        output$inputweightvalue <- renderPrint({input$weight})
        output$inputheightvalue <- renderPrint({input$height})

        output$bmi <- renderPrint({BMI(input$weight,input$height)})
    }
)

Thank you