-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable-summarizer.R
More file actions
457 lines (343 loc) · 19.8 KB
/
Copy pathtable-summarizer.R
File metadata and controls
457 lines (343 loc) · 19.8 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
# Part 0: Install the required R packages
list.of.packages <- c("odbc", "DBI","scales", "DT","shiny", "shinyWidgets")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages, type="binary",dependencies=TRUE)
# Part 1: Create helper functions that generate SQL queries ----
# most up-to-date codes: https://github.com/casualcomputer/sql.mechanic
# FUN: get_summary_codes ----
#' Generate SQL codes used to summarize tables ----
#' @param target_path Path to the table
#' @param show_codes character string: option to print the codes
#' @param type character string: choice of basic or advanced summary
#' @param dbtype character string: choice of database type
#' @return character vector containing the SQL scripts
get_summary_codes <- function(target_path, show_codes=FALSE, type="basic", dbtype="Netezza",
quote_table_name=FALSE, clipboard_enabled=TRUE){
target_path <- gsub("\\[|\\]","",target_path) #get rid of opening/closing square brackets in the [db].[schema].[table] notation.
db_path <- unlist(strsplit(target_path,"\\.")) #split on period to make a vector of length 3
if (length(db_path)!=3){
stop("The path of the table has an invalid format. Example of a valid format: 'DATABASE_NAME.SCHEMA_NAME.TABLE_NAME'")
}
if (!(dbtype %in% c("Netezza", "MSSQL"))){
stop("Currently, you can only generate SQL codes for 'Netezza' and 'MSSQL' databases. More updates are coming...")
}
if (!(type %in% c("basic","advanced"))){
stop("Input 'type' is invalid.\nReminder: type='basic' is the short summary and type='advanced' is the more detailed summary.")
}
db_name <- db_path[1] #database name
schema_name <- db_path[2] #schema name
table_name <- db_path[3] #table name
if(type=="basic"& dbtype=="Netezza"){
if (quote_table_name){ table_name_quoted = paste0("\'\"",table_name,"\"\'")} else {
table_name_quoted = paste0("\'",table_name,"\'")}
sql_codes <- paste0(" SELECT REPLACE(REPLACE(REPLACE(
'<start> SELECT ''<col>'' as colname,
COUNT(*) as numvalues,
MAX(freqnull) as freqnull,
CAST(MIN(minval) as CHAR(100)) as minval,
SUM(CASE WHEN <col> = minval THEN freq ELSE 0 END) as numminvals,
CAST(MAX(maxval) as CHAR(100)) as maxval,
SUM(CASE WHEN <col> = maxval THEN freq ELSE 0 END) as nummaxvals,
SUM(CASE WHEN freq =1 THEN 1 ELSE 0 END) as numuniques
FROM (SELECT <col>, COUNT(*) as freq
FROM ",schema_name,".<tab> GROUP BY <col>) osum
CROSS JOIN (SELECT MIN(<col>) as minval, MAX(<col>) as maxval, SUM(CASE WHEN <col> IS NULL THEN 1 ELSE 0 END) as freqnull
FROM (SELECT <col> FROM ",schema_name,".<tab>) osum
) summary',
'<col>', column_name),
'<tab>', ", table_name_quoted, "),
'<start>',
(CASE WHEN ordinal_position = 1 THEN ''
ELSE 'UNION ALL' END)) as codes_data_summary
FROM (SELECT table_name, case when regexp_like(column_name,'[a-z.]|GROUP') then \'\"\'||column_name||\'\"\'
else column_name end as column_name , ordinal_position
FROM information_schema.columns
WHERE table_name =","'",table_name,"'",") a;")
}
if(type=="advanced"&dbtype=="Netezza"){
if (quote_table_name){ table_name_quoted = paste0("\'\"",table_name,"\"\'")} else {
table_name_quoted = paste0("\'",table_name,"\'")}
sql_codes <- paste0("SELECT REPLACE(REPLACE(REPLACE(
'<start> SELECT ''<col>'' as colname,
COUNT(*) as numvalues,
MAX(freqnull) as freqnull,
CAST(MIN(minval) AS VARCHAR(250)) as minval,
SUM(CASE WHEN <col> = minval THEN freq ELSE 0 END) as numminvals,
CAST(MAX(maxval) AS VARCHAR(250)) as maxval,
SUM(CASE WHEN <col> = maxval THEN freq ELSE 0 END) as nummaxvals,
CAST(MIN(CASE WHEN freq = maxfreq THEN <col> END) AS VARCHAR(250)) as mode,
SUM(CASE WHEN freq = maxfreq THEN 1 ELSE 0 END) as nummodes,
MAX(maxfreq) as modefreq,
CAST(MIN(CASE WHEN freq = minfreq THEN <col> END) AS VARCHAR(250)) as antimode,
SUM(CASE WHEN freq = minfreq THEN 1 ELSE 0 END) as numantimodes,
MAX(minfreq) as antimodefreq,
SUM(CASE WHEN freq = 1 THEN freq ELSE 0 END) as numuniques
FROM (SELECT <col> , COUNT(*) as freq
FROM ",schema_name,".<tab>
GROUP BY <col> ) osum CROSS JOIN
(SELECT MIN(freq) as minfreq, MAX(freq) as maxfreq,
MIN(<col> ) as minval, MAX(<col> ) as maxval,
SUM(CASE WHEN <col> IS NULL THEN freq ELSE 0 END) as freqnull
FROM (SELECT <col> , COUNT(*) as freq
FROM ",schema_name,".<tab>
GROUP BY <col> ) osum) summary',
'<col>', column_name),
'<tab>',", table_name_quoted,"),
'<start>',
(CASE WHEN ordinal_position = 1 THEN ''
ELSE 'UNION ALL' END)) as CODES_DATA_SUMMARY
FROM (SELECT table_name, ordinal_position,
case when regexp_like(column_name,'[a-z.]|GROUP') then \'\"\'||column_name||\'\"\'
else column_name end as column_name
FROM information_schema.columns
WHERE table_name = ","'",table_name,"'",") a;")
}
if(type=="basic"& dbtype=="MSSQL"){
if (quote_table_name){ table_name_quoted = paste0("\'\"",table_name,"\"\'")} else {
table_name_quoted = paste0("\'",table_name,"\'")}
sql_codes <- paste0("
SELECT REPLACE(REPLACE(REPLACE('<start> SELECT ''<col>'' as colname,
COUNT(*) as numvalues, MAX(freqnull) as freqnull, CAST(MIN(minval) as
VARCHAR) as minval, SUM(CASE WHEN <col> = minval THEN freq ELSE 0 END)
as numminvals, CAST(MAX(maxval) as VARCHAR) as maxval, SUM(CASE WHEN
<col> = maxval THEN freq ELSE 0 END) as nummaxvals, SUM(CASE WHEN freq =
1 THEN 1 ELSE 0 END) as numuniques FROM (SELECT <col>, COUNT(*) as freq
FROM ", schema_name, ".<tab> GROUP BY <col>) osum CROSS JOIN (SELECT MIN(<col>) as minval,
MAX(<col>) as maxval, SUM(CASE WHEN <col> IS NULL THEN 1 ELSE 0 END) as
freqnull FROM (SELECT <col> FROM ", schema_name, ".<tab>) osum) summary',
'<col>', column_name),
'<tab>', ", table_name_quoted,"),
'<start>',
(CASE WHEN ordinal_position = 1 THEN ''
ELSE 'UNION ALL' END)) as CODES_DATA_SUMMARY
FROM (", "SELECT table_name, case when column_name like ","'",'%[\\.]%',"'",
" then concat(","'",'"',"'",",column_name,","'", '"' ,"'",")",
" else column_name end as column_name, ordinal_position",
" FROM information_schema.columns
WHERE table_name = ", "'",table_name,"'",") a;")
}
if(type=="advanced" & dbtype=="MSSQL"){
if (quote_table_name){ table_name_quoted = paste0("\'\"",table_name,"\"\'")} else {
table_name_quoted = paste0("\'",table_name,"\'")}
sql_codes <- paste0("
SELECT REPLACE(REPLACE(REPLACE(
'<start> SELECT ''<col>'' as colname,
COUNT(*) as numvalues,
MAX(freqnull) as freqnull,
CAST(MIN(minval) AS VARCHAR) as minval,
SUM(CASE WHEN <col> = minval THEN freq ELSE 0 END) as numminvals,
CAST(MAX(maxval) AS VARCHAR) as maxval,
SUM(CASE WHEN <col> = maxval THEN freq ELSE 0 END) as nummaxvals,
CAST(MIN(CASE WHEN freq = maxfreq THEN <col> END) AS VARCHAR) as mode,
SUM(CASE WHEN freq = maxfreq THEN 1 ELSE 0 END) as nummodes,
MAX(maxfreq) as modefreq,
CAST(MIN(CASE WHEN freq = minfreq THEN <col> END) AS VARCHAR) as antimode,
SUM(CASE WHEN freq = minfreq THEN 1 ELSE 0 END) as numantimodes,
MAX(minfreq) as antimodefreq,
SUM(CASE WHEN freq = 1 THEN freq ELSE 0 END) as numuniques
FROM (SELECT <col> , COUNT(*) as freq
FROM ",schema_name,".<tab>
GROUP BY <col> ) osum CROSS JOIN
(SELECT MIN(freq) as minfreq, MAX(freq) as maxfreq,
MIN(<col> ) as minval, MAX(<col> ) as maxval,
SUM(CASE WHEN <col> IS NULL THEN freq ELSE 0 END) as freqnull
FROM (SELECT <col> , COUNT(*) as freq
FROM ",schema_name,".<tab>
GROUP BY <col> ) osum) summary',
'<col>', column_name),
'<tab>', ", table_name_quoted, "),
'<start>',
(CASE WHEN ordinal_position = 1 THEN ''
ELSE 'UNION ALL' END)) as CODES_DATA_SUMMARY
FROM ( ", "SELECT table_name, case when column_name like ","'",'%[\\.]%',"'",
" then concat(","'",'"',"'",",column_name,","'", '"' ,"'",")",
" else column_name end as column_name, ordinal_position" ,
" FROM information_schema.columns
WHERE table_name = ","'",table_name,"'",") a;")
}
if(show_codes==TRUE){
cat(sql_codes)
cat(rep('\n',5))#print codes
}
if (clipboard_enabled) {writeClipboard(sql_codes)} #copy to clipboard, if the clipboard option is enabled
return(sql_codes) #return SQL texts
}
# # FUN: reformat_num
# # add commas to output table
# reformat_num = function(x){
# if(is.numeric(as.numeric(x))){return(comma(as.numeric(x)))}
# else {return(x)}
# }
# Part 2: Create a RShiny app
library(shiny)
library(shinyWidgets)
# Define UI for application ----
ui = fluidPage(
# CSS----
tags$style(HTML("
/* Change input texts*/
label,h3 {
font-family: monospace;
font-size: 15px;
font-weight: bold;
}
/*left, right margins*/
.row {
margin-right: 2px;
margin-left: 2px;
margin-top: 10px;
}
/*left, right, top margins*/
#DataTables_Table_0_wrapper {
margin-top: 10px;
margin-right: 5px;
margin-left: 5px;
}
/* Leave space to top*/
.col-sm-2 {
margin-top: 17px;
}
.dt-buttons{
margin: 4px 0 0 0;
}
#row1 {background-color: #b8c6db;
border-radius: 1em;
#margin-top: 10px;
background: rgb(255,255,255);
background: linear-gradient(90deg, rgba(255,255,255,1) 0%, rgba(208,208,208,0.3309698879551821) 74%);
}
.selectize-control {
width: 35em;
}
.selectize-dropdown-content {
font-size: 0.85em;
}
")),
# dynamic title: https://stackoverflow.com/questions/47896844/shiny-dynamically-change-tab-names
fluidRow(id= "row1", column(1, textInput("odbc_source", h3("ODBC Input"))),
column(5, uiOutput("schema_name_tables")),
column(2, radioButtons("checkMode", "Mode", list("Basic"="basic" ,"Advanced"="advanced"))),
column(2, radioButtons("checkQuote", "\"Table Name\"", list("No" = FALSE, "Yes"=TRUE ))),
column(2, radioButtons("checkDbtype", "Database",
list("Netezza" = "Netezza", "SQL Server" = "MSSQL")
)
)
),
fluidRow(addSpinner(DT::dataTableOutput("summary_table"), spin = "circle", color = "#3498db"))
)
# Define server logic ----
server = function(input, output,session) {
library(odbc)
library(DBI)
library(scales)
library(DT)
# number of rows -----
output$num_rows = renderText({
con = dbConnect(odbc::odbc(), input$odbc_source, encoding = 'windows-1252')
res = dbSendQuery(con, paste("select count(*) as cnt from", input$odbc_table_name))
num_rows = dbFetch(res)
print(num_rows)
num_rows= as.data.frame(num_rows)$CNT
print(num_rows)
dbClearResult(res)
dbDisconnect(con)
scales::comma(num_rows)
})
# drop-down list for database, schema and table ----
output$schema_name_tables = renderUI({
if (nchar(input$odbc_source)>0){
con = tryCatch({ dbConnect(odbc::odbc(), input$odbc_source, encoding = 'windows-1252')
}, error = function(e) {message(paste("Unable to connect to ODBC!")) })
res = tryCatch({ dbSendQuery(con, "select distinct TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME from INFORMATION_SCHEMA.TABLES")
}, error = function(e) {message(paste("Cannot load information schemas!")) })
list_of_schemas = tryCatch({ as.data.frame(dbFetch(res));
}, error = function(e) {message(paste("Cannot load information schemas!")) })
if (!is.null(res)){
dbClearResult(res)
}
if (!is.null(con)){
dbDisconnect(con)
}
selectInput(inputId = "odbc_table_name", label = h3("Table Input"),
choices = c("'No tables selected'",paste0(list_of_schemas$TABLE_CATALOG,".",list_of_schemas$TABLE_SCHEMA,"." ,list_of_schemas$TABLE_NAME)),
selected = NULL)
}
})
#the output table ----
output$summary_table = DT::renderDataTable({
# connect to ODBC data source
con = tryCatch({ dbConnect(odbc::odbc(), input$odbc_source, encoding = 'windows-1252')
}, error = function(e) {message(paste("Unable to connect to ODBC!")) })
# list available data sources
list_of_tables = tryCatch({ dbListTables(con)}, error = function(e){message("Unable to list tables!")})
# table name
curr_table = tryCatch({ unlist(strsplit(input$odbc_table_name,"\\."))[3]},
error = function(e){return(data.frame(Messages="Loading interface..."))},
warning = function(e){return(data.frame(Messages="Loading interface..."))})
# check if the current table exists in metadata
if (curr_table %in% list_of_tables){
sql_query = get_summary_codes(input$odbc_table_name,type=input$checkMode, dbtype=input$checkDbtype,
quote_table_name = input$checkQuote, clipboard_enabled = FALSE) #create SQL queries for summary
res = tryCatch({dbSendQuery(con, sql_query)},
error = function(e){return(data.frame(Messages="Wrong database type selection, or bad choice of quotation"))},
warning = function(e){return(data.frame(Messages="Wrong database type selection, or bad choice of quotation"))})
#get results
output_table = tryCatch({dbFetch(res)},
error = function(e){return(data.frame(Messages = "Wrong database type selection, or bad choice of quotation"))},
warning = function(e){return(data.frame(Messages = "Wrong database type selection, or bad choice of quotation"))}) #fetch the sql queries to run, which will generate summary query
tryCatch({
dbClearResult(res) #clear outputs
summary_query = paste(output_table$CODES_DATA_SUMMARY, collapse = ' ') #copy the summary query 1
print(paste("Table:",input$odbc_table_name))
print("Started Summarizing...")
res = dbSendQuery(con, summary_query) #run the summary query to fetch the summary table
output_table = dbFetch(res); #fetch summary output
cat("Results fetched!\n\n\n")
names(output_table) = toupper(names(output_table))
dbClearResult(res) #clear outputs
dbDisconnect(con)
output_table$NUMVALUES=scales::comma(as.integer(output_table$NUMVALUES))
output_table$FREQNULL=scales::comma(as.integer(output_table$FREQNULL))
#output_table$MAXVAL = unlist(lapply(output_table$MAXVAL ,reformat_num)) # want to reformat numbers only
#output_table$MINVAL = unlist(lapply(output_table$MINVAL ,reformat_num)) # want to reformat numbers only
output_table$NUMMINVALS=scales::comma(as.integer(output_table$NUMMINVALS))
output_table$NUMMAXVALS=scales::comma(as.integer(output_table$NUMMAXVALS))
output_table$NUMUNIQUES=scales::comma(as.integer(output_table$NUMUNIQUES))
print(names(output_table))
if (input$checkMode == "advanced"){
output_table$NUMMODES=scales::comma(as.integer(output_table$NUMMODES))
output_table$MODEFREQ=scales::comma(as.integer(output_table$MODEFREQ))
output_table$NUMANTIMODES=scales::comma(as.integer(output_table$NUMANTIMODES))
output_table$ANTIMODEFREQ=scales::comma(as.integer(output_table$ANTIMODEFREQ))
}
output_table[order(output_table$NUMVALUES,decreasing = TRUE),] #output: summary table
print(output_table[order(output_table$NUMVALUES,decreasing = TRUE),])
},
error = function(e){data.frame(message="Wrong database type selection, or bad choice of quotation");},
warning = function(e){data.frame(message="Wrong database type selection, or bad choice of quotation")})
} else {
if (nchar(input$odbc_source)>0){
data.frame(Messages = paste("The current table you are looking for doesn't exist in", input$odbc_source,"!", "Enter a valid table name."))
} else {
data.frame(Messages = "Please enter your ODBC connection name!")
}
}
},
server=FALSE,
extensions = "Buttons",
rownames= FALSE,
options = list( info = FALSE,
lengthMenu = list(c( 50 ,100,-1), c( "50","100","All" )),
dom = 'lfrtiBp',
buttons = list(list(extend ="csv", text ='Download .CSV',
filename = paste0(gsub("\\.","_",input$odbc_table_name),gsub("-| ","_",Sys.time()),'EST')),
list(extend ="pdf", text ='Download .PDF',
title = paste0(gsub("\\.","_",input$odbc_table_name),gsub("-| ","_",Sys.time()),'EST'),
orientation = 'landscape',pageSize='A3'))
)
)
}
# Run the application
options(shiny.host = '127.0.0.1')
options(shiny.port = 4809)
shinyApp(ui = ui, server = server)