-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
270 lines (230 loc) · 7.16 KB
/
server.R
File metadata and controls
270 lines (230 loc) · 7.16 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#load required packages
library(shiny)
library(ggplot2)
library(ggradar)
library(scales)
library(dplyr)
library(plotly)
library(janitor)
library(frisk)
#shiny server defintion
shinyServer(function(input, output, session) {
# render output text
output$txtout <- renderText({
paste(input$txt, input$slider, format(input$date), sep = ", ")
})
# render output table - testdata
output$table <- renderTable({
head(cars, 4)
})
# individual person plot
output$cvdRadarPlot <- renderPlot({
# calculate cvd for a single person
cvd_single <- cvd_single_person(input)
return(cvd_single$plot)
})
# render cvd table for single person tab
output$cvdOneTable <- renderTable({
# calculate cvd for a single person
cvd_single <- cvd_single_person(input)
return(head(cvd_single$data))
})
# render cvd text for single person tab
output$cvdOneText <- renderText({
# calculate cvd for a single person
cvd_single <- cvd_single_person(input)
resultData <- as.list(cvd_single$data)
# construct display message
paste('Dear', input$name,
'your estimated 10 years risk of having a Cardiovascular disease is',
resultData$risk, '%',
'Due to your risk factors, your heart age is:',
resultData$heart_age, 'years.', sep = " ")
})
# Population plot
output$cvdPopulationPlot <- renderPlotly({
if(input$populationTab=="simulation"){
# calculate cvd for a single person
cvd_population <- cvd_population(input)
return(cvd_population$plot)
} else {
return(plot_ly())
}
})
# upload data and render on table
output$cvdPopulationDT <- renderDataTable({
if(input$populationTab=="upload"){
return(uploadFile(input))
} else{
cvd_population <- cvd_population(input)
return(cvd_population$data)
}
},options = list(scrollX = TRUE))
})
#' @title cvd_single_person
#' @description
#' \code{cvd_single_person} returns relative risk and heart age
#' @param input A dataframe
#' @return list of data and plot
cvd_single_person <- function (input) {
# call cvd risk function
if(input$bmiTab=="nonBmi"){ # set if bmi value is used or not
# call cvd risk function
patentCvd <- calc_card_10_one(
age = input$"age",
gender = input$"gender",
bmi = NA,
cholesterol = input$"cholesterol",
hdl = input$"hdl",
sbp = input$"sbp",
is_sbp_under_treatment = input$"isSbpTreated",
smoking_status = input$"smoking_status",
diabetes_status = input$"diabetes_status"
)
} else {
# call cvd risk function
patentCvd <- calc_card_10_one(
age = input$"age",
gender = input$"gender",
bmi = input$"bmi",
cholesterol = NA,
hdl = NA,
sbp = input$"sbp",
is_sbp_under_treatment = input$"isSbpTreated",
smoking_status = input$"smoking_status",
diabetes_status = input$"diabetes_status"
)
}
print(head(patentCvd))
# clean data by removing empty columns
cleanPatentCvd <- as.data.frame(patentCvd) %>%
remove_empty_cols() %>%
subset(select = -c(risk, heart_age))
# outputCvd
outputCvd <- as.data.frame(patentCvd) %>%
remove_empty_cols() %>%
subset(select = c(points, risk, heart_age))
# radar plot of individual risk points
plot_radar <- ggradar(
cleanPatentCvd,
axis.labels = colnames(cleanPatentCvd)[-1],
centre.y = -5,
label.centre.y = TRUE,
grid.min = 0,
grid.mid = 8,
grid.max = 15,
values.radar = c("0", "8", "15"),
grid.line.width = 0.3,
grid.label.size = 4,
gridline.label.offset = 0,
axis.label.size = 3,
axis.line.colour = "gray40",
gridline.max.colour = "black",
plot.legend = TRUE,
legend.text.size = 10,
font.radar = "Arial"
)
return(list(plot = plot_radar,
data = outputCvd)
)
}
#' @title cvd_population
#' @description
#' \code{cvd_population} returns relative risk and heart age of a population
#' @param input A dataframe
#' @return list of data and plot
cvd_population <- function (input) {
sample_size <- input$sample_size
# simulate patients data
sample <- data.frame(age=sample(30:70,sample_size,rep=TRUE),
gender=sample(c("M","F"),sample_size,rep=TRUE),
bmi=sample(16:48, sample_size, rep = TRUE),
hdl=sample(10:100,sample_size,rep=TRUE),
chl=sample(100:400,sample_size,rep=TRUE),
sbp=sample(90:200,sample_size,rep=TRUE),
isSbpTreated=sample(c(TRUE,FALSE),sample_size,rep=TRUE),
smoking=sample(c(TRUE,FALSE),sample_size,rep=TRUE),
diabetes=sample(c(TRUE,FALSE),sample_size,rep=TRUE)
)
# call frisk function case no bmi
patients <-calc_card_10(sample, age="age", gender="gender", cholesterol="chl",
hdl="hdl", sbp="sbp", is_sbp_under_treatment="isSbpTreated",
smoking_status="smoking", diabetes_status="diabetes"
)
#plot the graph
plot_graph <- plot_ly(patients, x = ~chl, y = ~sbp, z = ~hdl,
marker = list(color = ~points,
colorscale = c('#FFE1A1', '#683531'),
showscale = T),
text = ~paste('SBP: ', sbp,
'</br> LDL: ', chl,
'</br> HDL: ', hdl)
) %>%
# add markers
add_markers() %>%
layout(
# add title
title = "Effects of hdl, chl and sbp on CVD Risk",
#Label x,y,z axis
scene = list(
aspectratio = list(
x = 1,
y = 1,
z = 1
),
# set 3d camera angle
camera = list(
center = list(
x = 0,
y = 0,
z = 0
),
# coordinates of the eye position
eye = list(
x = 1.96903462608,
y = -1.09022831971,
z = 0.405345349304
),
# configure box position
up = list(
x = 0,
y = 0,
z = 1
)
),
# drag mode
dragmode = "turntable",
# x axis label
xaxis = list(title = 'Cholesterol (LDL)'),
# y axis label
yaxis = list(title = 'Blood Pressure (SBP)'),
# z axis label
zaxis = list(title = 'Cholesterol (HDL)')),
# add annotation
annotations = list(
x = 1.13,
y = 1.05,
text = 'CVD Risk Scale',
xref = 'paper',
yref = 'paper',
showarrow = FALSE
))
return(list(plot = plot_graph,
data = patients)
)
}
# helper function to upload
uploadFile <- function(input){
inFile <- input$file1
if (is.null(inFile))
return(NULL)
tbl <- read.csv(
inFile$datapath,
header = input$header,
sep = input$sep,
quote = input$quote
)
return(
tbl ^ 2
)
}