#app.R library(shiny) library(BioAge) library(dplyr) #library(RSQLite) library(googlesheets4) library(googledrive) library(shinyvalidate) library(shinyjs) library(plotly) library(lubridate) library(pander) library(tidyr) library(shiny.router) #options(gargle_oauth_cache = ".secrets") createReactiveDataset <- function() { return(reactiveVal(data.frame(date = as.Date(character()), quantity = integer(), item = character()))) } # Define dataset and its reactive variable dataset <- createReactiveDataset() # Dummy dataset for development localDataset <- reactiveVal(data.frame( date = as.Date(c('2024-12-01', '2024-12-02')), quantity = c(10, 20), item = c('Apples', 'Oranges') )) # For local development, bypass authentication isLocal <- TRUE home_route <- route("/", function() { fluidPage( h1("Home Page"), DT::dataTableOutput("dataTable") ) }) checkRange <- function(value, min, max){ if(!is.na(value) & (value < min || value > max)){ paste0("Please specify a number that is between ", min, " and ", max, ", thank you!") } else { "" } } search_string = "" query_params <- parseQueryString(search_string) #if !is.null() emiglio = query_params$useremail nomignolo = paste0(query_params$firstname, " ", query_params$lastname) phone_prefix_regex <- "^\\+?\\d{1,3}$" phone_number_regex <- "^[0-9]{10,10}$" validatePhone <- function(value, n_char_min){ if(value != ""){ if(grepl("^[0-9]{1,100}$", value)){ if (nchar(value) == n_char_min) { "" } else{ paste0("Please specify a number that contains ", n_char_min, " digits") } }else{ "Please enter only digits" } } else{ "" } } ui <- fluidPage( tags$head( tags$link(rel = "stylesheet", href = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css"), tags$style(HTML(" .control-label { color: black !important; } .strongy{ background-color: #c8e6c9; padding: 10px } .form-control{ border-color: grey !important } #loading { display: none; text-align: center; } .loader { border: 6px solid #f3f3f3; /* Light grey */ border-top: 6px solid #3498db; /* Blue */ border-radius: 50%; width: 50px; height: 50px; animation: spin 2s linear infinite; margin: 20px auto; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } ")) ), useShinyjs(), titlePanel("Biological Age Calculator"), sidebarLayout( sidebarPanel( fluidRow( column(6, textInput("Name", "Enter name", query_params$firstname)), column(6, textInput("Surname", "Enter your last name", query_params$lastname)) ), fluidRow( column(6, textInput("phone_prefix", "Enter Country Code", "+91")), column(6, textInput("phone_number", "Enter your phone number", "")) ), dateInput("dob", "Date of Birth*", value="1990-01-01"), dateInput("bloodTestDate", "Date of TEST", value = Sys.Date()), numericInput("albumin", "Albumin (g/dL)", value = NA, min = 0), verbatimTextOutput("values"), numericInput("lymph", "Lymphocytes (%)", value = NA, min = 0), numericInput("mcv", "Mean Cell Volume (MCV)", value = NA, min = 0), numericInput("glucose", "Glucose (mg/L)", value = NA, min = 0), numericInput("rdw", "Red Cell Dist Width (RDW)", value = NA, min = 0), numericInput("creat", "Creatinine (mg/dL)", value = NA, min = 0), numericInput("crp", "CRP (mg/L)", value = NA, min = 0), numericInput("alp", "Alkaline Phosphatase (U/L)", value = NA, min = 0), numericInput("wbc", "White Blood Cells (cells/mL)", value = NA, min = 0), actionButton("submit", "Submit", title = "Fill in the phone number field to enable") ), mainPanel( tabsetPanel( tabPanel("Calculator", div(id = "loading", class = "loader", style = "display: none"), # Loading spinner uiOutput("results"), uiOutput("message")), tabPanel("My historical data - Plot", uiOutput("notlogged"), actionLink("openLinkButton", "Create an account here / log in here", href = "", style = "background-color:#c8e6c9; padding: 10px; font-size: 2em"), plotOutput("biologicalAgePlot")) , tabPanel("My historical data - Table", tableOutput("table"), actionLink("openLinkButton", "Create an account here / log in here", href = "", style = "background-color:#c8e6c9; padding: 10px; font-size: 2em"), uiOutput("notlogged1")) ) ) ) ) server <- function(input, output, session) { sheet_id <- "https://docs.google.com/spreadsheets/d/1xQpghper_5FCWkYFByIpdVKFmBofgM7uVrG_bsaGW58/edit?gid=0#gid=0" values <- reactiveVal(data.frame()) router_server( list(home_route) ) # Dummy dataset #dataset <- data.frame(date = as.Date(c('2024-12-01', '2024-12-02')), #quantity = c(10, 20), #item = c('Apples', 'Oranges')) # Render the dataset in a table #output$dataTable <- DT::renderDataTable({ #dataset #}) observeEvent(input$submit, { shinyjs::show(id = "loading") # Show loading spinner # Collecting inputs name <- paste0(input$Name, " ", input$Surname) ageAtTestDate <- round(as.numeric(difftime(Sys.Date(), input$dob, units = "days")) / 365.25, 1) biological_age <- runif(1, min = ageAtTestDate - 5, max = ageAtTestDate + 5) # Simulated calculation bioage_color <- if (biological_age < ageAtTestDate) "green" else if (biological_age > ageAtTestDate) "red" else "yellow" # Show the BioAge Modal show_bioage_modal(name, ageAtTestDate, biological_age, bioage_color) output$results <- renderUI({ paste("Thank you for submitting your details. Your biological age will be calculated based on the provided data.") }) }) # Define show_bioage_modal function outside observeEvent show_bioage_modal <- function(name, ageAtTestDate, biological_age, bioage_color) { diff <- round(abs(biological_age - ageAtTestDate), 2) message <- if (biological_age < ageAtTestDate) { tags$p( paste0( name,", your healthy choices show! your BioAge is ", diff, " years younger than your calendar Age." ), style = "padding-bottom: 24px; font-size: 16px; font-weight: bold; font-family: sans-serif; font-variant: contextual;" ) } else if (biological_age > ageAtTestDate) { tags$p( paste0( name,", your BioAge is ", diff, " years higher than your Calendar Age. Don’t worry—small, consistent steps in the right direction can help you close the gap!" ), style = "padding-bottom: 24px; font-size: 16px; font-weight: bold; font-family: sans-serif; font-variant: contextual;" ) } else { tags$p( paste0( name,", your BioAge matches your Calendar Age, indicating balanced health markers." ), style = "padding-bottom: 24px; font-size: 16px; font-weight: bold; font-family: sans-serif; font-variant: contextual;" ) } showModal( modalDialog( title = NULL, div( style = "background-color: #0E406B; color: white; padding: 24px; text-align: center; border-top-left-radius: 5px; border-top-right-radius: 5px; margin: -15px -15px 0 -15px; font-family: 'Archia', sans-serif;", h3( paste0(name,"'s BioAge Results"), style = "margin: 0; padding: 0; font-size: 26px; text-align: center; font-family: 'Archia', sans-serif; font-variant: common-ligatures; color: white; line-height: 1;" ) ), div( style = "background-color: white; text-align: center; font-family: 'Archia', sans-serif; padding-top: 20px;", fluidRow( column(5, div( tags$p("Calendar Age", style = "font-weight: bold; font-size: 20px; color: black;"), tags$p(paste(ageAtTestDate, "years"), style = "font-size: 24px; font-weight: bold; color: black;") )), column(2, div( tags$p("→", style = "font-size: 59px; font-weight: bold; color: darkblue; margin-top: 8px;") )), column(5, div( tags$p("BioAge", style = "font-weight: bold; font-size: 20px; color: black;"), tags$p( paste(round(biological_age, 2), "years"), style = sprintf("font-size: 24px; font-weight: bold; color: %s;", bioage_color) ) )) ) ), div( style = "text-align: center; margin-top: 20px; color: #333333; margin-bottom: 0px;", message ), div( style = "text-align: center;", tags$p("Complete report sent to your WhatsApp", tags$i(class = "fa-brands fa-whatsapp fa-beat", style = "color: #25D366; margin-left: 5px; font-size: 16px;"), style = "font-size: 14px; color: black; margin-bottom: 0px;"), tags$p("xxxxxxxx4321", style = "font-size: 14px; color: black; margin-bottom: -16px;") ), easyClose = FALSE, footer = tags$button( "Close", onclick = "Shiny.setInputValue('close_modal', true);", style = "background-color: #86D1F1; color: black; border: none; padding: 10px 20px; border-radius: 5px; font-size: 16px; cursor: pointer;" ) ) ) } output$message <- renderUI({ HTML("
Biological age refers to how old a person seems based on the functioning and condition of their body systems, rather than the time since birth (chronological age). It is influenced by genetics, lifestyle, and environmental factors, and can provide a more accurate representation of an individual's health and longevity prospects.
We have based this calculation of your biological age on scientific data from the National Health and Nutrition Examination Survey (NHANES). The package uses published biomarker algorithms to calculate three biological aging measures: Klemera-Doubal Method (KDM) biological age, phenotypic age, and homeostatic dysregulation.
Kwon, D., Belsky, D.W. A toolkit for quantification of biological age from blood chemistry and organ function test data: BioAge. GeroScience 43, 2795–2808 (2021).
These results are solely for informational use and shouldn't replace professional medical advice, diagnosis, or treatment. Always seek the guidance of a healthcare provider for any medical conditions or before making changes to your health regimen, including starting new diets, exercises, or supplements, or altering medication. Never discontinue medication or follow any health advice without consulting your healthcare provider.
Your biological age is ", round(biological_age, 1), " years
", "Your actual age is ", ageAtTestDate, " years