Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- report.Rmd +44 -0
- server.R +145 -0
- ui.R +28 -0
report.Rmd
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: "BioAge Report"
|
| 3 |
+
output: html_document
|
| 4 |
+
params:
|
| 5 |
+
name: "User"
|
| 6 |
+
age: 30
|
| 7 |
+
bioage: 30
|
| 8 |
+
gender: "Male"
|
| 9 |
+
smoking_status: "No"
|
| 10 |
+
bmi: 25
|
| 11 |
+
waist_circumference: 80
|
| 12 |
+
date: "`r Sys.Date()`"
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# BioAge Report for `r params$name`
|
| 16 |
+
|
| 17 |
+
## Personal Information
|
| 18 |
+
|
| 19 |
+
- **Name**: `r params$name`
|
| 20 |
+
- **Gender**: `r params$gender`
|
| 21 |
+
- **Chronological Age**: `r params$age` years
|
| 22 |
+
- **Smoking Status**: `r params$smoking_status`
|
| 23 |
+
- **BMI**: `r params$bmi`
|
| 24 |
+
- **Waist Circumference**: `r params$waist_circumference` cm
|
| 25 |
+
|
| 26 |
+
## BioAge Calculation
|
| 27 |
+
|
| 28 |
+
Your biological age is calculated based on several factors.
|
| 29 |
+
Below are the results:
|
| 30 |
+
|
| 31 |
+
- **Chronological Age**: `r params$age` years
|
| 32 |
+
- **Calculated BioAge**: `r params$bioage` years
|
| 33 |
+
|
| 34 |
+
### Interpretation
|
| 35 |
+
|
| 36 |
+
```{r}
|
| 37 |
+
if (params$bioage <= params$age) {
|
| 38 |
+
result_message <- paste0("Congratulations, ", params$name, "! Your biological age is ", round(params$bioage, 2),
|
| 39 |
+
" years old, which is equal to or less than your chronological age. This indicates a healthy lifestyle.")
|
| 40 |
+
} else {
|
| 41 |
+
result_message <- paste0("Hi, ", params$name, ", your biological age is ", round(params$bioage, 2),
|
| 42 |
+
" years old, which is higher than your chronological age. Consider adopting healthier lifestyle habits to improve your biological age.")
|
| 43 |
+
}
|
| 44 |
+
result_message
|
server.R
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#Server.R
|
| 2 |
+
library(shiny)
|
| 3 |
+
library(rmarkdown)
|
| 4 |
+
library(knitr)
|
| 5 |
+
library(tinytex)
|
| 6 |
+
|
| 7 |
+
# Function: Biological Age Calculation
|
| 8 |
+
calculate_bioage <- function(age, gender, smoking_status, bmi, waist_circumference) {
|
| 9 |
+
bioage <- age + (bmi * 0.1) + (ifelse(smoking_status == "Yes", 5, 0))
|
| 10 |
+
return(bioage)
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
# Function: Validate User Inputs
|
| 14 |
+
validate_inputs <- function(input) {
|
| 15 |
+
validate(
|
| 16 |
+
need(input$age > 0, "Age must be positive"),
|
| 17 |
+
need(input$bmi > 0, "BMI must be positive"),
|
| 18 |
+
need(input$waist_circumference > 0, "Waist circumference must be positive")
|
| 19 |
+
)
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# Function: Show Modal Dialog starting
|
| 23 |
+
show_bioage_modal <- function(name, age, bioage) {
|
| 24 |
+
# Determine the message based on BioAge vs Chronological Age.
|
| 25 |
+
message <- if (bioage < age) {
|
| 26 |
+
paste0(
|
| 27 |
+
name, ", your BioAge is ", round(bioage, 2),
|
| 28 |
+
" years, which is lower than your Chronological Age. Great job maintaining healthy habits!"
|
| 29 |
+
)
|
| 30 |
+
} else if (bioage > age) {
|
| 31 |
+
paste0(
|
| 32 |
+
name, ", your BioAge is ", round(bioage, 2),
|
| 33 |
+
" years, which is higher than your Chronological Age. Consider focusing on healthier lifestyle choices."
|
| 34 |
+
)
|
| 35 |
+
} else {
|
| 36 |
+
paste0(
|
| 37 |
+
name, ", your BioAge matches your Chronological Age, indicating balanced health markers."
|
| 38 |
+
)
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
# Modal dialogue starting
|
| 42 |
+
showModal(
|
| 43 |
+
modalDialog(
|
| 44 |
+
title = tagList(h3(paste0(name, "'s BioAge"))),
|
| 45 |
+
div(
|
| 46 |
+
style = "text-align: center; margin-top: 20px;",
|
| 47 |
+
fluidRow(
|
| 48 |
+
column(5, div(
|
| 49 |
+
p("Chronological Age", style = "font-weight: bold; font-size: 16px;"),
|
| 50 |
+
p(paste(age, "years"), style = "font-size: 24px; font-weight: bold; color: #4CAF50;")
|
| 51 |
+
)),
|
| 52 |
+
column(2, div(
|
| 53 |
+
p("→", style = "font-size: 32px; font-weight: bold; color: #888; margin-top: 25px;")
|
| 54 |
+
)),
|
| 55 |
+
column(5, div(
|
| 56 |
+
p("BioAge", style = "font-weight: bold; font-size: 16px;"),
|
| 57 |
+
p(paste(round(bioage, 2), "years"), style = "font-size: 24px; font-weight: bold; color: #2196F3;")
|
| 58 |
+
))
|
| 59 |
+
)
|
| 60 |
+
),
|
| 61 |
+
div(
|
| 62 |
+
style = "text-align: center; margin-top: 20px;",
|
| 63 |
+
p(message, style = "font-size: 16px; font-weight: bold;")
|
| 64 |
+
),
|
| 65 |
+
div(
|
| 66 |
+
style = "text-align: center; margin-top: 10px;",
|
| 67 |
+
p("Complete report sent to your WhatsApp", style = "font-size: 14px;"),
|
| 68 |
+
p("xxxxxxxx4321", style = "font-size: 14px; color: #888;")
|
| 69 |
+
),
|
| 70 |
+
easyClose = TRUE
|
| 71 |
+
)
|
| 72 |
+
)
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# Pdf report generation block
|
| 77 |
+
generate_report <- function(params, output_file) {
|
| 78 |
+
src <- normalizePath('report.Rmd') # Ensure 'report.Rmd' exists in your project directory
|
| 79 |
+
|
| 80 |
+
# Copy Rmd file to a temporary directory ( using tinytex not LaTex) which is lite weight.
|
| 81 |
+
owd <- setwd(tempdir())
|
| 82 |
+
on.exit(setwd(owd))
|
| 83 |
+
|
| 84 |
+
file.copy(src, 'report.Rmd', overwrite = TRUE)
|
| 85 |
+
|
| 86 |
+
# Render the report with the provided parameters in the form entries
|
| 87 |
+
rmarkdown::render(
|
| 88 |
+
input = 'report.Rmd',
|
| 89 |
+
output_file = output_file,
|
| 90 |
+
output_format = 'pdf_document',
|
| 91 |
+
envir = new.env(parent = globalenv()) # Use a new environment to avoid conflicts
|
| 92 |
+
)
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
# Server Logic
|
| 96 |
+
server <- function(input, output, session) {
|
| 97 |
+
observeEvent(input$calculate, {
|
| 98 |
+
validate_inputs(input)
|
| 99 |
+
|
| 100 |
+
bioage <- calculate_bioage(
|
| 101 |
+
age = input$age,
|
| 102 |
+
gender = input$gender,
|
| 103 |
+
smoking_status = ifelse(input$smoker, "Yes", "No"),
|
| 104 |
+
bmi = input$bmi,
|
| 105 |
+
waist_circumference = input$waist_circumference
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Pass required arguments explicitly
|
| 109 |
+
show_bioage_modal(
|
| 110 |
+
name = input$name,
|
| 111 |
+
age = input$age,
|
| 112 |
+
bioage = bioage
|
| 113 |
+
)
|
| 114 |
+
})
|
| 115 |
+
|
| 116 |
+
output$downloadReport <- downloadHandler(
|
| 117 |
+
filename = function() {
|
| 118 |
+
paste("bioage_report_", Sys.Date(), ".pdf", sep = "")
|
| 119 |
+
},
|
| 120 |
+
content = function(file) {
|
| 121 |
+
tryCatch({
|
| 122 |
+
generate_report(
|
| 123 |
+
params = list(
|
| 124 |
+
name = input$name,
|
| 125 |
+
age = input$age,
|
| 126 |
+
bioage = calculate_bioage(
|
| 127 |
+
age = input$age,
|
| 128 |
+
gender = input$gender,
|
| 129 |
+
smoking_status = ifelse(input$smoker, "Yes", "No"),
|
| 130 |
+
bmi = input$bmi,
|
| 131 |
+
waist_circumference = input$waist_circumference
|
| 132 |
+
)
|
| 133 |
+
),
|
| 134 |
+
output_file = file
|
| 135 |
+
)
|
| 136 |
+
}, error = function(e) {
|
| 137 |
+
showModal(modalDialog(
|
| 138 |
+
title = "Error",
|
| 139 |
+
paste("Failed to generate the report:", e$message),
|
| 140 |
+
easyClose = TRUE
|
| 141 |
+
))
|
| 142 |
+
})
|
| 143 |
+
}
|
| 144 |
+
)
|
| 145 |
+
}
|
ui.R
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#ui.R
|
| 2 |
+
library(shiny)
|
| 3 |
+
|
| 4 |
+
ui <- fluidPage(
|
| 5 |
+
titlePanel("BioAge Calculator"),
|
| 6 |
+
sidebarLayout(
|
| 7 |
+
sidebarPanel(
|
| 8 |
+
h4("Enter Your Details"),
|
| 9 |
+
textInput("name", "Name", placeholder = "Enter your full name"),
|
| 10 |
+
numericInput("age", "Chronological Age (years)", value = 30, min = 0, step = 1),
|
| 11 |
+
radioButtons("gender", "Gender", choices = c("Male", "Female"), inline = TRUE),
|
| 12 |
+
checkboxInput("smoker", "Smoker", value = FALSE),
|
| 13 |
+
numericInput("bmi", "BMI (Body Mass Index)", value = 25, min = 0, step = 0.1),
|
| 14 |
+
numericInput("waist_circumference", "Waist Circumference (cm)", value = 80, min = 0, step = 1),
|
| 15 |
+
actionButton("calculate", "Calculate BioAge", class = "btn-primary"),
|
| 16 |
+
br(),
|
| 17 |
+
br(),
|
| 18 |
+
downloadButton("downloadReport", "Download Report", class = "btn-success")
|
| 19 |
+
),
|
| 20 |
+
mainPanel(
|
| 21 |
+
h3("Result"),
|
| 22 |
+
p(
|
| 23 |
+
"Once you calculate your BioAge, a summary will appear in a popup window. ",
|
| 24 |
+
"Additionally, you can download a detailed report as a PDF using the button above."
|
| 25 |
+
)
|
| 26 |
+
)
|
| 27 |
+
)
|
| 28 |
+
)
|