Monitoring Diabetes’ risk and BMI thanks to a Shiny dashboard

[This article was first published on MilanoR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Hi everyone and welcome back to our blog!
Valentine’s day has come and I guess many of you have eaten a lot of sweets during these days, so it’s the right time for a health check; we’ve got you covered, with a touch of r-based magic!

A little backstory: R-lab in 2018

In January 2018 i joined MilanoR, a community dedicated to bring together local R-users, aiming to share knowledge, best practice and good times with everyone who wants to get involved, at all skill levels; you can know more about the project here.

Between all the event formats they experimented, the most interesting are the R-labs. An R-lab is a non-competitive workshop, where everyone works together through a common effort, be it the development of a Shiny dashboard, optimizing an existing one, or simply helping the main guest solving a business problem with R.

Cool, isn’t it? Check out some of the previous events on our blog!

During our January R-lab, we met Riccardo Rossi, computational biologist and bioinformatics facility manager at INGM.

Showing us the already existing medical guidelines to assess risks of obesity, type two diabetes, hypertension and cardiovascular health, he invited us to build a Shiny app to allow people to keep in check their health status, just by entering some key parameters, such as height, weight and age.

Creating the dashboard

After meeting Riccardo, i embraced this challenge and started thinking; how could i translate medical guidelines, expressed as formulas, into an easy, working piece of R code?

Assessing the risk: server functions

My first goal was to build two functions, one for assessing risk of obesity and the second one to assess the risk of type 2 diabetes.

For the sake of simplcity, i’ll only show the first one:


obesity_risk <- function(weight, height, gender){
    bmi_2 = weight/(height^2)
    if (gender == "female" && bmi_2 < 25){ob_absolut = 1}
    if (gender == "female" && bmi_2 > 25 & bmi_2 < 30){ob_absolut = 19.5}
    if (gender == "male" && bmi_2 < 25){ ob_absolut = 1}
    if (gender == "male" && bmi_2 > 25 & bmi_2 < 30){ob_absolut = 13}
    if (bmi_2 > 30) {return("100%")}
      ob_relative = round((ob_absolut/100)/(8/100),1)    

Following the provided guidelines, this function calculates the user’s BMI, and returns the relative obesity risk. The other one does the same to assess the risk of contracting type 2 diabetes.

Interacting with the user

What’s an app without an user interacting with it? Front End time!
Using shiny and shinyDashboard libraries i designed a user-friendly interface to allow people to enter the needed personal data:

  • age
  • gender
  • weight
  • height
  • waist
  • lifestyle habits

 dashboardHeader(title = "Hi, I'm Doctor Thomas!", titleWidth = 300),
 dashboardSidebar(disable = TRUE),

     box(title = "Your data", solidHeader = TRUE, width = 3, status = "primary",
         radioButtons("gender", "What's your gender?", choices = c("male", "female")),
         numericInput("age", "How old are you?",25),
         numericInput("weight", "What is your weight (Kilos)?",70),
         numericInput("height", "What is your height (Meters) ?",1.7),
         numericInput("waist", "What is your waist size?",70), 
         radioButtons("hypdrugs", "Do you take hypertension drugs?", choices = c("Yes", "No"))

Enough with the code, show me the dashboard!


The full working app is hosted here, let us know what you think about it.
If you’re interested in the full code i will upload it on github and edit this post. See you at the next meetup!

The post Monitoring Diabetes’ risk and BMI thanks to a Shiny dashboard appeared first on MilanoR.

To leave a comment for the author, please follow the link and comment on their blog: MilanoR. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)