-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_functions.R
126 lines (109 loc) · 4.33 KB
/
test_functions.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
# store.figure() -----------------------------------------------------------
# This is custom figure saving (with sequential file naming) only if the changes exists.
# Usage:
# l = list()
# df = data.frame(x = 1 , y = 1)
# store.figure(filename = "Test", data = df, lt = l, console = T)
# store.figure(filename = "Test", data = df, lt = l, console = T)
# str.list(l)
# df = data.frame(x = 2 , y = 1)
# store.figure(filename = "Test", data = df, lt = l, console = T)
# str.list(l)
# df = data.frame(x = 2 , y = 2)
# store.figure(filename = "Test-New", data = df, lt = l, console = T)
# str.list(l)
store.figure <- function(filename, data, lt, check = T,
subfolder = "04-Graphics",
console = FALSE,
fun_family = "csv",
envir = rlang::caller_env(),
...) {
install("rlang")
# Extracting names of the data and the list
data.name = rlang::enexpr(data)
lt.name = deparse(substitute(lt))
# Extracting source file name
current.file.name = get.source.file.name()
# Extracting the file prefix if exists
prefix = get.file.prefix(current.file.name)
# Generating the name of the .Rdata file
stored.list.file.name = subfolder %/% current.file.name %+%
"data"
# Checking whethere .Rdata file already exists in the subfolder
if (file.exists(stored.list.file.name)) {
# If Yes, then load it in a new environment to reduce the name conflicts.
env.stored = new.env()
load(stored.list.file.name, envir = env.stored)
# Printing various messages of this actions
catn(" Data is already saved in:",
color = "blue", console = console, newline = F)
catn(stored.list.file.name, color = "red", console = console)
# Setting 'compare' to note if data saved in .Rdata file needs to be compared
compare = TRUE
} else {
compare = FALSE
}
# Checking
# 1. Whether a list exists if not create.
# 2. Check whether we already have the data with the same name in the list l
if (is.null(names(lt)) || any("figures" != names(lt))) {
lt$figures = list()
catn(" Existing list doesn't contain the figure!",
color = "blue", console = console)
}
file.index = filename == names(lt$figures)
if (any(file.index)) {
# Case when figure name exists
figure.name = names(lt$figures)[file.index]
filename.n = prefix %+% figure.name
catn(" figures of the name",
color = "blue", newline = F, console = console)
catn(filename, color = "red", newline = F, console = console)
catn("' exists!", color = "blue", console = console)
catn(" Data need to rewritten?",
color = "blue", newline = F, console = console)
# Checking whether the data is identical as in the list
# In case we need to overwrite
if (!identical(lt$figures[[figure.name]][[data.name]], data)) {
catn(" Yes!", color = "green", console = console)
write = TRUE
} else {
catn(" No!", color = "red", console = console)
write = FALSE
}
# Case when figure name doesn't exist!
} else {
index = pad.00(length(lt$figures) + 1)
figure.name = index %+% "_" %+% filename %+% "_[R].csv"
filename.n = prefix %+% figure.name
write = TRUE
}
# Case when data needs to be written or overwritten
if (write) {
lt1 = paste0(lt.name, "$figures[['", figure.name, "']]")
eval(parse(text = paste0(lt1, "= list()")), envir = envir)
eval(parse(text = paste0(lt1, " %<% ", data.name)), envir = envir)
# Case when data needs to be overwritten
if (compare) {
data.same = identical(env.stored[[lt.name]]$figures[[figure.name]][[data.name]], data)
} else {
data.same = FALSE
}
# Implementation
# Case: No need to write!
if (compare & data.same) {
catn(" figure data is identical to stored data!",
color = "blue", console = console)
# Case: Data needs to be freshly saved.
} else {
catn(" Saving:", color = "blue", newline = F, console = console)
catn(filename.n, color = "green", console = console)
write_csv.adv(data = data,
file.name = filename.n,
fun_family = fun_family,
...)
eval(parse(text = "save(" %+% lt.name %+% ", file = '" %+% stored.list.file.name %+% "', envir = envir)"))
}
}
return(invisible())
}