-
-
Notifications
You must be signed in to change notification settings - Fork 341
/
storage.R
251 lines (204 loc) · 5.97 KB
/
storage.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
library(dplyr)
library(digest)
library(DBI)
library(RMySQL)
library(RSQLite)
library(mongolite)
library(googlesheets4)
library(aws.s3)
library(rdrop2)
DB_NAME <- "shinyapps"
TABLE_NAME <- "google_form_mock"
SHEET_ID <- "126sYt93gzRGJE6n54CY1Z5VgyXl19btsy8zVweLvYu8"
# decide which function to use to save based on storage type
get_save_fxn <- function(type) {
fxn <- sprintf("save_data_%s", type)
stopifnot(existsFunction(fxn))
fxn
}
save_data <- function(data, type) {
fxn <- get_save_fxn(type)
do.call(fxn, list(data))
}
# decide which function to use to load based on storage type
get_load_fxn <- function(type) {
fxn <- sprintf("load_data_%s", type)
stopifnot(existsFunction(fxn))
fxn
}
load_data <- function(type) {
fxn <- get_load_fxn(type)
data <- do.call(fxn, list())
# Just for a nicer UI, if there is no data, construct an empty
# dataframe so that the colnames will still be shown
if (nrow(data) == 0) {
data <-
matrix(nrow = 0, ncol = length(fields_all),
dimnames = list(list(), fields_all)) %>%
data.frame
}
data %>% dplyr::arrange(desc(timestamp))
}
#### Method 1: Local text files ####
results_dir <- "responses"
save_data_flatfile <- function(data) {
data <- t(data)
file_name <- paste0(
paste(
get_time_human(),
digest(data, algo = "md5"),
sep = "_"
),
".csv"
)
# write out the results
write.csv(x = data, file = file.path(results_dir, file_name),
row.names = FALSE, quote = TRUE)
}
load_data_flatfile <- function() {
files <- list.files(file.path(results_dir), full.names = TRUE)
data <-
lapply(files, read.csv, stringsAsFactors = FALSE) %>%
do.call(rbind, .)
data
}
#### Method 2: SQLite ####
save_data_sqlite <- function(data) {
db <- dbConnect(SQLite(), options()$sqlite$file)
query <-
sprintf("INSERT INTO %s (%s) VALUES ('%s')",
TABLE_NAME,
paste(names(data), collapse = ", "),
paste(data, collapse = "', '")
)
dbGetQuery(db, query)
dbDisconnect(db)
}
load_data_sqlite <- function() {
db <- dbConnect(SQLite(), options()$sqlite$file)
query <- sprintf("SELECT * FROM %s", TABLE_NAME)
data <- dbGetQuery(db, query)
dbDisconnect(db)
data
}
#### Method 3: MySQL ####
save_data_mysql <- function(data) {
db <- dbConnect(MySQL(), dbname = DB_NAME,
host = options()$mysql$host,
port = options()$mysql$port,
user = options()$mysql$user,
password = options()$mysql$password)
query <-
sprintf("INSERT INTO %s (%s) VALUES ('%s')",
TABLE_NAME,
paste(names(data), collapse = ", "),
paste(data, collapse = "', '")
)
dbGetQuery(db, query)
dbDisconnect(db)
}
load_data_mysql <- function() {
db <- dbConnect(MySQL(), dbname = DB_NAME,
host = options()$mysql$host,
port = options()$mysql$port,
user = options()$mysql$user,
password = options()$mysql$password)
query <- sprintf("SELECT * FROM %s", TABLE_NAME)
data <- dbGetQuery(db, query)
dbDisconnect(db)
data
}
#### Method 4: MongoDB ####
collection_name <- sprintf("%s.%s", DB_NAME, TABLE_NAME)
save_data_mongodb <- function(data) {
db <- mongo(collection = TABLE_NAME,
url = sprintf(
"mongodb+srv://%s:%s@%s/%s",
options()$mongodb$username,
options()$mongodb$password,
options()$mongodb$host,
DB_NAME
),
options = ssl_options(weak_cert_validation = TRUE))
data <- as.data.frame(t(data))
db$insert(data)
}
load_data_mongodb <- function() {
db <- mongo(collection = TABLE_NAME,
url = sprintf(
"mongodb+srv://%s:%s@%s/%s",
options()$mongodb$username,
options()$mongodb$password,
options()$mongodb$host,
DB_NAME
),
options = ssl_options(weak_cert_validation = TRUE))
data <- db$find()
data
}
#### Method 5: Google Sheets ####
gs4_auth(path = "googlesheets_serviceaccount.json", email = "daattali@gmail.com", cache = "secrets")
save_data_gsheets <- function(data) {
data <- data %>% as.list() %>% data.frame()
sheet_append(SHEET_ID, data)
}
load_data_gsheets <- function() {
read_sheet(SHEET_ID)
}
#### Method 6: Dropbox ####
drop_auth(rdstoken = "dropbox_token.rds")
save_data_dropbox <- function(data) {
# Create a temporary file to hold the data
data <- t(data)
file_name <- paste0(
paste(
get_time_human(),
digest(data, algo = "md5"),
sep = "_"
),
".csv"
)
file_path <- file.path(tempdir(), file_name)
write.csv(data ,file_path, row.names = FALSE, quote = TRUE)
# Upload the file to dropbox
drop_upload(file_path, path = TABLE_NAME)
}
load_data_dropbox <- function() {
files_info <- drop_dir(TABLE_NAME)
file_paths <- files_info$path_display
# Only take the last 20 because each file takes ~1 second to download
file_paths <- tail(file_paths, 20)
data <-
lapply(file_paths, drop_read_csv, stringsAsFactors = FALSE) %>%
do.call(rbind, .)
data
}
#### Method 7: Amazon S3 ####
s3_bucket_name <- TABLE_NAME %>% gsub("_", "-", .)
save_data_s3 <- function(data) {
# Create a plain-text representation of the data
data <- paste0(
paste(names(data), collapse = ","), "\n",
paste(unname(data), collapse = ",")
)
file_name <- paste0(
paste(
get_time_human(),
digest(data, algo = "md5"),
sep = "_"
),
".csv"
)
# Upload the file to S3
put_object(file = charToRaw(data), object = file_name, bucket = s3_bucket_name)
}
load_data_s3 <- function() {
file_names <- get_bucket_df(s3_bucket_name)[["Key"]]
data <- lapply(file_names, function(x) {
object <- get_object(x, s3_bucket_name)
object_data <- readBin(object, "character")
read.csv(text = object_data, stringsAsFactors = FALSE)
}) %>%
do.call(rbind, .)
data
}