/
table_modal_w_download.R
185 lines (168 loc) · 5.05 KB
/
table_modal_w_download.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
#' Table Modal with Download UI
#' @noRd
#'
#' @importFrom shiny NS
#' @importFrom shiny tagList
#' @importFrom shiny modalDialog
#' @importFrom shiny h3
#' @importFrom shiny p
#' @importFrom shiny div
#' @importFrom shiny downloadButton
#' @importFrom shiny modalButton
#' @importFrom glue glue
#' @importFrom DT renderDataTable
#'
#' @param id The namespace Id
#' @param title The title to be displayed in the modal.
#' @param download_title The title on the download button.
#' @param n_rows The number of rows of the result.
#' @param result The data frame to display in the modal.
#'
#' @return A shiny tagList
table_modal_w_download_UI <- function(id, title, download_title, n_rows, result) {
ns <- shiny::NS(id)
options(scipen = 99999)
shiny::tagList(
shiny::showModal(
shiny::modalDialog(
easyClose = TRUE,
size = "xl",
shiny::h3(glue("{title}")),
shiny::p(
glue::glue("{format(n_rows, big.mark = \",\")} rows")
),
shiny::div(
class = "table-responsive",
style = "max-height: 70vh;",
DT::renderDataTable(
options = list(dom = "t", paging = FALSE, ordering = FALSE),
server = TRUE,
rownames = FALSE,
{
if(nrow(result) > 1000){
result[1:1000, ]
} else {
result
}
}
)
),
footer = shiny::tagList(
shiny::downloadButton(
ns("downloadQuery"),
glue::glue("{download_title}")
),
shiny::modalButton("Dismiss")
)
)
)
)
}
#' Table Modal with Download Server (Preview)
#' @noRd
#'
#' @importFrom shiny moduleServer
#' @importFrom shiny downloadHandler
#' @importFrom glue glue
#' @importFrom utils write.csv
#'
#' @param id A namespace id.
#' @param result A data frame to open for downloading.
#'
#' @return A model server.
table_modal_w_download_Server <- function(id, result) {
shiny::moduleServer(
id,
function(input, output, session) {
output$downloadQuery <-
shiny::downloadHandler(
filename = function(){
glue::glue(
"query_{format(Sys.time(), \"%Y%m%d%H%M%S\")}.csv"
)
},
content = function(file) {
utils::write.csv(result, file, row.names = FALSE)
}
)
}
)
}
#' Table Modal with Download Server (Full Table)
#' @noRd
#'
#' @importFrom shiny moduleServer
#' @importFrom shiny downloadHandler
#' @importFrom shiny removeModal
#' @importFrom shiny withProgress
#' @importFrom shiny setProgress
#' @importFrom shiny showNotification
#' @importFrom DBI dbQuoteIdentifier
#' @importFrom DBI dbSendQuery
#' @importFrom DBI dbFetch
#' @importFrom DBI dbHasCompleted
#' @importFrom DBI Id
#' @importFrom glue glue
#' @importFrom data.table fwrite
#' @importFrom data.table rbindlist
#'
#' @param id A namespace id.
#' @param con A database connection object.
#' @param schema The schema name.
#' @param table The table name.
#' @param n_rows The number of rows of the table.
#' @param step_size The number of rows to retrieve at each step.
#'
#' @return A model server.
table_modal_w_download_full_Server <- function(id, con, schema, table, n_rows, step_size = floor(n_rows / 100)) {
shiny::moduleServer(
id,
function(input, output, session) {
tryCatch({
output$downloadQuery <-
shiny::downloadHandler(
filename = function(){
glue::glue(
"{schema}_{table}.csv"
)
},
content = function(file) {
shiny::withProgress(
min = 0,
max = n_rows,
value = 0,
message = "Initializing",
expr = {
query <- paste(
"SELECT * FROM",
DBI::dbQuoteIdentifier(
con,
DBI::Id(schema = schema, table = table)
)
)
res <- DBI::dbSendQuery(con, query)
table_pieces <- vector("list", length = 101)
i <- 1
while (!DBI::dbHasCompleted(res)){
table_pieces[[i]] <- DBI::dbFetch(res, n = step_size)
shiny::setProgress(
value = step_size * i,
message = paste("Rows Retrieved:", step_size * i),
session = session
)
i <- i + 1
}
full_table <- data.frame(
data.table::rbindlist(table_pieces)
)
}
)
data.table::fwrite(full_table, file)
}
)
}, error = function(error){
shiny::showNotification(error$message)
})
}
)
}