-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
444 lines (408 loc) · 10.7 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
library(shiny)
library(lubridate)
library(dplyr)
library(tidyr)
library(reactable)
library(shinyscreenshot)
library(shinyjs)
library(shinyWidgets)
calculate_days_and_weekdays <- function(year, month) {
# Generate all dates in the given month
start_date <- ymd(sprintf("%04d-%02d-01", year, month))
end_date <- ceiling_date(start_date, "month") - days(1)
all_dates <- seq(start_date, end_date, by = "day")
# Determine the weekday of the first date
first_weekday <- wday(start_date) # 1 = Sunday, ..., 7 = Saturday
# Add leading NA values to align the first date with Sunday
if (first_weekday != 1) { # If the first date is not Sunday
leading_nas <- rep(NA, first_weekday - 1)
} else {
leading_nas <- c()
}
days <- c(leading_nas, day(all_dates))
# Create a full sequence of weekdays
total_days <- length(days)
weekdays <- rep(c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"), length.out = total_days)
# Calculate week numbers
week_number <- floor((seq_along(days) - 1) / 7) + 1
# Create a data frame
result <- data.frame(
Day = days,
Weekday = weekdays,
Week = week_number
)
return(result)
}
# Shiny UI
ui <- fluidPage(
tags$head(
tags$link(
rel = "icon",
type = "image/png",
href = "logo.png" # Favicon
)
),
tags$head(
tags$title("Minimal Calendar")
),
# open graph tags
tags$head(
tags$meta(property = "og:title", content = "Minimal Calendar"),
tags$meta(property = "og:description", content = "Create your custom monthly calendar"),
tags$meta(property = "og:type", content = "website"),
tags$meta(property = "og:url", content = "https://jhkim.shinyapps.io/calendar/"),
tags$meta(property = "og:image", content = "logo.png"),
),
# CUSTOM CSSs
tags$head(
tags$style(HTML("
.rt-thead{
height: 9vh;
}
.rt-tbody{
height: 89vh;
}
/* header v-align*/
.rt-th.rt-align-right{
align-items: center;
}
/* header align*/
.rt-th.rt-align-right .rt-th-inner{
text-align: center;
}
#file_progress{
display: none;
}
#image{
margin:auto;
}
/*# icon of size controller*/
button.btn.radiobtn.btn-custom-class.disabled{
cursor: initial;
background: #337ab7;
color: white;
opacity: 1;
}
#swal2-html-container{
text-align:left;
}
"))
),
useShinyjs(),
useSweetAlert(),
style = "margin-top: 1em; margin-bottom: 1em;",
div(
id = "info",
style = "display: flex",
# DATE
column(
width = 3,
airMonthpickerInput(
inputId = "ym",
label = NULL,
value = Sys.Date(),
addon = "left",
dateFormat = "MMMM yyyy",
addonAttributes = list(
style = "
background: #337ab7;
color: white;
"
),
width = "100%"
)
),
# Calendar Size
column(
width = 3,
fluidRow(
column(
width = 3,
radioGroupButtons(
inputId = "target",
label = NULL,
choices = c("H", "D"),
status = "primary"
)
),
column(
width = 9,
sliderInput(
inputId = "size",
label = NULL,
min = 5,
max = 50,
value = 20,
ticks = FALSE,
step = 1
)
)
)
),
# Image handler
column(
width = 3,
fluidRow(
column(
width = 3,
radioGroupButtons(
inputId = "types",
label = NULL,
choices = c(
`<i class='fa-regular fa-calendar'></i>` = "cal",
`<i class='fa-regular fa-image'></i>` = "image"
),
status = "primary"
)
),
column(
width = 9,
# IMAGE
div(
id = "fileUI",
style = "display: none;",
fileInput(
"file",
NULL,
accept = "image/*",
placeholder = "Upload Image",
buttonLabel = icon("upload"),
)
),
)
)
),
column(
width = 3,
id = "imageopt",
style = "display:none;",
fluidRow(
column( # width
width = 6,
fluidRow(
column(
width = 1,
radioGroupButtons(
inputId = "i1",
status = "custom-class",
label = NULL,
disabled = TRUE,
selected = character(0),
choices = c(
`<i class='fa-solid fa-arrows-up-down'></i>` = "ii"
)
)
),
column(
width = 8,
style = "margin-left:10%;",
sliderInput(
inputId = "image.location",
label = NULL,
min = 0,
max = 100,
value = 0,
step = 1,
ticks = FALSE,
width = "100%"
)
)
)
),
column( # location
width = 6,
fluidRow(
column(
width = 1,
radioGroupButtons(
inputId = "i2",
status = "custom-class",
label = NULL,
disabled = TRUE,
selected = character(0),
choices = c(
`<i class='fa-solid fa-arrows-left-right'></i>` = "ii"
)
)
),
column(
width = 8,
style = "margin-left:10%;",
sliderInput(
inputId = "image.width",
label = NULL,
min = 0,
max = 100,
value = 100,
step = 5,
ticks = FALSE
)
)
)
)
)
),
# HIDDEN BUTTONS
actionButton("screenshot", "NA", style = "display:none;"),
actionButton("toggle", "NA", style = "display: none;")
),
uiOutput("UIs")
)
# Shiny Server
server <- function(input, output, session) {
# INIT VALUE
year.val <- reactiveVal(value = NULL)
month.val <- reactiveVal(value = NULL)
size.header <- reactiveVal(value = 20)
size.date <- reactiveVal(value = 18)
image.width <- reactiveVal(value = 100)
image.location <- reactiveVal(value = 0)
# Initial Note
sendSweetAlert(
session = session,
title = "Welcome !!",
text =
tags$span(
tags$h3("How to Use?"),
"1. Click", tags$text("Sat", style = 'color: blue'), "will hide options",
tags$br(),
"2. Click", tags$text("Sun", style = 'color: red'), "will download as image",
tags$br(),
tags$br(),
"Code in", tags$a(href = "https://github.com/jahnen/calendar", target = "_", "Here")
)
)
observeEvent(input$size, {
req(input$size)
target <- input$target
if (target == "H") {
size.header(input$size)
}
if (target == "D") {
size.date(input$size)
}
})
observeEvent(input$ym, {
vals <- as.character(input$ym)
year.val(as.numeric(substr(vals, 1, 4)))
month.val(as.numeric(substr(vals, 6, 7)))
})
observeEvent(input$image.width, {
req(input$image.width)
image.width(input$image.width)
})
observeEvent(input$image.location, {
req(input$image.location)
image.location(input$image.location)
})
output$UIs <- renderUI({
# only calendar
if (input$types == "cal") {
shinyjs::hide("fileUI")
shinyjs::hide("imageopt")
list(
column(
width = 12,
reactableOutput("calendar_table", width = "100%", height = "99%")
)
)
}
# image + calendar
else if (input$types == "image") {
shinyjs::show("fileUI")
shinyjs::show("imageopt")
list(
column(
width = 3,
style = paste0("margin-top:", image.location(), "em;"),
imageOutput(
"image",
width = paste0(image.width(), "%"),
height = "100%"
)
),
column(
width = 9,
reactableOutput("calendar_table", width = "100%", height = "99%")
)
)
}
})
output$calendar_table <- renderReactable({
year <- year.val()
month <- month.val()
# Calculate calendar data
calendar_data <- calculate_days_and_weekdays(year, month)
# Reshape the data for gt table
calendar_table <- calendar_data %>%
pivot_wider(names_from = Weekday, values_from = Day, values_fill = list(Day = NA)) %>%
arrange(Week) |>
select(-Week)
# Sunday Onclick
onclick.sun <- "document.getElementById('screenshot').click();"
# Saturday Onclick
onclick.sat <- "document.getElementById('toggle').click();"
calendar_table |>
reactable(
class = "height: 90vh;",
sortable = FALSE,
# bordered = TRUE,
compact = TRUE,
defaultColDef = colDef(
headerStyle = list(
fontSize = paste0(size.header(), "px")
),
style = list(
fontSize = paste0(size.date(), "px")
)
),
columns = list(
Sun = colDef(
html = TRUE,
header = JS(
paste0(
'function(column) {
return `<div style="cursor: pointer; color: #bf616a" onclick ="', onclick.sun, '">` + column.name + `</div>`
}'
)
),
style = list(
fontSize = paste0(size.date(), "px"),
color = "#bf616a"
)
),
Sat = colDef(
html = TRUE,
header = JS('function(column) {
return `<div style="cursor: pointer; color: #337ab7" onclick = "', onclick.sat, '">` + column.name + `</div>`
}'),
style = list(
fontSize = paste0(size.date(), "px"),
color = "#337ab7"
)
)
)
)
})
# image handler
observeEvent(input$file, {
req(input$file)
output$image <- renderImage(
{
list(src = input$file$datapath, alt = "Image", width = "100%", height = "100%")
},
deleteFile = FALSE
)
})
# saturday
observeEvent(input$toggle, {
shinyjs::toggle("info")
})
# sunday
observeEvent(input$screenshot, {
updateReactable("calendar_table")
screenshot(scale = 2)
})
}
# Run the Shiny app
shinyApp(ui = ui, server = server)