-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExportTimeProcessing.R
More file actions
184 lines (114 loc) · 6.28 KB
/
ExportTimeProcessing.R
File metadata and controls
184 lines (114 loc) · 6.28 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
# > PROJECT INFO
# NAME: PROJECT NAME - TEMPLATE
# LEAD: LEADING AUTHOR(S) NAME(S)
#
# > THIS SCRIPT
# AIM: EXPORT TIME OF PROCESSING FUNCTION
# AUTHOR: JOÃO PEDRO VIEIRA
#
# > NOTES
# 1: -
# FUNCTION -------------------------------------------------------------------------------------------------------------------------------------------
ExportTimeProcessing <- function(fctn.codeDir = "") {
# EXPORT TIME OF PROCESSING TO A CSV TABLE
#
# ARGS
# fctn.codeDir: name of code directory where the script lives
#
# RETURN
# nothing, only export information to a csv table
# LIBRARIES
require("tidyverse")
require("tictoc")
require("here")
# checks if tic.log only have one time information
if (length(tictoc::tic.log(format = TRUE)) != 1) {
stop("tictoc::tic.log has more than 1 time info, remember to tictoc::tic.clearlog() before running the script again")
}
# checks if file exists or not
if (!file.exists(paste0(fctn.codeDir, "/_timeProcessing_", stringr::str_replace_all(fctn.codeDir, "/", "_"), ".csv"))) {
# extract time in seconds
time.in.seconds <-
tictoc::tic.log(format = TRUE) %>%
as.character() %>%
stringr::str_extract(pattern = "(\\d+(?:\\.\\d+)?)(?=\\s+sec\\s+elapsed)") %>%
as.numeric()
if (time.in.seconds > 86400) {
# transform time from seconds to days
time.in.days <- (time.in.seconds/86400) %>% round(digits = 0) %>% as.character() %>% paste0(" days")
# creates csv file with the first entry
tibble::tibble(time = paste0((tictoc::tic.log(format = FALSE))[[1]]$msg, ": ", time.in.days)) %>%
readr::write_csv(col_names = F,
file = here::here(paste0(fctn.codeDir, "/_timeProcessing_", stringr::str_replace_all(fctn.codeDir, "/", "_"), ".csv")))
} else if (time.in.seconds > 3600) {
# transform time from seconds to hours
time.in.hours <- (time.in.seconds/3600) %>% round(digits = 0) %>% as.character() %>% paste0(" hours")
# creates csv file with the first entry
tibble::tibble(time = paste0((tictoc::tic.log(format = FALSE))[[1]]$msg, ": ", time.in.hours)) %>%
readr::write_csv(col_names = F,
file = here::here(paste0(fctn.codeDir, "/_timeProcessing_", stringr::str_replace_all(fctn.codeDir, "/", "_"), ".csv")))
} else if (time.in.seconds > 60) {
# transform time from seconds to minutes
time.in.minutes <- (time.in.seconds/60) %>% round(digits = 0) %>% as.character() %>% paste0(" minutes")
# creates csv file with the first entry
tibble::tibble(time = paste0((tictoc::tic.log(format = FALSE))[[1]]$msg, ": ", time.in.minutes)) %>%
readr::write_csv(col_names = F,
file = here::here(paste0(fctn.codeDir, "/_timeProcessing_", stringr::str_replace_all(fctn.codeDir, "/", "_"), ".csv")))
} else {
time.in.seconds <- time.in.seconds %>% round(digits = 0) %>% as.character() %>% paste0(" seconds")
# creates csv file with the first entry
tibble::tibble(time = paste0((tictoc::tic.log(format = FALSE))[[1]]$msg, ": ", time.in.seconds)) %>%
readr::write_csv(col_names = F,
file = here::here(paste0(fctn.codeDir, "/_timeProcessing_", stringr::str_replace_all(fctn.codeDir, "/", "_"), ".csv")))
}
# clear tic toc log
tictoc::tic.clearlog()
} else {
# read existing table of times
time.table <- readr::read_csv(paste0(fctn.codeDir, "/_timeProcessing_", stringr::str_replace_all(fctn.codeDir, "/", "_"), ".csv"),
col_names = "time")
# checks if time for the input script was already registered or not
if(any(stringr::str_detect(time.table$time, (tictoc::tic.log(format = FALSE))[[1]]$msg))) {
# prints message
print(paste0((tictoc::tic.log(format = FALSE))[[1]]$msg, " already registered! If you want to re-register first manually exclude the entry in the csv file"))
} else {
# extract time in seconds
time.in.seconds <-
tictoc::tic.log(format = TRUE) %>%
as.character() %>%
stringr::str_extract(pattern = "(\\d+(?:\\.\\d+)?)(?=\\s+sec\\s+elapsed)") %>%
as.numeric()
if (time.in.seconds > 86400) {
# transform time from seconds to days
time.in.days <- (time.in.seconds/86400) %>% round(digits = 0) %>% as.character() %>% paste0(" days")
# appends new time (in days) to existing table
time.table <- dplyr::bind_rows(time.table,
tibble::tibble(time = paste0((tictoc::tic.log(format = FALSE))[[1]]$msg, ": ", time.in.days)))
} else if (time.in.seconds > 3600) {
# transform time from seconds to hours
time.in.hours <- (time.in.seconds/3600) %>% round(digits = 0) %>% as.character() %>% paste0(" hours")
# appends new time (in hours) to existing table
time.table <- dplyr::bind_rows(time.table,
tibble::tibble(time = paste0((tictoc::tic.log(format = FALSE))[[1]]$msg, ": ", time.in.hours)))
} else if (time.in.seconds > 60) {
# transform time from seconds to minutes
time.in.minutes <- (time.in.seconds/60) %>% round(digits = 0) %>% as.character() %>% paste0(" minutes")
# appends new time (in minutes) to existing table
time.table <- dplyr::bind_rows(time.table,
tibble::tibble(time = paste0((tictoc::tic.log(format = FALSE))[[1]]$msg, ": ", time.in.minutes)))
} else {
time.in.seconds <- time.in.seconds %>% round(digits = 0) %>% as.character() %>% paste0(" seconds")
# appends new time (in seconds) to existing table
time.table <- dplyr::bind_rows(time.table,
tibble::tibble(time = paste0((tictoc::tic.log(format = FALSE))[[1]]$msg, ": ", time.in.seconds)))
}
# save table
readr::write_csv(time.table,
col_names = F,
file = here::here(paste0(fctn.codeDir, "/_timeProcessing_", stringr::str_replace_all(fctn.codeDir, "/", "_"), ".csv")))
}
# clear tic toc log
tictoc::tic.clearlog()
}
}
# END OF SCRIPT --------------------------------------------------------------------------------------------------------------------------------------