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.

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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%")}

else

{

ob_relative=round((ob_absolut/100)/(8/100),1)

return(paste0(ob_relative,"%"));

}

}

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:

4 Comments

Nice app but there are edge-cases that aren't accounted for such as when the BMI is exactly 25. In this case, it falls between the 25 cutoffs and so an error is thrown. Similar things happen if the BMI is exactly 30.