Spaces:
Sleeping
Sleeping
File size: 4,487 Bytes
5b962ea |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
#Server.R
library(shiny)
library(rmarkdown)
library(knitr)
library(tinytex)
# Function: Biological Age Calculation
calculate_bioage <- function(age, gender, smoking_status, bmi, waist_circumference) {
bioage <- age + (bmi * 0.1) + (ifelse(smoking_status == "Yes", 5, 0))
return(bioage)
}
# Function: Validate User Inputs
validate_inputs <- function(input) {
validate(
need(input$age > 0, "Age must be positive"),
need(input$bmi > 0, "BMI must be positive"),
need(input$waist_circumference > 0, "Waist circumference must be positive")
)
}
# Function: Show Modal Dialog starting
show_bioage_modal <- function(name, age, bioage) {
# Determine the message based on BioAge vs Chronological Age.
message <- if (bioage < age) {
paste0(
name, ", your BioAge is ", round(bioage, 2),
" years, which is lower than your Chronological Age. Great job maintaining healthy habits!"
)
} else if (bioage > age) {
paste0(
name, ", your BioAge is ", round(bioage, 2),
" years, which is higher than your Chronological Age. Consider focusing on healthier lifestyle choices."
)
} else {
paste0(
name, ", your BioAge matches your Chronological Age, indicating balanced health markers."
)
}
# Modal dialogue starting
showModal(
modalDialog(
title = tagList(div(style = "text-align: center;",h3(paste0(name, "'s BioAge")))),
div(
style = "text-align: center; margin-top: 20px;",
fluidRow(
column(5, div(
p("Chronological Age", style = "font-weight: bold; font-size: 16px;"),
p(paste(age, "years"), style = "font-size: 24px; font-weight: bold; color: #4CAF50;")
)),
column(2, div(
p("→", style = "font-size: 32px; font-weight: bold; color: #888; margin-top: 25px;")
)),
column(5, div(
p("BioAge", style = "font-weight: bold; font-size: 16px;"),
p(paste(round(bioage, 2), "years"), style = "font-size: 24px; font-weight: bold; color: #2196F3;")
))
)
),
div(
style = "text-align: center; margin-top: 20px;",
p(message, style = "font-size: 16px; font-weight: bold;")
),
div(
style = "text-align: center; margin-top: 10px;",
p("Complete report sent to your WhatsApp", style = "font-size: 14px;"),
p("xxxxxxxx4321", style = "font-size: 14px; color: #888;")
),
easyClose = TRUE
)
)
}
# Pdf report generation block
generate_report <- function(params, output_file) {
src <- normalizePath('report.Rmd') # Ensure 'report.Rmd' exists in your project directory
# Copy Rmd file to a temporary directory ( using tinytex not LaTex) which is lite weight.
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
# Render the report with the provided parameters in the form entries
rmarkdown::render(
input = 'report.Rmd',
output_file = output_file,
output_format = 'pdf_document',
envir = new.env(parent = globalenv()) # Use a new environment to avoid conflicts
)
}
# Server Logic
server <- function(input, output, session) {
observeEvent(input$calculate, {
validate_inputs(input)
bioage <- calculate_bioage(
age = input$age,
gender = input$gender,
smoking_status = ifelse(input$smoker, "Yes", "No"),
bmi = input$bmi,
waist_circumference = input$waist_circumference
)
# Pass required arguments explicitly
show_bioage_modal(
name = input$name,
age = input$age,
bioage = bioage
)
})
output$downloadReport <- downloadHandler(
filename = function() {
paste("bioage_report_", Sys.Date(), ".pdf", sep = "")
},
content = function(file) {
tryCatch({
generate_report(
params = list(
name = input$name,
age = input$age,
bioage = calculate_bioage(
age = input$age,
gender = input$gender,
smoking_status = ifelse(input$smoker, "Yes", "No"),
bmi = input$bmi,
waist_circumference = input$waist_circumference
)
),
output_file = file
)
}, error = function(e) {
showModal(modalDialog(
title = "Error",
paste("Failed to generate the report:", e$message),
easyClose = TRUE
))
})
}
)
}
|