Wikki13 commited on
Commit
5b962ea
·
verified ·
1 Parent(s): 3da502d

Update server.R

Browse files
Files changed (1) hide show
  1. server.R +145 -145
server.R CHANGED
@@ -1,145 +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
- }
 
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(div(style = "text-align: center;",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
+ }