-
Notifications
You must be signed in to change notification settings - Fork 0
/
shiny_server.R
211 lines (182 loc) · 5.86 KB
/
shiny_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
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
# the app logic
server <- function(input, output, session) {
onload <- reactiveVal(TRUE)
# log in values
token <- reactiveVal("")
user <- reactiveVal(list())
# we take the first free slot
nslots <- 3
free_slots <- reactiveVal(paste(1:nslots))
file_tree <- reactiveValues()
repos <- reactiveValues()
filenames <- reactiveVal(character(0))
# When OK button is pressed, attempt to authenticate. If successful,
# remove the modal.
observeEvent(input$login, {
isolate({
Username <- input$username
Password <- input$password
})
jwt <- ices_token(username = Username, password = Password, refresh = TRUE)
token(jwt)
if (!is.empty(token())) {
userinfo <- get_with_token("https://taf.ices.dk/api/User", token())
user(userinfo)
removeModal()
# add map selector
mod_map_selector_server("map_selector_1")
appendTab(
"tabset",
tabPanel(
title = "Stock assessment selection",
value = "Stock assessment selection",
mod_map_selector_ui("map_selector_1")
),
select = TRUE
)
} else {
showModal(loginModal(failed = TRUE))
}
})
# observe first url
observeEvent(session$clientData$url_search,
{
if (onload()) {
showModal(loginModal())
# print("observing first url")
query <- getQueryString()
if (is.null(query$repo)) {
# not valid
updateURL()
} else {
repos[[paste0("file_tree_", free_slots()[1])]] <- query$repo
query_file_tree <- CreateInteractiveTreeDF(query$repo)
file_tree[[paste0("file_tree_", free_slots()[1])]] <- query_file_tree
appendTab(
"tabset",
tabPanel(
tab_title(query$repo),
layout_sidebar(
sidebar = sidebar(
mod_file_tree_ui(paste0("file_tree_", free_slots()[1])),
width = "40%"
),
mod_file_viz_ui(paste0("file_viz_", free_slots()[1]))
)
),
select = TRUE
)
# add files
if (!is.null(query$file)) {
query_files <- strsplit(query$file, ",")[[1]]
query_file_ids <- which(query_file_tree$pathString %in% file.path(query$repo, query_files))
filenames(paste0("file_tree_1-", query_file_ids))
}
# remove from empy slot
free_slots(free_slots()[-1])
}
onload(FALSE)
}
},
priority = 99
)
# only run after initialisation
observeEvent(session$clientData$url_search,
{
print("observing url")
query <- getQueryString()
repos_vec <- unlist(reactiveValuesToList(repos))
if (is.null(query$repo) || query$repo == "") {
updateURL()
} else if (!query$repo %in% repos_vec && length(repos_vec) >= nslots) {
updateURL()
} else if (query$repo %in% repos_vec) {
updateURL(repo = query$repo, file = query$file) # trim url
if (trimws(strsplit(input$tabset, "\n")[[1]][2]) != query$repo) {
updateTabsetPanel(inputId = "tabset", selected = query$repo)
}
} else {
updateURL(repo = query$repo, file = query$file) # trim url
repos[[paste0("file_tree_", free_slots()[1])]] <- query$repo
file_tree[[paste0("file_tree_", free_slots()[1])]] <- CreateInteractiveTreeDF(query$repo)
appendTab(
"tabset",
tabPanel(
tab_title(query$repo),
layout_sidebar(
sidebar = sidebar(
mod_file_tree_ui(paste0("file_tree_", free_slots()[1])),
width = "40%"
),
mod_file_viz_ui(paste0("file_viz_", free_slots()[1]))
)
),
select = TRUE
)
# remove from empy slot
free_slots(free_slots()[-1])
}
onload(FALSE)
},
ignoreInit = TRUE
)
# only run after initialisation
observeEvent(input$remove_tab,
{
removeTab(inputId = "tabset", target = paste(tab_title(input$remove_tab)))
slot_to_free <- names(which(unlist(reactiveValuesToList(repos)) == input$remove_tab))
repos[[slot_to_free]] <- NULL
file_tree[[slot_to_free]] <- NULL
filenames(filenames()[!grepl(slot_to_free, filenames())])
free_slots(c(free_slots(), gsub("file_tree_", "", slot_to_free)))
},
ignoreInit = TRUE
)
# only run after initialisation
## NEVER RUNS!! input$tabset is always NULL
observeEvent(input$tabset,
{
print("observing tab click")
if (input$tabset == "Stock assessment selection") {
updateURL()
} else {
query <- getQueryString()
repo <- trimws(strsplit(input$tabset, "\n")[[1]][2])
updateURL(repo = repo, file = query$file, mode = "replace")
}
onload(FALSE)
},
ignoreInit = TRUE
)
observeEvent(input$clicked_text, {
if (!input$clicked_text %in% filenames()) {
filenames(c(filenames(), input$clicked_text))
}
})
# Main modules
mod_file_tree_server("file_tree_1", file_tree)
mod_file_tree_server("file_tree_2", file_tree)
mod_file_tree_server("file_tree_3", file_tree)
mod_file_viz_server("file_viz_1", repos, file_tree, filenames)
mod_file_viz_server("file_viz_2", repos, file_tree, filenames)
mod_file_viz_server("file_viz_3", repos, file_tree, filenames)
# Debugging
observe({
print("---changes---")
print(free_slots())
print("input$tabset")
print(input$tabset)
print(session$clientData$url_search)
print(paste("on load", onload()))
print(unlist(reactiveValuesToList(repos)))
print(filenames())
print("input$clicked_text:")
print(input$clicked_text)
# print("here comes the token:")
# print(token())
print("here comes the user:")
print(user())
print("input$remove_tab")
print(input$remove_tab)
})
}