-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
580 lines (521 loc) · 18.4 KB
/
app.R
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# Weight Loss Trend
library(shiny)
library(shinydashboard)
library(reactable)
library(shinyjs)
library(shinyWidgets)
GOALWEIGHT <- 185
############################## UI ##############################################
ui <- dashboardPage(
title = "Weight Loss Tracking Dashboard",
dashboardHeader(
title = "Weight Loss Tracking Dashboard"
),
{dashboardSidebar(
useShinyjs(),
tags$head(
tags$style(
HTML(".sidebar { height: 90vh; overflow-y: auto; }")
),
tags$link(rel = "shortcut icon", href = "favicon.ico")
),
fluidRow(
style = "margin: 3px",
actionBttn(
"test",
label = "View on Github",
onclick ="window.open('https://github.com/cluffa/weight-loss-dash', '_blank')",
style = "simple",
color = "warning"
),
radioButtons(
"drType",
label = "Date Range Type:",
choices = c(
"Preset",
"Range Input",
"Date Range Selector"
),
selected = "Preset",
inline = TRUE
),
numericInput(
"drNum",
label = "Date Range:",
value = 3,
width = "100px"
),
radioButtons(
"drUnit",
label = NULL,
choices = c(
"Days",
"Weeks",
"Months",
"Years"
),
selected = "Months",
inline = TRUE
),
radioButtons(
"drSimple",
label = "Date Range:",
choices = c(
`30 Days` = Sys.Date() - 30,
`90 Days` = Sys.Date() - 90,
`6 Months` = Sys.Date() - 180,
`1 Year` = Sys.Date() - 365,
`2 Years` = Sys.Date() - 730,
`3 Years` = Sys.Date() - 1095
),
# selected = c(`90 Days` = Sys.Date() - 90),
selected = c(`6 Months` = Sys.Date() - 180),
inline = TRUE
),
dateRangeInput(
"drSelector",
label = "Date Range:",
start = as.Date("2023-01-01", "%Y-%m-%d"),
end = Sys.Date()
),
sliderInput(
"smoothing",
label = "Spline Smoothing:",
min = 0,
max = 1,
step = 0.05,
value = 0.5,
),
awesomeCheckbox(
"showGoal",
label = "Show Projected Goal on Body Weight Plot",
status = "primary"
),
awesomeCheckbox(
"showMM",
label = "Show Min/Max Weights",
status = "primary"
),
awesomeCheckbox(
"model30",
value = FALSE,
label = "Fit Linear Model on Last 30 Days Only",
status = "primary"
),
# sliderInput(
numericInput(
"fitdays",
label = "Adjust the Number of Days (if box checked above):",
min = 5,
# max = 90,
step = 5,
value = 30,
)
),
collapsed = FALSE
)}, # sidebar
{dashboardBody(
fluidRow(
tabBox(
tabPanel(
title = "Body Weight",
plotOutput(
"plot1",
height = "500px",
),
),
tabPanel(
title = "Caloric Deficit/Excess",
plotOutput("calPlot", height = "500px"),
),
height = "580px"
),
tabBox(
id = "tabs",
height = "580px",
tabPanel(
"Stats",
h4("Weight Stats"),
verbatimTextOutput("stats"),
h4("Trends and Goal Projection"),
sliderInput(
"goalwt",
label = "Goal Weight:",
min = GOALWEIGHT - 25,
max = GOALWEIGHT + 25,
value = GOALWEIGHT,
step = 5
),
verbatimTextOutput("summary")
),
tabPanel(
"Model Stats",
verbatimTextOutput("models"),
tags$head(tags$style("#models{font-size: 12px;}"))
),
tabPanel(
"Table",
reactableOutput("table")
),
tabPanel(
"Info",
strong("BMR/TDEE Formula used"),
p("Mifflin-St Jeor equation", a(href = "https://pubmed.ncbi.nlm.nih.gov/2305711/", "https://pubmed.ncbi.nlm.nih.gov/2305711/")),
p("Men: (10 × weight in kg) + (6.25 × height in cm) - (5 × age in years) + 5"),
p("Women: (10 × weight in kg) + (6.25 × height in cm) - (5 × age in years) - 161"),
strong("Background"),
p("This dashboard was created so that I could easily track my weight over time.
I aim for a specific average pounds lost per week over the last 30 or 90 days.
With this dashboard I can easily view those trends and adjust accordingly."),
p("Because the observations are not spread evenly, I created a spline to
approximate the weight with values spread exactly 24 hours apart.
The linear regression line is fit using those points. The slope is then used
to predict when I will meet my goal weight.
")
)
),
)
)} # body
)
################################## SERVER ######################################
server <- function(input, output) {
# library(Cairo)
# options(shiny.usecairo=TRUE)
shinyjs::hide("drSelector")
shinyjs::hide("drNum")
shinyjs::hide("drUnit")
#library(tidyverse)
library(dplyr)
library(ggplot2)
library(lubridate)
# read in data
source("data.R")
df <- get_weight()
loseit <- get_loseit()
df_desc <- arrange(df, desc(date))
# create a spline
spl <- reactive({
smooth.spline(df$date, df$weight, spar = input$smoothing)
}) # |> bindCache(input$smoothing)
pred <- reactive({
rng_start <- floor_date(df$date[1], "days")
rng_stop <- ceiling_date(tail(df$date, 1), "days")
rng <- seq(rng_start, rng_stop, by = "24 hours")
predict(spl(), as.numeric(rng))
}) # |> bindCache(spl())
# wait for changes in the date range type, then show/hide the appropriate inputs
observeEvent(input$drType, {
type = input$drType
if(type == "Date Range Selector") {
shinyjs::hide("drSimple")
shinyjs::show("drSelector")
shinyjs::hide("drNum")
shinyjs::hide("drUnit")
} else if(type == "Range Input") {
shinyjs::hide("drSimple")
shinyjs::hide("drSelector")
shinyjs::show("drNum")
shinyjs::show("drUnit")
} else {
shinyjs::show("drSimple")
shinyjs::hide("drSelector")
shinyjs::hide("drNum")
shinyjs::hide("drUnit")
}
});
# get the data in the range
get_date_range <- reactive({
type <- input$drType
if(type == "Date Range Selector") {
return(as.POSIXct(input$drSelector))
} else if(type == "Range Input") {
return(
as.POSIXct(c(
Sys.Date() - period(input$drNum, tolower(input$drUnit)),
Sys.Date()
))
)
} else {
return(
c(
as.POSIXct(input$drSimple),
as.POSIXct(Sys.Date())
)
)
}
}) # |> bindCache(input$drType, input$drSelector, input$drNum, input$drUnit, input$drSimple)
get_df <- reactive({
range <- get_date_range()
df <- df_desc
df <- df[df$date >= range[1] & df$date <= range[2] + 86400,]
df
}) # |> bindCache(get_date_range())
get_loseit_in_range <- reactive({
range <- get_date_range()
loseit[loseit$date >= range[1] & loseit$date <= range[2] + 86400,]
}) # |> bindCache(get_date_range())
get_model <- reactive({
shorten <- shorten()
pred <- get_spline_pred_in_range() |> shorten()
lm(weight ~ date, pred)
}) # |> bindCache(shorten(), get_spline_pred_in_range())
get_cals <- reactive({
pred <- pred()
c(diff(pred$y), NA) * 3500
}) # |> bindCache(pred())
get_spline_pred_in_range <- reactive({
pred <- pred()
range <- get_date_range()
cals <- get_cals()
in_rng <- pred$x >= range[1] & pred$x <= range[2] + 86400
data.frame(
date = as.POSIXct(pred$x[in_rng], origin = "1970-1-1"),
weight = pred$y[in_rng],
cals = cals[in_rng]
)
}) # |> bindCache(pred(), get_date_range(), get_cals())
get_gw <- reactive({
input$goalwt
})
output$calPlot <- renderPlot({
df <- get_spline_pred_in_range()
loseit <- get_loseit_in_range()
ylimits <- c(
min(
min(loseit$weekdiff, na.rm = TRUE),
min(df$cals, na.rm = TRUE)
),
max(
max(loseit$weekdiff, na.rm = TRUE),
max(df$cals, na.rm = TRUE)
)
)
p <- ggplot(df) +
geom_point(
aes(date, diff),
data = loseit,
color = "lightgray",
alpha = 0.5
) +
geom_line(
aes(date, weekdiff, linetype = tdee_method),
data = loseit,
color = "darkgray"
) +
geom_hline(
yintercept = 0,
color = "green"
) +
geom_line(
aes(date, cals),
color = "blue"
) +
theme_bw() +
ylab("calories per day") +
scale_y_continuous(
sec.axis = sec_axis(~ . / 500, name = "pounds per week", breaks = seq(-5, 5, by = 1)),
breaks = seq(-5000, 5000, by = 500)
) +
theme(legend.position = "bottom") +
coord_cartesian(
ylim = ylimits
)
return(p)
}) # |> bindCache(get_loseit_in_range(), get_spline_pred_in_range(), sizePolicy = sizeGrowthRatio(width = 400, height = 400, growthRate = 1.1))
get_mm <- reactive({
df <- get_df()
mm <- df[c(which.max(df$weight), which.min(df$weight)),]
mm$label <- c("max", "min")
return(mm)
}) # |> bindCache(get_df())
output$plot1 <- renderPlot({
shorten <- shorten()
range <- get_date_range()
df <- get_df()
gw_df <- get_goal_date()
spl <- get_spline_pred_in_range()
mm <- get_mm()
leg <- c(
"Observed Weight" = "darkgray",
"Spline Fit" = "blue",
"Linear Model" = "red",
"Projected Goal Date" = "green"
)
p <- ggplot() +
geom_point(
aes(y = weight, x = date, color = "Observed Weight"),
data = df,
) +
geom_line(
aes(date, weight, color = "Spline Fit"),
data = spl,
linewidth = 1
) +
scale_color_manual(
name = NULL,
breaks = names(leg),
values = leg
) +
scale_y_continuous(
breaks = seq(0, 500, by = 5)
) +
theme_bw() +
theme(
legend.position = "bottom",
legend.background = element_rect(fill = "transparent")
)
if (input$showGoal) {
p <- p + geom_point(
aes(y = weight, x = date, color = "Projected Goal Date"),
data = gw_df,
size = 3
) +
geom_smooth(
aes(x = date, y = weight, color = "Linear Model"),
data = bind_rows(spl, gw_df) |> shorten(extra = 1),
method = "lm",
linetype = 2,
linewidth = 1,
formula = y ~ x,
se = FALSE
) +
geom_text(
aes(
x = date,
y = weight,
label = format(date, "%Y-%m-%d"),
fontface = "bold"
),
data = gw_df,
vjust = "outward",
hjust = "inward",
nudge_y = -1
)
} else {
p <- p +
geom_smooth(
aes(x = date, y = weight, color = "Linear Model"),
data = spl |> shorten(),
method = "lm",
linetype = 2,
linewidth = 1,
formula = y ~ x,
se = FALSE
)
}
if (input$showMM) {
p <- p +
geom_text(
aes(
x = date,
y = weight,
label = paste(" ", weight, label, " "),
fontface = "bold"
),
data = mm,
hjust = "inward",
vjust = "outward",
) +
geom_point(
aes(
x = date,
y = weight,
),
data = mm,
size = 2
)
}
return(p)
}) # |> bindCache(shorten(), get_date_range(), get_df(), get_goal_date(), get_spline_pred_in_range(), get_mm(), shorten(), input$showGoal, input$showMM, sizePolicy = sizeGrowthRatio(width = 400, height = 400, growthRate = 1.1))
shorten <- reactive({
function(df, extra = 0) {
if (input$model30) {
return(tail(df, n = input$fitdays + extra))
} else {
return(df)
}
}
}) # |> bindCache(input$model30, input$fitdays)
get_goal_date <- reactive({
gw <- get_gw()
model <- get_model()
gwdate <- (gw - model$coefficients[1])/model$coefficients[2]
data.frame(
weight = gw,
date = as.POSIXct(gwdate, origin = "1970-1-1")
)
}) # |> bindCache(get_gw(), get_model())
get_cw <- reactive({
round(tail(get_spline_pred_in_range()$weight, n = 1), 1)
}) # |> bindCache(get_spline_pred_in_range())
output$summary <- renderPrint({
model <- get_model()
coef <- unname(model$coefficients[2])*86400
gw <- get_gw()
if (input$model30) {
loseit <- get_loseit_in_range()
loseit <- loseit[loseit$date > (Sys.Date() - input$fitdays),]
} else {
loseit <- get_loseit_in_range()
}
cals <- coef * 3500
gwdate <- (gw - model$coefficients[1])/model$coefficients[2]
gwdate <- as.Date(as.POSIXct.numeric(gwdate, origin = "1970-1-1"))
weeks <- round((gwdate - Sys.Date()) / 7, 1)
food_mean <- mean(loseit$food)
tdee_mean <- mean(loseit$tdee)
est_act <- (food_mean - cals)/(tdee_mean/input$mult)
trend <- ifelse(coef < 0, "Losing", "Gaining")
coef <- abs(coef)
cat(
ifelse(
input$model30,
paste("Linear Model Fit on Last", input$fitdays, "days"),
"Linear Model Fit on Selected Date Range"
),
"\nDaily Trend:", trend, coef |> round(2), "lbs/day",
"\nWeekly Trend:", trend, (coef*7) |> round(2), "lbs/week",
"\nGoal Projection:", as.character(gwdate),
paste0("(", as.character(weeks), " Weeks)"),
"\nAvg Daily Diff Based on Trend:", round(cals, 0),
"\nAvg Daily Intake:", food_mean |> round(),
"\nAvg Est. TDEE:", tdee_mean |> round(),
"\nAvg Daily Diff Based on Intake:", mean(loseit$diff) |> round()
)
}) # |> bindCache(get_model(), get_gw(), input$model30, input$fitdays, input$mult)
output$stats <- renderPrint({
df <- get_df() |>
mutate(
date = format(date, "%Y-%m-%d")
)
df$date <- as.character(df$date)
suppressWarnings(
cat(
"Current Weight:", get_cw(), "lbs",
"\nRange Low:", min(df$weight), "on", df$date[which.min(df$weight)],
"\nRange High:", max(df$weight), "on", df$date[which.max(df$weight)],
"\nRange Mean:", round(mean(df$weight, na.rm = TRUE),1)
)
)
}) # |> bindCache(get_df(), get_date_range(), get_cw())
output$models <- renderPrint({
model <- get_model()
spl <- spl()
print(spl)
summary(model)
}) # |> bindCache(get_model(), spl())
output$table <- renderReactable({
data_frame <- get_spline_pred_in_range()
data_frame[nrow(data_frame):1,] |>
mutate(
date = as_date(date),
weight = round(weight, 1),
cals = round(cals)
) |>
reactable(
striped = TRUE
)
})
}
# Run the application
shinyApp(ui = ui, server = server)