Skip to content

Commit 5977acc

Browse files
committed
small changes
1 parent 6b23f09 commit 5977acc

23 files changed

+269
-226
lines changed

R/DWF_IAS_distribution.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,16 @@ IAS_distribution <- function(
244244

245245
IASDT.R::cat_time(
246246
paste0("There are ", length(Countries2Exclude), " countries to exclude:"),
247-
level = 2, time = FALSE)
247+
level = 2, cat_timestamp = FALSE)
248248

249249
# Mask grid to exclude countries - `TRUE` for grid cells to be considered as
250250
# presence if present in any of the data source; `FALSE` for grid cells need
251251
# to be masked as 0 in species distribution maps (1 becomes 0)
252252
if (length(Countries2Exclude) > 0) {
253253

254254
IASDT.R::cat_time(
255-
paste(sort(Countries2Exclude), collapse = " + "), level = 3, time = FALSE)
255+
paste(sort(Countries2Exclude), collapse = " + "),
256+
level = 3, cat_timestamp = FALSE)
256257

257258
Mask_Keep <- Grid_10_CNT %>%
258259
dplyr::mutate(Keep = !(Country %in% Countries2Exclude)) %>%

R/DWF_IAS_process.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ IAS_process <- function(env_file = ".env", n_cores = 6L, overwrite = TRUE) {
194194

195195
IASDT.R::cat_diff(
196196
init_time = .StartTimeDist,
197-
prefix = "Processing Species-specific data took ", n_lines = 1, level = 2)
197+
prefix = "Processing Species-specific data took ",
198+
msg_n_lines = 1, level = 2)
198199

199200
# # ..................................................................... ###
200201

R/DWF_bioreg.R

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ bioreg_process <- function(env_file = ".env") {
113113
"Download link extraction failed. Found: ", length(BioReg_URL2),
114114
call. = FALSE)
115115
}
116-
IASDT.R::cat_time(BioReg_URL2, level = 2, time = FALSE)
116+
IASDT.R::cat_time(BioReg_URL2, level = 2, cat_timestamp = FALSE)
117117

118118
# # |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ##
119119

@@ -122,11 +122,12 @@ bioreg_process <- function(env_file = ".env") {
122122
Zip_file <- IASDT.R::path(Path_Raw, ZipFileName)
123123
DownCommand <- stringr::str_glue(
124124
'curl -J --create-dirs --output-dir {Path_Raw} -o\\
125-
"{ZipFileName}" -L {BioReg_URL2} --silent --max-time 300')
125+
"{ZipFileName}" -L {BioReg_URL2} --silent --max-cat_time 300')
126126

127127
attempt <- 1
128128
repeat {
129-
IASDT.R::cat_time(paste0("Attempt ", attempt), level = 2, time = FALSE)
129+
IASDT.R::cat_time(
130+
paste0("Attempt ", attempt), level = 2, cat_timestamp = FALSE)
130131

131132
invisible(IASDT.R::system_command(DownCommand))
132133

@@ -239,7 +240,7 @@ bioreg_process <- function(env_file = ".env") {
239240
# Saving processed data ----
240241
IASDT.R::cat_time("Saving processed data")
241242

242-
IASDT.R::cat_time("tiff", level = 1, time = FALSE)
243+
IASDT.R::cat_time("tiff", level = 1, cat_timestamp = FALSE)
243244
terra::writeRaster(
244245
x = BioReg_R, overwrite = TRUE,
245246
filename = file.path(Path_BioReg, "BioReg_R.tif"))
@@ -250,12 +251,12 @@ bioreg_process <- function(env_file = ".env") {
250251
file = file.path(Path_BioReg, "BioReg_R.tif.vat.dbf"),
251252
factor2char = TRUE, max_nchar = 254)
252253

253-
IASDT.R::cat_time("RData - raster object", level = 1, time = FALSE)
254+
IASDT.R::cat_time("RData - raster object", level = 1, cat_timestamp = FALSE)
254255
IASDT.R::save_as(
255256
object = terra::wrap(BioReg_R), object_name = "BioReg_R",
256257
out_path = file.path(Path_BioReg, "BioReg_R.RData"))
257258

258-
IASDT.R::cat_time("RData - sf object", level = 1, time = FALSE)
259+
IASDT.R::cat_time("RData - sf object", level = 1, cat_timestamp = FALSE)
259260
save(BioReg_sf, file = file.path(Path_BioReg, "BioReg_sf.RData"))
260261

261262
# # ..................................................................... ###

R/DWF_railway_intensity.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ railway_intensity <- function(
238238
file = IASDT.R::path(Path_Railways, "Railways_Links.RData"))
239239

240240
IASDT.R::cat_diff(
241-
init_time = .StartTimeDown, n_lines = 1, level = 1,
241+
init_time = .StartTimeDown, msg_n_lines = 1, level = 1,
242242
prefix = "Preparing railways download links took ")
243243

244244
# # ..................................................................... ###
@@ -423,7 +423,7 @@ railway_intensity <- function(
423423

424424
IASDT.R::cat_diff(
425425
init_time = .StartTimeProcess,
426-
prefix = "Processing railway data took ", n_lines = 1, level = 1)
426+
prefix = "Processing railway data took ", msg_n_lines = 1, level = 1)
427427

428428
# # ..................................................................... ###
429429

R/DWF_river_length.R

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ river_length <- function(env_file = ".env", cleanup = FALSE) {
132132

133133
# Extract only the missing or outdated files
134134
if (length(Rivers2extract) > 0) {
135-
IASDT.R::cat_time("Extracting files", level = 1, time = FALSE)
135+
IASDT.R::cat_time("Extracting files", level = 1, cat_timestamp = FALSE)
136136
archive::archive_extract(
137137
Path_Rivers_Zip, dir = Path_Rivers_Interim,
138138
files = basename(Rivers2extract)) %>%
@@ -166,7 +166,8 @@ river_length <- function(env_file = ".env", cleanup = FALSE) {
166166

167167
# Load data from sf file if already exists and valid
168168
IASDT.R::cat_time(
169-
"GPKG file was already processed as sf", level = 2, time = FALSE)
169+
"GPKG file was already processed as sf",
170+
level = 2, cat_timestamp = FALSE)
170171
River_sf <- IASDT.R::load_as(File_Sf)
171172

172173
} else {
@@ -194,7 +195,8 @@ river_length <- function(env_file = ".env", cleanup = FALSE) {
194195

195196
if (nrow(gpkg_2_Extract) > 0) {
196197

197-
IASDT.R::cat_time("Extracting gpkg file", level = 2, time = FALSE)
198+
IASDT.R::cat_time(
199+
"Extracting gpkg file", level = 2, cat_timestamp = FALSE)
198200

199201
# `archive::archive_extract` will always keep the file structure
200202
# inside of the ZIP file, which is not what needed here.
@@ -205,15 +207,16 @@ river_length <- function(env_file = ".env", cleanup = FALSE) {
205207
} else {
206208

207209
IASDT.R::cat_time(
208-
"gpkg file was already extracted", level = 2, time = FALSE)
210+
"gpkg file was already extracted",
211+
level = 2, cat_timestamp = FALSE)
209212

210213
}
211214

212215
# # .................................. #
213216

214217
# Read the gpkg file as sf object
215218
IASDT.R::cat_time(
216-
"Read the gpkg files as sf", level = 2, time = FALSE)
219+
"Read the gpkg files as sf", level = 2, cat_timestamp = FALSE)
217220

218221
River_sf <- sf::st_read(
219222
# Read the gpkg files
@@ -238,7 +241,7 @@ river_length <- function(env_file = ".env", cleanup = FALSE) {
238241

239242
# Save the processed data as an sf object
240243
IASDT.R::cat_time(
241-
"Saving processed data as sf", level = 2, time = FALSE)
244+
"Saving processed data as sf", level = 2, cat_timestamp = FALSE)
242245
IASDT.R::save_as(
243246
object = River_sf, object_name = "River_sf", out_path = File_Sf)
244247
}
@@ -247,7 +250,7 @@ river_length <- function(env_file = ".env", cleanup = FALSE) {
247250
# # .................................. #
248251

249252
# Rasterize the river network data
250-
IASDT.R::cat_time("Rasterizing", level = 2, time = FALSE)
253+
IASDT.R::cat_time("Rasterizing", level = 2, cat_timestamp = FALSE)
251254

252255
River_sf %>%
253256
dplyr::mutate(
@@ -310,13 +313,13 @@ river_length <- function(env_file = ".env", cleanup = FALSE) {
310313
# # |||||||||||||||||||||||||||||||||||
311314

312315
# Save as RData
313-
IASDT.R::cat_time("Save as RData", level = 1, time = FALSE)
316+
IASDT.R::cat_time("Save as RData", level = 1, cat_timestamp = FALSE)
314317
IASDT.R::save_as(
315318
object = terra::wrap(River_Lengths), object_name = "river_length",
316319
out_path = IASDT.R::path(Path_Rivers, "River_Lengths.RData"))
317320

318321
# Save as tif files
319-
IASDT.R::cat_time("Save as tif files", level = 1, time = FALSE)
322+
IASDT.R::cat_time("Save as tif files", level = 1, cat_timestamp = FALSE)
320323
terra::writeRaster(
321324
x = River_Lengths,
322325
filename = IASDT.R::path(

R/general_cat_diff.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
cat_diff <- function(
4848
init_time, chunk_text = "Session summary", prefix = "Completed in ",
49-
cat_info = FALSE, level = 0L, time = FALSE, ...) {
49+
cat_info = FALSE, level = 0L, cat_timestamp = FALSE, ...) {
5050

5151
if (is.null(init_time)) {
5252
stop("`init_time` cannot be NULL", call. = FALSE)
@@ -57,7 +57,8 @@ cat_diff <- function(
5757
prefix <- paste0("\n", prefix)
5858
}
5959

60-
Period <- lubridate::time_length(lubridate::now(tzone = "CET") - init_time) %>%
60+
Period <- lubridate::time_length(
61+
lubridate::now(tzone = "CET") - init_time) %>%
6162
lubridate::seconds_to_period()
6263
Period_hours <- stringr::str_pad(
6364
(lubridate::hour(Period) + 24 * lubridate::day(Period)),
@@ -69,7 +70,7 @@ cat_diff <- function(
6970

7071
paste0(Period_hours, ":", Period_minutes, ":", Period_seconds) %>%
7172
paste0(prefix, .) %>%
72-
IASDT.R::cat_time(level = level, time = time, ...)
73+
IASDT.R::cat_time(level = level, cat_timestamp = cat_timestamp, ...)
7374

7475
return(invisible(NULL))
7576
}

R/general_cat_sep.R

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
#' readability of console output in R scripts or during interactive sessions.
1010
#' @param n_separators integer; the number of separator lines to print. Default
1111
#' is `1`.
12-
#' @param lines_before,lines_after integer; the number of extra empty lines to
13-
#' print before and after the separator lines. Default is `0` and `1`,
14-
#' respectively.
12+
#' @param sep_lines_before,sep_lines_after integer; the number of extra empty
13+
#' lines to print before and after the separator lines. Default is `0` and
14+
#' `1`, respectively.
1515
#' @param line_char character; the character used to construct the separator
1616
#' line. Default is `"-"`.
17-
#' @param repetitions integer; the number of times the character is repeated to
18-
#' form a separator line. Default is `50`.
17+
#' @param line_char_rep integer; the number of times the character is repeated
18+
#' to form a separator line. Default is `50`.
1919
#' @param ... additional arguments to be passed to `cat()`.
2020
#' @name cat_sep
2121
#' @inheritParams cat_time
@@ -31,44 +31,46 @@
3131
#'
3232
#' cat_sep(2,2,3, line_char = "*")
3333
#'
34-
#' cat_sep(2,2,3, line_char = "*", repetitions = 20)
34+
#' cat_sep(2,2,3, line_char = "*", line_char_rep = 20)
3535
#' @export
3636

3737
cat_sep <- function(
38-
n_separators = 1L, lines_before = 0L, lines_after = 1L, line_char = "-",
39-
repetitions = 50L, bold = FALSE, red = FALSE, ...) {
38+
n_separators = 1L, sep_lines_before = 0L, sep_lines_after = 1L,
39+
line_char = "-", line_char_rep = 50L, cat_bold = FALSE, cat_red = FALSE,
40+
...) {
4041

4142
# Check input arguments
4243
AllArgs <- ls(envir = environment())
4344
AllArgs <- purrr::map(
4445
AllArgs,
4546
function(x) get(x, envir = parent.env(env = environment()))) %>%
4647
stats::setNames(AllArgs)
47-
NumericArgs <- c("n_separators", "lines_before", "lines_after", "repetitions")
48+
NumericArgs <- c(
49+
"n_separators", "sep_lines_before", "sep_lines_after", "line_char_rep")
4850
IASDT.R::check_args(
4951
args_all = AllArgs, args_to_check = NumericArgs, args_type = "numeric")
5052
IASDT.R::check_args(
5153
args_all = AllArgs, args_to_check = "line_char", args_type = "character")
5254

53-
if (bold) {
55+
if (cat_bold) {
5456
line_char <- crayon::bold(line_char)
5557
}
56-
if (red) {
58+
if (cat_red) {
5759
line_char <- crayon::red(line_char)
5860
}
5961

60-
if (lines_before > 0) {
61-
cat(strrep("\n", lines_before), ...)
62+
if (sep_lines_before > 0) {
63+
cat(strrep("\n", sep_lines_before), ...)
6264
}
6365

6466

65-
paste(rep(line_char, repetitions), collapse = "") %>%
67+
paste(rep(line_char, line_char_rep), collapse = "") %>%
6668
rep(n_separators) %>%
67-
paste0(collapse = "\n") %>%
69+
paste(collapse = "\n") %>%
6870
cat(...)
6971

70-
if (lines_after > 0) {
71-
cat(strrep("\n", lines_after), ...)
72+
if (sep_lines_after > 0) {
73+
cat(strrep("\n", sep_lines_after), ...)
7274
}
7375
return(invisible(NULL))
7476
}

R/general_cat_time.R

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@
1010
#' print after the message.
1111
#' @param text character; the text to print before the timestamp. If empty
1212
#' (default), only the timestamp is printed.
13-
#' @param n_lines integer; the number of newline characters to print after the
14-
#' message. Default is 1.
15-
#' @param time logical; whether to include the time in the timestamp. Default is
16-
#' `TRUE`. If `FALSE`, only the text is printed.
17-
#' @param date logical; whether to include the date in the timestamp. Only
13+
#' @param msg_n_lines integer; the number of newline characters to print after
14+
#' the message. Default is 1.
15+
#' @param cat_timestamp logical; whether to include the time in the timestamp.
16+
#' Default is `TRUE`. If `FALSE`, only the text is printed.
17+
#' @param cat_date logical; whether to include the date in the timestamp. Only
1818
#' effective if `time` is `TRUE`. Default is `FALSE`, meaning only the time is
1919
#' printed. If `TRUE`, the date is printed in the format "%d/%m/%Y %X".
2020
#' @param time_zone character; the time zone to use for the timestamp. Default
2121
#' is `CET`.
2222
#' @param level integer; the level at which the message will be printed. If e.g.
2323
#' level = 1, the following string will be printed at the beginning of the
2424
#' message: " >>> ". Default is `0`.
25-
#' @param bold logical; whether to print the text in bold. Default is `FALSE`.
26-
#' @param red logical; whether to print the text in red. Default is `FALSE`.
25+
#' @param cat_bold logical; whether to print the text in bold. Default is
26+
#' `FALSE`.
27+
#' @param cat_red logical; whether to print the text in red. Default is `FALSE`.
2728
#' @param ... additional arguments passed to `cat`.
2829
#' @name cat_time
2930
#' @author Ahmed El-Gabbas
@@ -33,15 +34,15 @@
3334
#' @examples
3435
#' cat_time()
3536
#'
36-
#' cat_time(date = TRUE)
37+
#' cat_time(cat_date = TRUE)
3738
#'
3839
#' cat_time("time now")
3940
#'
40-
#' cat_time("\n\nTime now", n_lines = 2L, level = 1L)
41+
#' cat_time("\n\nTime now", msg_n_lines = 2L, level = 1L)
4142
#'
4243
#' cat_time(
43-
#' "\ntime now", date = TRUE, bold = TRUE, red = TRUE,
44-
#' n_lines = 2L, level = 1L)
44+
#' "\ntime now", cat_date = TRUE, cat_bold = TRUE, cat_red = TRUE,
45+
#' msg_n_lines = 2L, level = 1L)
4546
#'
4647
#' # The use of levels
4748
#' {
@@ -52,8 +53,8 @@
5253
#' }
5354

5455
cat_time <- function(
55-
text = "", n_lines = 1L, time = TRUE, bold = FALSE,
56-
red = FALSE, date = FALSE, time_zone = "CET", level = 0L, ...) {
56+
text = "", msg_n_lines = 1L, cat_timestamp = TRUE, cat_bold = FALSE,
57+
cat_red = FALSE, cat_date = FALSE, time_zone = "CET", level = 0L, ...) {
5758

5859
# Validate inputs
5960
AllArgs <- ls(envir = environment())
@@ -63,23 +64,23 @@ cat_time <- function(
6364
stats::setNames(AllArgs)
6465
IASDT.R::check_args(
6566
args_all = AllArgs, args_type = "logical",
66-
args_to_check = c("time", "bold", "red", "date"))
67+
args_to_check = c("cat_timestamp", "cat_bold", "cat_red", "cat_date"))
6768
IASDT.R::check_args(
6869
args_all = AllArgs, args_type = "numeric",
69-
args_to_check = c("n_lines", "level"))
70+
args_to_check = c("msg_n_lines", "level"))
7071
rm(AllArgs, envir = environment())
7172

7273
# Current time
7374
Now <- lubridate::now(tzone = time_zone)
7475

7576
# Format date / time
76-
if (date && time) {
77+
if (cat_date && cat_timestamp) {
7778
Now <- format(Now, "%d/%m/%Y %X")
7879
Now2 <- paste0(" - ", Now)
79-
} else if (date) {
80+
} else if (cat_date) {
8081
Now <- format(Now, "%d/%m/%Y")
8182
Now2 <- paste0(" - ", Now)
82-
} else if (time) {
83+
} else if (cat_timestamp) {
8384
Now <- format(Now, "%X")
8485
Now2 <- paste0(" - ", Now)
8586
} else {
@@ -114,15 +115,15 @@ cat_time <- function(
114115

115116
}
116117

117-
if (bold) {
118+
if (cat_bold) {
118119
text <- crayon::bold(text)
119120
}
120-
if (red) {
121+
if (cat_red) {
121122
text <- crayon::red(text)
122123
}
123124

124125
cat(text, ...)
125-
cat(rep("\n", n_lines))
126+
cat(rep("\n", msg_n_lines))
126127

127128
return(invisible(NULL))
128129
}

0 commit comments

Comments
 (0)