-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.R
1003 lines (866 loc) · 35.8 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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
library(shiny)
library(dplyr)
library(tidyr)
library(forcats)
library(ggplot2)
library(scales)
library(RColorBrewer)
library(plotly)
library(shinythemes)
library(rmarkdown)
# Server
server <- function(input, output, session) {
# Leftmost Panel, Output Current Targets
output$table_sliders <- renderTable(data.frame(cals = input$calSliders,
prot = input$proteinSlider/400 * input$calSliders,
carbs = input$carbSlider/400 * input$calSliders,
fat = input$fatSlider/900 * input$calSliders))
output$table_text <- renderTable(data.frame(
cals = as.numeric(input$proteinText)*4 + as.numeric(input$carbText)*4 + as.numeric(input$fatText)*9,
prot = input$proteinText,
carbs = input$carbText,
fat = input$fatText))
# Link Sliders to Sum to 100
# Update Protein Slider
observeEvent(input$proteinSlider, {
if(as.numeric(input$proteinSlider) + as.numeric(input$carbSlider) + as.numeric(input$fatSlider) != 100){
updateSliderInput(session = session, inputId = "carbSlider",
value = input$carbSlider - (input$fatSlider + input$carbSlider + input$proteinSlider - 100)/2)
updateSliderInput(session = session, inputId = "fatSlider",
value = input$fatSlider - (input$fatSlider + input$carbSlider + input$proteinSlider - 100)/2)
}
})
#Update Fat Slider
observeEvent(input$fatSlider, {
if(as.numeric(input$proteinSlider) + as.numeric(input$carbSlider) + as.numeric(input$fatSlider) != 100){
updateSliderInput(session = session, inputId = "carbSlider",
value = input$carbSlider - (input$fatSlider + input$carbSlider + input$proteinSlider - 100)/2)
updateSliderInput(session = session, inputId = "proteinSlider",
value = input$proteinSlider - (input$fatSlider + input$carbSlider + input$proteinSlider - 100)/2)
}
})
#Update Carb Slider
observeEvent(input$carbSlider, {
if(as.numeric(input$proteinSlider) + as.numeric(input$carbSlider) + as.numeric(input$fatSlider) != 100){
updateSliderInput(session = session, inputId = "fatSlider",
value = input$fatSlider - (input$fatSlider + input$carbSlider + input$proteinSlider - 100)/2)
updateSliderInput(session = session, inputId = "proteinSlider",
value = input$proteinSlider - (input$fatSlider + input$carbSlider + input$proteinSlider - 100)/2)
}
})
# read data and select relevent columns
data <- read.csv('https://raw.githubusercontent.com/UBC-MDS/MacroView/main/data/cleaned_dataset.csv') |>
select(c('Food.name', 'Weight', 'Energy', 'Protein', 'Carbohydrate', 'Total.Fat'))
data[nrow(data)+1,] <- c('None', 1, 0, 0, 0, 0)
# input dropdown food selection
selected_foods = list()
food_list <- data$Food.name
# populate dropdown lists
observe({
updateSelectInput(session, inputId='select_food1', label='Select food', choices=food_list, selected='Chicken, ground, lean, cooked')
updateSelectInput(session, inputId='select_food2', label='Select food', choices=food_list, selected='Rice, brown, long-grain, cooked')
updateSelectInput(session, inputId='select_food3', label='Select food', choices=food_list, selected='Banana')
updateSelectInput(session, inputId='select_food4', label='Select food', choices=food_list, selected='None')
updateSelectInput(session, inputId='select_food5', label='Select food', choices=food_list, selected='None')
})
# update total input food energy table to be plotted
reactive_data <- reactive({
f1 <- data |>
filter(Food.name == input$select_food1)
f2 <- data |>
filter(Food.name == input$select_food2)
f3 <- data |>
filter(Food.name == input$select_food3)
f4 <- data |>
filter(Food.name == input$select_food4)
f5 <- data |>
filter(Food.name == input$select_food5)
cal = (as.numeric(f1[[3]])*(input$g1/as.numeric(f1[[2]])) + as.numeric(f2[[3]])*(input$g2/as.numeric(f2[[2]]))
+ as.numeric(f3[[3]])*(input$g3/as.numeric(f3[[2]])) + as.numeric(f4[[3]])*(input$g4/as.numeric(f4[[2]]))
+ as.numeric(f5[[3]])*(input$g5/as.numeric(f5[[2]])))
prot <- (4*as.numeric(f1[[4]])*(input$g1/as.numeric(f1[[2]])) + 4*as.numeric(f2[[4]])*(input$g2/as.numeric(f2[[2]]))
+ 4*as.numeric(f3[[4]])*(input$g3/as.numeric(f3[[2]])) + 4*as.numeric(f4[[4]])*(input$g4/as.numeric(f4[[2]]))
+ 4*as.numeric(f5[[4]])*(input$g5/as.numeric(f5[[2]])))
carbs <- (4*as.numeric(f1[[5]])*(input$g1/as.numeric(f1[[2]])) + 4*as.numeric(f2[[5]])*(input$g2/as.numeric(f2[[2]]))
+ 4*as.numeric(f3[[5]])*(input$g3/as.numeric(f3[[2]])) + 4*as.numeric(f4[[5]])*(input$g4/as.numeric(f4[[2]]))
+ 4*as.numeric(f5[[5]])*(input$g5/as.numeric(f5[[2]])))
fat <- (9*as.numeric(f1[[6]])*(input$g1/as.numeric(f1[[2]])) + 9*as.numeric(f2[[6]])*(input$g2/as.numeric(f2[[2]]))
+ 9*as.numeric(f3[[6]])*(input$g3/as.numeric(f3[[2]])) + 9*as.numeric(f4[[6]])*(input$g4/as.numeric(f4[[2]]))
+ 9*as.numeric(f5[[6]])*(input$g5/as.numeric(f5[[2]])))
input_foods <- data.frame(
nutrients = c("Cals", "Prot", "Carbs", "Fat"),
values = c(cal, prot, carbs, fat)
)
})
# Slider input
get_data_sliders <- function(){
df <- data.frame(
nutrients = c("Cals", "Prot", "Carbs", "Fat"),
values = c(as.numeric(input$calSliders), as.numeric(input$proteinSlider)/100 * as.numeric(input$calSliders),
as.numeric(input$carbSlider)/100 * as.numeric(input$calSliders),
as.numeric(input$fatSlider)/100 * as.numeric(input$calSliders)))
input_foods <- reactive_data()
df['values_input'] <- input_foods['values']
df <- df |>
mutate(nutrients = as_factor(nutrients) |> fct_relevel(c("Fat", "Carbs", "Prot", "Cals")))
cals_input <- df |> filter(nutrients == "Cals") |> pull(values_input)
cals_goal <- df |> filter(nutrients == "Cals") |> pull(values)
list(df = df, cals_input = cals_input, cals_goal = cals_goal)
}
# Manual input
get_data_manual <- function(){
cals <- as.numeric(input$proteinText)*4 + as.numeric(input$carbText)*4 + as.numeric(input$fatText)*9
df <- data.frame(
nutrients = c("Cals", "Prot", "Carbs", "Fat"),
values = c(cals, as.numeric(input$proteinText)*4,
as.numeric(input$carbText)*4, as.numeric(input$fatText)*9))
# get summary data from food input
input_foods <- reactive_data()
df['values_input'] <- input_foods['values']
df <- df |>
mutate(nutrients = as_factor(nutrients) |> fct_relevel(c("Fat", "Carbs", "Prot", "Cals")))
cals_input <- df |> filter(nutrients == "Cals") |> pull(values_input)
cals_goal <- df |> filter(nutrients == "Cals") |> pull(values)
list(df = df, cals_input = cals_input, cals_goal = cals_goal)
}
# Calculate proportions for the subplot
calc_proportions <- function(df){
prop_df <- df |> filter(nutrients != "Cals")
prop_df['goal'] <- prop_df['values'] / sum(prop_df['values'])
prop_df['input'] <- prop_df['values_input'] / sum(prop_df['values_input'])
prop_df <- prop_df |>
select(c('nutrients', 'goal', 'input')) |>
pivot_longer(2:3, names_to = 'field', values_to = 'prop') |>
mutate(nutrients = fct_rev(nutrients))
prop_df
}
# Main plot
main_plot <- function(data){
df <- data$df
cals_input <- df |> filter(nutrients == "Cals") |> pull(values_input)
cals_goal <- df |> filter(nutrients == "Cals") |> pull(values)
plot <- ggplot(data = df) +
geom_point(aes(x = nutrients, y = values), shape = '-', stroke = 15, size = 15) +
geom_col(aes(x = nutrients, y = values_input, fill = nutrients), alpha=0.7, colour = "black") +
labs(x = "Nutrient", y = "Calories") +
ylim(0, max(cals_input, cals_goal)) +
theme_bw(base_size = 20) +
theme(
legend.key.size = unit(1, 'cm'),
legend.key.height = unit(1, 'cm'),
legend.key.width = unit(1, 'cm'),
) +
scale_fill_brewer(palette = 'Dark2')
plot
}
# Sub plot
sub_plot <- function(data){
df <- data$df
cals_input <- df |> filter(nutrients == "Cals") |> pull(values_input)
cals_goal <- df |> filter(nutrients == "Cals") |> pull(values)
prop_df <- calc_proportions(df)
cals_df <- data.frame(
field = c('goal', 'input'),
cals = c(cals_goal, cals_input),
prop = c(1.17, 1.17),
nutrients = c('Carbs', 'Carbs')
)
subplot <- ggplot(prop_df) +
aes(
y = field,
x = prop,
fill = nutrients
) +
geom_bar(
stat = "identity",
colour = 'black',
alpha = 0.7
) +
geom_text(
aes(label = ifelse(prop >= 0.05, paste0(sprintf("%.0f", prop*100),"%"),"")),
position = position_stack(vjust = 0.5),
colour = "black",
fontface = "bold",
size = 6
) +
scale_x_continuous(breaks = c(0, .25, .5, .75, 1), labels = percent_format()) +
labs(
y = "",
x = "",
fill = ""
) +
theme_bw(base_size = 20) +
theme(
legend.key.size = unit(1, 'cm'),
legend.key.height = unit(1, 'cm'),
legend.key.width = unit(1, 'cm'),
) +
scale_fill_manual(values = rev(brewer.pal(n=3, "Dark2"))) +
geom_text(
aes(label = paste(round(cals, 0), "\ncalories")),
data = cals_df,
size = 6,
hjust = 0.75,
colour = brewer.pal(n=4, "Dark2")[4],
fontface = "bold"
) +
guides(fill = guide_legend(reverse = TRUE))
subplot
}
get_input_foods <- function(){
input_foods_list <- c(
input$select_food1,
input$select_food2,
input$select_food3,
input$select_food4,
input$select_food5
) # This is super good coding btw
input_grams_list <- c(
input$g1, input$g2, input$g3, input$g4, input$g5
)
foods <- data.frame(
food_name = input_foods_list,
weight_grams = input_grams_list
) |>
filter(food_name != 'None')
foods
}
#If using Sliders
observeEvent(input$selectSliders,{
data <- get_data_sliders()
# Main plot
plot1 <- main_plot(data)
output$main_plot <- renderPlot(plot1)
# Sub plot
subplot1 <- sub_plot(data)
output$sub_plot <- renderPlot(subplot1)
})
#If using Manual
observeEvent(input$selectText,{
data <- get_data_manual()
# Main plot
plot2 <- main_plot(data)
output$main_plot <- renderPlot(plot2)
# Sub plot
subplot2 <- sub_plot(data)
output$sub_plot <- renderPlot(subplot2)
})
# Get list of parameters for report
get_report_params <- function(data){
# Run the analysis
# --
input_foods_df <- get_input_foods()
totals_df <- data$df |>
rename(
nutrient = nutrients,
goal_calories = values,
input_calories = values_input
)
main_plot <- main_plot(data)
proportions_df <- calc_proportions(data$df) |>
pivot_wider(names_from = field, values_from = prop) |>
rename(
nutrient = nutrients,
goal_proportion = goal,
input_proportion = input
)
cals_prop <- data.frame(nutrient = c('Cals'), goal_proportion = c(1), input_proportion=c(1))
proportions_df <- rbind(cals_prop, proportions_df)
sub_plot <- sub_plot(data)
# --
# Set up parameters to pass to Rmd document
params <- list(
input_foods = input_foods_df,
totals = totals_df,
main_plot = main_plot,
proportions = proportions_df,
sub_plot = sub_plot
)
params
}
#---Downloads---
# Download report (slider input)
# output$download_sliders <- downloadHandler(
# filename = "report.html",
# content = function(file) {
# data <- get_data_sliders()
# params <- get_report_params(data)
# # params <- list(
# # input_foods = 0,
# # totals = 0,
# # main_plot = 0,
# # proportions = 0,
# # sub_plot = 0
# # )
# render_report(file = file, params = params)
# }
# )
output$download_sliders <- downloadHandler(
filename = "report.html",
content = function(file) {
# Copy the report file to a temporary directory before processing it
tempReport <- file.path(tempdir(),
'report.Rmd')
file.copy(from = 'report.Rmd',
to = tempReport,
overwrite = TRUE)
# Set up parameters to pass to Rmd document
# params <- list(
# input_foods = 0, # temp
# totals = 1, # temp
# main_plot = 2, # temp
# proportions = 3, # temp
# sub_plot = 4 # temp
# )
data <- get_data_sliders()
params <- get_report_params(data)
# Notification
id <- showNotification(
"Rendering report...",
duration = NULL,
closeButton = FALSE
)
on.exit(removeNotification(id))
rmarkdown::render(tempReport,
output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
output$download_manual <- downloadHandler(
filename = "report.html",
content = function(file) {
# Copy the report file to a temporary directory before processing it
tempReport <- file.path(tempdir(),
'report.Rmd')
file.copy(from = 'report.Rmd',
to = tempReport,
overwrite = TRUE)
# Set up parameters to pass to Rmd document
# params <- list(
# input_foods = 0, # temp
# totals = 1, # temp
# main_plot = 2, # temp
# proportions = 3, # temp
# sub_plot = 4 # temp
# )
data <- get_data_sliders()
params <- get_report_params(data)
# Notification
id <- showNotification(
"Rendering report...",
duration = NULL,
closeButton = FALSE
)
on.exit(removeNotification(id))
rmarkdown::render(tempReport,
output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
# # Download report (manual input)
# output$download_manual <- downloadHandler(
# filename = "report.html",
# content = function(file) {
# data <- get_data_manual()
# params <- get_report_params(data)
# # params <- list(
# # input_foods = 0,
# # totals = 0,
# # main_plot = 0,
# # proportions = 0,
# # sub_plot = 0
# # )
# render_report(file = file, params = params)
# }
# )
# Download dataset
output$download_dataset <- downloadHandler(
filename = "food_data.csv",
content = function(file) {
data <- read.csv('https://raw.githubusercontent.com/UBC-MDS/MacroView/main/data/cleaned_dataset.csv') |>
select(c('Food.name', 'Weight', 'Energy', 'Protein', 'Carbohydrate', 'Total.Fat')) |>
rename(
food_name = Food.name,
weight_grams = Weight,
energy_kcal = Energy,
protein_grams = Protein,
carbohydrate_grams = Carbohydrate,
total_fat_grams = Total.Fat
)
write.csv(data, file)
}
)
#Statistics/ Ranking Plot Tab
output$sortedChart <- renderPlotly({
data <- read.csv('https://raw.githubusercontent.com/UBC-MDS/MacroView/main/data/cleaned_dataset.csv') |> rename(name = Food.name)
primaryCol <- data[, gsub(" ", ".", input$primarycomponent, fixed=TRUE)]
secondaryCol <- data[, gsub(" ", ".", input$secondarycomponent, fixed=TRUE)]
data$colByWeightPrimary <- ifelse(data$Weight == 0, 0, primaryCol / data$Weight)
data$colByWeightSecondary <- ifelse(data$Weight == 0, 0, secondaryCol / data$Weight)
top_data <- data |> arrange(desc(colByWeightPrimary), desc(colByWeightSecondary)) |> head(input$topK)
top_data |>
plot_ly(x = ~reorder(name,-(colByWeightPrimary * 1000 + colByWeightSecondary)), y = ~colByWeightPrimary) |>
add_bars() |> layout(title = 'Food ranked by energy/component, sorted', xaxis = list(title = 'Food Name'))
})
}
# UI
ui <- navbarPage(
theme = shinytheme("spacelab"),
'MacroView',
# dashboard tab
tabPanel(
'Dashboard',
# first navbar page
h1('MacroView Main Dashboard'),
sidebarLayout(
# sidebar panel for inputs
sidebarPanel(
width=5,
fluidRow(
# target input
column(
width=5,
h4("Enter Nutrient Targets"),
actionButton("selectSliders", "Plot Sliders", class = "btn-block"),
numericInput(
"calSliders",
"Enter Calorie Goal",
value = 2000,
min = 0,
max = 20000
),
sliderInput(
"proteinSlider",
"Protein %",
value = 30,
min = 0,
max = 100
),
sliderInput(
"carbSlider",
"Carb %",
value = 40,
min = 0,
max = 100
),
sliderInput(
"fatSlider",
"Fat %",
value = 30,
min = 0,
max = 100
),
tableOutput("table_sliders"),
actionButton("selectText", "Plot Manual Input", class = "btn-block"),
textInput("proteinText", "Protein (Grams)",
value = 150),
textInput("carbText", "Carbs (Grams)",
value = 200),
textInput("fatText", "Fat (Grams)",
value = 65),
tableOutput("table_text")
),
# user input entry
column(
width=7,
h4("Food Entry"),
fluidRow(
fluidRow(
column(
h5('Foods'),
selectInput(
inputId = 'select_food1',
label = 'Select food 1',
choices = 'Names',
selected = 'Chicken, ground, lean, cooked',
width = '100%'
),
width = 8,
offset = 0
),
column(
h5('Weights'),
numericInput(
"g1",
"Grams",
value = 500,
min = 0,
max = 9000
),
width = 4,
offset = 0
)
),
# mid panel row 2
fluidRow(
column(
selectInput(
inputId = 'select_food2',
label = 'Select food 2',
choices = 'Names',
selected = 'Rice, brown, long-grain, cooked',
width = '100%'
),
width = 8,
offset = 0
),
column(
numericInput(
"g2",
"Grams",
value = 400,
min = 0,
max = 9000
),
width = 4,
offset = 0
)
),
# mid panel row 3
fluidRow(
column(
selectInput(
inputId = 'select_food3',
label = 'Select food 3',
choices = 'Names',
selected = 'Banana',
width = '100%'
),
width = 8,
offset = 0
),
column(
numericInput(
"g3",
"Grams",
value = 200,
min = 0,
max = 9000
),
width = 4,
offset = 0
)
),
# mid panel row 4
fluidRow(
column(
selectInput(
inputId = 'select_food4',
label = 'Select food 4',
choices = 'Names',
selected = 'None',
width = '100%'
),
width = 8,
offset = 0
),
column(
numericInput(
"g4",
"Grams",
value = 0,
min = 0,
max = 9000
),
width = 4,
offset = 0
)
),
# mid panel row 5
fluidRow(
column(
selectInput(
inputId = 'select_food5',
label = 'Select food 5',
choices = 'Names',
selected = 'None',
width = '100%'
),
width = 8,
offset = 0
),
column(
numericInput(
"g5",
"Grams",
value = 0,
min = 0,
max = 9000
),
width = 4,
offset = 0
)
)
),
fluidRow(
h2(""),
column(
width = 7,
)
),
fluidRow(
h2(""),
column(
width = 7,
)
),
fluidRow(
h2(""),
column(
width = 7,
)
),
fluidRow(
h2(""),
column(
width = 7,
)
),
# Download report button
fluidRow(
column(
h4("Download Report"),
width = 5,
offset = 4,
),
column(
h5("Slider Input"),
downloadButton("download_sliders"),
width = 5,
offset = 4,
),
column(
h5("Manual Input"),
downloadButton("download_manual"),
width = 5,
offset = 4,
),
)
)
)
),
# mainpanel for plots
mainPanel(
width=7,
h1("Macronutrient Totals"),
plotOutput("main_plot", width = "1000px"),
h1("Macronutrient Proportions"),
plotOutput("sub_plot", width = "1000px")
),
position = "left"
)
),
tabPanel('About',
h2('Macroview'),
'Macroview is an application designed to provide a convenient, premium level
experience for tracking macronutrient intake, while employing transparent,
government approved nutrition data.',
h3('Macro Tracking:'),
'Tracking macronutrients is an essential component of the flexible dieting
nutrition strategy, a very common approach for both competitive athletes and
individuals with specific health/ physique goals. As the name implies, flexible
dieting provides more flexibility than a set diet plan, allowing for variation of
food intake while maintain the progress/ performance benefits of structured
dieting by ensuring overall nutrition targets. It is important to note that
flexible dieting is not a replacement healthy eating, and users should still
ensure to hit their nutrient targets with mostly whole, healthy foods. A common
approach is the 80/20 or 90/10 rule, where individuals attempt to have 80-90% of
their nutrients come from healthy, whole sources, with the remainder being free
to come from processed/junk foods',
h5(''),
'The essence of the strategy is to set specific daily targets for the main three
macronutrients (Protein, Carbohydrates, Fats), but to take a flexible approach
to fulfilling these targets. This accomplishes control of total intake via
implicitly setting a calorie total as a sum of the energy content of macro
goals, allowing for control of body weight changes. But beyond simple calorie
tracking, macro tracking allows for specific optimization of the benefits of
each macronutrient:',
h4('Protein:'),
'A minimum amount of protein is required for optimal protein synthesis, and
increased protein promotes satiety.',
h4('Carbohydrates:'),
'Carbohydrates are important for energy and ensuring optimal training performance.
Carbohydrates are the main macronutrient manipulated to control total intake.',
h4('Fats:'),
'A minimum amount of fat is required for proper health/ hormonal function.
Increased fats can also help increase total caloric intake for individuals
who struggle with hitting sufficient calories for their goals. ',
h3('Target Setting Guidance:'),
'Please note that the developers of Macroview are not doctors or registered
The recommendations below are based on personal anecdotal and coaching experience.
Specifically, developer Samson Bakos is an experienced natural bodybuilder and
certified personal trainer with experience in nutrition coaching for both lifestyle
and competitive clients. Please do not attempt drastic diet protocols without
guidance of a professional.',
h4('Total Calories:'),
'An idea of baseline caloric needs can be obtained from simple calculators,
i.e.:',
tags$a(href='https://www.calculator.net/bmr-calculator.html', 'this tool here'),
'which give an estimate
based on sex, height, weight and activity level. From this basal metabolic rate,
users can choose to maintain, gain or lose weight. A surplus or deficit of ~500
calories will lead to a gain or loss of 1 pound, respectively. So for a user with
a maintenance caloric intake of 2500, they will lose approximately 1 pound a week
eating 2000 calories. Note that basal metabolic rate is roughly distributed on
a bell curve, with some individuals having much higher or much lower needs. The
best way to establish your metabolic rate is to track both intake and weight
trends for several weeks, and calculate your needs from there, but calculators
ive a good place to start. Note as well that it is recommended to not lose or
gain more than 1% of your total body mass per week.',
h4('Protein:'),
'The RDA requirement (0.8g per kg bodyweight) is generally considered to be
very low for individuals involved in athletics or desiring physique changes.
Macroview developers recommend 1.5g/kg for individuals involved in regular
athletics, to as high as 2.0-2.2g/kg for those involved in intense resistance
training.',
h4('Fat and Carbs:'),
'Fat and Carbs are generally considered “energy calories” and are used to fill
out the remainder of the desired caloric total. Carbs promote energy and training
performance, and fats promote satiety. Exact balance depends on user food
preferences, but it is not recommended to drop below 0.3g/kg for either
macronutrient.',
h3('App Usage:'),
'See the ReadMe for detailed example images of usage.',
'The left-most panel allows users to set their macronutrient targets, with a
choice between using sliders to set macronutrient percentages (top) or inputting
specific values (bottom). Once set, users must click either the “Plot Sliders” or
“Plot Manual Input” to display plots, which appear on the right side. The top plot
displays the set targets and current logged intake in terms of calories. The
bottom plot displays a bar plot of the current macro breakdown of intake as
percentages alongside the desired final breakdown based on set targets.',
h5(''),
'The middle panel allows for selection of a food from the dataset, along with
a quantity consumed in grams. Once food consumed has been entered, users should
click the desired plotting button (see above) to visualize their intake.'
),
tabPanel('Data',
h3('About the Dataset:'),
'The dataset for this app is “Nutrient Value of Some Common Foods (NVSCF)”,
provided by Health Canada, available through the open.canada.ca portal: ',
tags$a(href='https://open.canada.ca/data/en/dataset/a289fd54-060c-4a96-9fcf-b1c6e706426f.', "here"),
h5(''),
'The dataset is designed as a “quick and easy reference to help make informed
food choices through an understanding of the nutrient content of the foods you
eat.”',
h5(''),
'Note that Macroview does not use all the available columns in the dataset,
as this app is used to track calories and macronutrients, not micronutrients
(vitamins/ minerals) or other values (sugars, cholesterol, saturated fat, etc).
Making healthy choices with respect to specific food selections is left to the
user. More information for foods in the dataset is available in a directly
readable format in the following booklet: ',
tags$a(href='https://open.canada.ca/data/en/dataset/a289fd54-060c-4a96-9fcf-b1c6e706426f/resource/a30e489c-f191-42b5-8f22-1e366e99e7a1', 'here')
),
tabPanel(
'Download',
# first navbar page
h2("Download the Food Dataset"),
mainPanel(
fluidRow(
column(
"",
"",
h4("Download Dataset"),
"",
width = 12,
offset = 0.2,
),),
# Download button
fluidRow(column(
downloadButton("download_dataset"),
width = 10,
offset = 0.2,
),),
fluidRow(column(
'Please note that this data was provided by Health Canada.',
width = 10,
offset = 0.2,
),),
)
),
tabPanel(
'Statistics',
titlePanel("Get the Nutrition Rank!"),
sidebarLayout(
sidebarPanel(
selectInput(
inputId = 'primarycomponent',
label = 'Select your primary component to rank',
choices = c(
'Energy',
'Protein',
'Carbohydrate',
'Total Sugar',
'Total Fat',
'Saturated Fat',
'Monounsaturated Fat',
'Polyunsaturated Fat',
'Cholesterol',
'Calcium',
'Iron',
'Sodium',
'Potassium',
'Magnesium',
'Phosphorus',
'Vitamin A',
'Lycopene',
'Folate',
'DHA',
'EPA',
'Vitamin D',
'Vitamin B12',
'Vitamin E',
'Trans Fat',
'Vitamin C'
),
selected = 'Energy'
),
selectInput(
inputId = 'secondarycomponent',
label = 'Select your secondary component to rank',
choices = c(
'Energy',
'Protein',
'Carbohydrate',
'Total Sugar',
'Total Fat',
'Saturated Fat',
'Monounsaturated Fat',
'Polyunsaturated Fat',
'Cholesterol',
'Calcium',
'Iron',
'Sodium',
'Potassium',
'Magnesium',
'Phosphorus',
'Vitamin A',
'Lycopene',
'Folate',
'DHA',
'EPA',
'Vitamin D',
'Vitamin B12',
'Vitamin E',
'Trans Fat',
'Vitamin C'
),
selected = 'Energy'
),
sliderInput(
inputId = 'topK',
label = 'Choose top K food to rank',
min = 2,
max = 44,
value = 10
)
),
mainPanel(plotlyOutput(outputId = 'sortedChart'))
)
)
)