-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
160 lines (150 loc) · 5.75 KB
/
server.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
server <- function(input, output, session) {
data.re <- reactiveValues()
data.re$CurrentPlan <- read_sheet(data.url, 'CurrentPlan') %>%
as.data.table()
data.re$RecipeList <- read_sheet(data.url, 'RecipeList') %>%
as.data.table()
data.re$Ingredients <- read_sheet(data.url, 'Ingredients') %>%
as.data.table()
data.re$Instructions <- read_sheet(data.url, 'Instructions') %>%
as.data.table()
# Planner ####
pickerInputforPlanner <- function(day) {
RecipeList.pickerInput <- lapply(data.re$RecipeList$Recipe_ID,function(x) x)
names(RecipeList.pickerInput) <- data.re$RecipeList$Name
pickerInput(
inputId = paste0(substr(day, 1, 3) %>% tolower(), 'MealPlan'),
label = day,
choices = RecipeList.pickerInput,
choicesOpt = list(
subtext = data.re$RecipeList$Tags
),
selected = data.re$CurrentPlan[Day == day, Recipe_ID],
options = list(
`live-search` = TRUE
)
)
}
output$sunMealPlan <- renderUI({pickerInputforPlanner('Sunday')})
output$monMealPlan <- renderUI({pickerInputforPlanner('Monday')})
output$tueMealPlan <- renderUI({pickerInputforPlanner('Tuesday')})
output$wedMealPlan <- renderUI({pickerInputforPlanner('Wednesday')})
output$thuMealPlan <- renderUI({pickerInputforPlanner('Thursday')})
output$friMealPlan <- renderUI({pickerInputforPlanner('Friday')})
output$satMealPlan <- renderUI({pickerInputforPlanner('Saturday')})
observeEvent(input$savePlanAction, {
confirmSweetAlert(
session = session,
inputId = "confirmSavePlanAction",
title = "Are you sure you want to save this plan?"
)
})
observeEvent(input$confirmSavePlanAction, {
if(input$confirmSavePlanAction == TRUE) {
# Save Stuff
output <- data.table(
Day = c('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
Recipe_ID = c(input$sunMealPlan, input$monMealPlan, input$tueMealPlan, input$wedMealPlan, input$thuMealPlan, input$friMealPlan, input$satMealPlan)
)
write_sheet(output, data.url, 'CurrentPlan')
}
})
observeEvent(input$clearPlanAction, {
confirmSweetAlert(
session = session,
inputId = "confirmClearPlanAction",
title = "Are you sure you want clear current plan?"
)
})
observeEvent(input$confirmClearPlanAction, {
if(input$confirmClearPlanAction == TRUE) {
for(day in c('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')) {
updatePickerInput(
session,
inputId = paste0(day, 'MealPlan'),
selected = 0
)
}
}
})
observeEvent(input$randomPlanAction, {
confirmSweetAlert(
session = session,
inputId = "confirmrandomPlanAction",
title = "Are you sure you want to random plan?"
)
})
observeEvent(input$confirmrandomPlanAction, {
if(input$confirmrandomPlanAction == TRUE) {
Recipe_IDs <- sample(data.re$RecipeList[Recipe_ID != 0]$Recipe_ID, 7)
days <- c('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
for(i in 1:7) {
updatePickerInput(
session,
inputId = paste0(days[i], 'MealPlan'),
selected = Recipe_IDs[i]
)
}
}
})
# Ingredients List ####
data.re$ShoppingList <- read_sheet(data.url, 'ShoppingList') %>%
as.data.table()
observeEvent(input$importMealPlan, {
meals <- c(input$sunMealPlan, input$monMealPlan, input$tueMealPlan, input$wedMealPlan, input$thuMealPlan, input$friMealPlan, input$satMealPlan)
meals <- unique(meals)
new.data.ShoppingList <- data.re$Ingredients %>%
.[Recipe_ID %in% meals] %>%
.[, .(Amount = sum(Amount)), by = .(Category, Ingredient, Unit)] %>%
setorder(Category, Ingredient) %>%
setcolorder(c('Category', 'Ingredient', 'Amount', 'Unit'))
data.re$ShoppingList <- new.data.ShoppingList
})
observeEvent(input$saveShoppingList, {
confirmSweetAlert(
session = session,
inputId = "confirmsaveShoppingList",
title = "Are you sure you want to save this shopping list? This will overwrite the current shopping list."
)
})
observeEvent(input$confirmsaveShoppingList, {
if(input$confirmsaveShoppingList == TRUE) {
write_sheet(data.re$ShoppingList, data.url, 'ShoppingList')
}
})
output$shoppingListDT <- renderDT({
data <- data.re$ShoppingList
datatable(
data = data,
options = list(
autoWidth = TRUE,
dom = 'Bfrtip',
ordering = TRUE,
paging = FALSE,
searching = FALSE,
info = FALSE
),
rownames = FALSE
)
})
# Recipes ####
output$recipesDT <- renderDT({
data <- copy(data.re$RecipeList)
data[, Recipe_ID := NULL]
data[, Link := paste0("<a href='",Link,"' target='_blank'>",Link,"</a>")]
datatable(
data = data,
options = list(
autoWidth = TRUE,
dom = 'Bfrtip',
ordering = TRUE,
paging = FALSE,
searching = TRUE,
info = FALSE
),
filter = 'top',
rownames = FALSE,
escape = FALSE
)
})
}