-
Notifications
You must be signed in to change notification settings - Fork 1
/
_storage.r
56 lines (49 loc) · 1.72 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
loadPackages(
'googledrive'
)
saveToStorages = function(data, meta, storages = g$storage$default$save) {
fileName = glue("{meta$id}.{meta$format}")
if (meta$format == 'csv') {
fileWriteFunction = function(d, f) fwrite(d, f)
} else {
stop(glue("Format type '{meta$format}' not implemented!"))
}
for (storage in storages) {
l(glue("Storage '{storage}':"))
if (storage == "local") {
filePath = file.path(g$storage$local$path, fileName)
l(glue("-> '{filePath}'"), iL = 2)
fileWriteFunction(data, filePath)
} else if (storage == "googledrive") {
fileTemp = tempfile()
fileWriteFunction(data, fileTemp)
l(glue("-> '{fileName}'"), iL = 2)
uploadGoogleDrive(fileTemp, fileName)
} else {
stop(glue("Storage type '{storage}' not implemented!"))
}
}
}
loadFromStorage = function(id, format = 'csv', storage = g$storage$default$load) {
# format = 'csv'
# id = "temperature-hdd"
fileName = glue("{id}.{format}")
if (format == 'csv') {
fileReadFunction = function(f) fread(f)
} else {
stop(glue("Format type '{format}' not implemented!"))
}
if (storage == "local") {
file = file.path(g$storage$local$path, fileName)
return(fileReadFunction(file))
} else if (storage == "googledrive") {
file = tempfile()
drive_download(file.path(g$googledrive, fileName), file)
return(fileReadFunction(file))
} else {
stop(glue("Storage type '{storage}' not implemented!"))
}
}
uploadGoogleDrive = function(file, fileName) {
drive_put(file, path = file.path(g$storage$googledrive$path, fileName))
}