-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcalendar-edit-schedules.R
142 lines (122 loc) · 3.18 KB
/
calendar-edit-schedules.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
library(shiny)
library(toastui)
ui <- fluidPage(
tags$h2("Create, edit and remove schedule interactively"),
tags$p("Click on the calendar to create a new schedule, then you will be able to edit or delete it."),
fluidRow(
column(
width = 9,
calendarOutput("my_calendar")
),
column(
width = 3,
uiOutput("schedule_add"),
uiOutput("schedule_update"),
uiOutput("schedule_delete")
)
)
)
server <- function(input, output) {
# Create calendar
output$my_calendar <- renderCalendar({
cal <- calendar(
defaultDate = Sys.Date(),
navigation = TRUE,
isReadOnly = FALSE,
useCreationPopup = TRUE
) %>%
cal_month_options(narrowWeekend = TRUE) %>%
cal_schedules(
id = "r_intro",
calendarId = "courses",
title = "R - introduction",
body = "What is R?",
start = paste(Sys.Date(), "08:00:00"),
end = paste(Sys.Date(), "12:30:00"),
category = "allday"
)
})
# Interactive counter to give ID to schedules created/edited/deleted
schedule_count <- reactiveVal(0)
# Display changes
output$schedule_add <- renderUI({
if (!is.null(input$my_calendar_add)) {
new <- input$my_calendar_add
tags$div(
"Schedule",
tags$b(paste0("schedule_", schedule_count())),
"have been added with:",
tags$ul(
lapply(
seq_along(new),
function(i) {
tags$li(
tags$b(names(new)[i], ":"),
new[[i]]
)
}
)
)
)
}
})
output$schedule_update <- renderUI({
if (!is.null(input$my_calendar_update)) {
changes <- input$my_calendar_update$changes
tags$div(
"Schedule",
tags$b(input$my_calendar_update$schedule$id),
"have been updated with:",
tags$ul(
lapply(
seq_along(changes),
function(i) {
tags$li(
tags$b(names(changes)[i], ":"),
changes[[i]]
)
}
)
)
)
}
})
output$schedule_delete <- renderUI({
if (!is.null(input$my_calendar_delete)) {
remove <- input$my_calendar_delete
tags$div(
"Schedule",
tags$b(input$my_calendar_delete$id),
"have been deleted with:",
tags$ul(
lapply(
seq_along(remove),
function(i) {
tags$li(
tags$b(names(remove)[i], ":"),
remove[[i]]
)
}
)
)
)
}
})
# Update the calendar
observeEvent(input$my_calendar_add, {
# Update count and add ID
new_count <- schedule_count() + 1
new_schedule <- input$my_calendar_add
new_schedule$id <- paste0("schedule_", new_count)
cal_proxy_add("my_calendar", new_schedule)
schedule_count(new_count)
})
observeEvent(input$my_calendar_update, {
cal_proxy_update("my_calendar", input$my_calendar_update)
})
observeEvent(input$my_calendar_delete, {
cal_proxy_delete("my_calendar", input$my_calendar_delete)
})
}
# Run the application
shinyApp(ui = ui, server = server)