-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
313 lines (235 loc) · 11.2 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
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
server <- function(input, output) {
# Only Convert Boolean to Regex
bool_to_regex <- function(boolQ, ignore_case = TRUE, wildcard = FALSE) {
# Convert Boolean to Regex
inclOR <- gsub(" OR ", "|", boolQ, ignore.case = TRUE)
inclAND <- gsub(" AND ", "\\(\\?\\:\\.\\+\\)" , inclOR, ignore.case = TRUE)
# Case Sensitive Flag
if (ignore_case) {
out <- tolower(inclAND)
} else {
out <- inclAND
}
# Wildcard Flag
if (wildcard) {
outWild <- gsub("\\*", "\\.\\*", out)
return(outWild)
}
return(out)
}
format_query = function(oquery, ignore_case = TRUE, wildcard = TRUE) {
isRegex = stringr::str_detect(oquery, "\\|")
if (isRegex) {
if (ignore_case) {
out = tolower(oquery)
} else {
out = oquery
}
} else {
out <- bool_to_regex(oquery, ignore_case = ignore_case, wildcard = wildcard)
}
return(out)
}
# create loaders
tbl <- addLoader$new("tbl", type = "facebook")
data <- reactiveVal(NULL)
timing <- reactiveVal(NULL)
observeEvent(input$create_button, {
shinyalert("Query execution in Process ... ",
paste("\nPlease consider that this requeriment could take around 20-30 minutes,
depending the size of the data.\n"),
paste("Timing Information:\n", Sys.time()), type = "success")
if(input$filter_db == "core"){
# Measure the time it takes to execute the query
timing_start <- Sys.time()
regexQCode <- input$filter_code_core
regexQ <- "" # Initialize regexQ outside the if condition
if (!is.null(input$filter_regex) && length(input$filter_regex) > 0 && input$filter_regex == TRUE) {
regexQ <- format_query(oquery = input$filter_regex, ignore_case = input$checkbox1, wildcard = input$checkGroup2)
return(regexQ)
}
query <- paste0("CREATE TABLE source_data AS (SELECT title, snippet, publication_date_mdy, language_code, source_name, source_code, publisher_name, title, body, word_count FROM '/database/data/*snappy.parquet' WHERE (publication_date_mdy >='", input$daterange1[1], "' AND publication_date_mdy <= '", input$daterange1[2], "' AND source_name = '", regexQCode, "'));")
con <- dbConnect(duckdb::duckdb(), ":memory:", port = "3838")
dbExecute(con, query)
newDF <- tbl(con, "source_data") %>%
filter(grepl(regexQ, title, ignore.case = TRUE) | grepl(regexQ, body, ignore.case = TRUE)) %>%
collect()
# Record the time taken
timing_end <- Sys.time()
timing_duration <- timing_end - timing_start
timing(timing_duration)
data(newDF)
dbDisconnect(con, shutdown = TRUE)
}
if(input$filter_db == "additional"){
# Measure the time it takes to execute the query
timing_start <- Sys.time()
regexQCode <- input$filter_code_additional
# regexQ <- format_query(oquery = input$filter_regex)
regexQ <- "" # Initialize regexQ outside the if condition
if (!is.null(input$filter_regex) && length(input$filter_regex) > 0 && input$filter_regex == TRUE) {
regexQ <- format_query(oquery = input$filter_regex, ignore_case = input$checkbox1, wildcard = input$checkGroup2)
return(regexQ)
}
query <- paste0("CREATE TABLE source_data AS (SELECT title, snippet, publication_date_mdy, language_code, source_name, source_code, publisher_name, title, body, word_count FROM '/database/data/*snappy.parquet' WHERE (publication_date_mdy >='", input$daterange1[1], "' AND publication_date_mdy <= '", input$daterange1[2], "' AND source_name = '", regexQCode, "'));")
con <- dbConnect(duckdb::duckdb(), ":memory:", port = "3838")
dbExecute(con, query)
newDF <- tbl(con, "source_data") %>%
filter(grepl(regexQ, title, ignore.case = TRUE) | grepl(regexQ, body, ignore.case = TRUE)) %>%
collect()
# Record the time taken
timing_end <- Sys.time()
timing_duration <- timing_end - timing_start
timing(timing_duration)
data(newDF)
dbDisconnect(con, shutdown = TRUE)
}
})
output$tbl <- DT::renderDT({
on.exit(tbl$hide())
tbl$show()
Sys.sleep(1)
if (is.null(data())) {
return(NULL)
}
DT::datatable(data(),
style = 'bootstrap',
rownames = FALSE,
extensions = c('Buttons', 'FixedHeader', 'KeyTable'),
plugins = 'natural',
options = list(dom = 'Bfrtip', pageLength = 1, buttons = list(
list(extend = "collection", buttons = c('csv', 'excel', 'pdf'),
text = "Download Current Page", filename = "page",
exportOptions = list(
modifier = list(page = "current")
)
),
list(extend = "collection", buttons = c('csv', 'excel', 'pdf'),
text = "Download Full Results", filename = "data",
exportOptions = list(
modifier = list(page = "all")
)
)
)))
})
# Initialize modal
modal <- modalDialog(
title = "Database Connection Successful!",
size = "l",
easyClose = TRUE,
footer = tagList(
modalButton("Close")
),
DT::DTOutput("modal_table")
)
# Observe event to open the modal when the "Show Modal" button is clicked
observeEvent(input$create_button, {
showModal(modal)
output$modal_table <- DT::renderDT(server = FALSE, {
DT::datatable(data(),
style = 'bootstrap',
rownames = FALSE,
extensions = c('Buttons', 'FixedHeader', 'KeyTable', 'Scroller'),
plugins = 'natural',
options = list(
deferRender = TRUE,
scrollY = 300,
scrollX = TRUE,
autoWidth = TRUE,
dom = 'Bfrtip',
pageLength = 1,
buttons = list(
list(
extend = "collection",
buttons = c('csv', 'excel', 'pdf'),
text = "Download Current Page",
filename = "page",
exportOptions = list(
modifier = list(page = "current")
)
),
list(
extend = "collection",
buttons = c('csv', 'excel', 'pdf'),
text = "Download Full Results",
filename = "data",
exportOptions = list(
modifier = list(page = "all")
)
))
))
})
})
observe({
if (!is.null(timing())) {
shinyalert("Timing Information", paste("Query execution time:\n", timing(),
"\nNumber of rows: ", nrow(data())))
}
})
observeEvent(input$reset_button, {
session$reload()
})
timevisData <- data.frame(
DataBase = c(rep("Additional", 50), rep("Core", 67)),
Publisher_Name = c(
"Herald Sun - Online", "Herald-Sun", "The Australian", "The Australian - Online",
"The Sydney Morning Herald", "The Sydney Morning Herald - Online", "National Post",
"The Globe and Mail", "The Globe and Mail", "The Toronto Sun",
"Mumbai Mirror", "The Hindu Online", "The Hindu*", "The Times of India*",
"La Jornada", "Metro Mexico", "Reforma", "Reforma.com", "Daily Sun Nigeria",
"Nigerian Tribune", "The Nation", "Vanguard", "The Guardian",
"Daily Independent Nigeria", "Daily Trust Nigeria", "Daily Sun", "Sunday Times",
"The Star", "Mail & Guardian Online", "Sowetan", "20 Minutos", "El Mundo",
"El País - English Edition", "El País - Nacional", "Elmundo.es", "Elpais.com",
"Blick", "Blick Online", "Neue Zürcher Zeitung", "Tages Anzeiger",
"Tages Anzeiger Online", "Cumhuriyet", "Hürriyet", "Hürriyet Daily News",
"Milliyet", "Jewish Voice", "Asian Image", "The Haitian Times",
"Radio Ambulante", "Code Switch", "Die Welt", "Süddeutsche Zeitung",
"WELT online", "ARD Transkripte", "Bayerisches Fernsehen Transkripte",
"bild.de", "BILD Plus", "ARD Alpha Transkripte", "Radio Bremen TV Transkripte",
"MDR Transkripte", "NDR Transkripte", "Nürnberger Nachrichten", "Ostsee-Zeitung",
"Ostsee-Zeitung Online", "RBB Transkripte", "RTL Transkripte",
"Saarbrücker Zeitung", "Sat.1 Transkripte", "SR Fernsehen Transkripte",
"Der Tagesspiegel Online", "Der Tagesspiegel", "Thüringer Allgemeine",
"Weser Kurier", "BILD", "Gazeta.pl", "Gazeta Wyborcza & Wyborcza.pl",
"Rzeczpospolita", "Fakt", "Fakt", "The Daily Telegraph", "The Guardian",
"Guardian.co.uk", "The Telegraph Online", "Evening Times", "Liverpool Echo",
"liverpoolecho.co.uk", "Bournemouth Echo", "The Northern Echo",
"Evening Standard", "Evening Standard Online", "thesun.co.uk", "The Sun",
"Ballymoney & Moyle Times", "The Wall Street Journal", "New York Post",
"The Washington Post", "Washington Post.com", "The Wall Street Journal Online",
"The Atlanta Journal - Constitution", "Deseret News", "Las Vegas Sun",
"Star-Tribune", "PBS: PBS NewsHour", "NBC News: Nightly News",
"The Philadelphia Inquirer", "The Santa Fe New Mexican", "St. Louis Post-Dispatch",
"Tampa Bay Times", "Times of Northwest Indiana", "kabeleins Transkripte",
"ProSieben Transkripte", "ZDF Transkripte", "Dziennik Gazeta Prawna",
"Dziennik Gazeta Prawna Online", "MSNBC: The Rachel Maddow Show",
"CNN: Anderson Cooper 360°", "Fox News Channel: Tucker Carlson Tonight"
),
Country = c(rep("Australia", 6), rep("Canada", 4), rep("India", 4), rep("Mexico", 4),
rep("Nigeria", 7), rep("South Africa", 5), rep("Spain", 6),
rep("Switzerland", 5), rep("Turkey", 4), "Germany","United Kingdom",
rep("United States", 3), rep("Germany", 24), rep("Poland", 5),
rep("United Kingdom", 14), rep("United States", 16), rep("Germany", 3),
rep("Poland", 2), rep("United States", 3)),
Start_Date = c(rep("2012-01-01", 18), rep("2015-01-01", 7), rep("2012-01-01", 20),
rep("2015-01-01", 5), rep("2002-01-01", 3), rep("2012-01-01", 21),
rep("2002-01-01", 3), rep("2012-01-01", 2), rep("2002-01-01", 4),
rep("2012-01-01", 10), "2002-01-01", "2012-01-01",
rep("2002-01-01", 3), rep("2012-01-01", 14), rep("2002-01-01", 2),
rep("2012-01-01", 3)),
End_Date = c(rep("2021-12-31", 117))
)
output$tbl_names <- DT::renderDT(server = FALSE,{
DT::datatable(timevisData,
extensions = c("SearchPanes", "Select"),
options = list(dom = "Ptip")
) |>
formatStyle(
'DataBase',
backgroundColor = styleEqual(
unique(timevisData$DataBase), c('#e8f4f8', '#d2f8d2')
)
)
})
}