-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_files.R
73 lines (54 loc) · 1.88 KB
/
copy_files.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
# Load packages ----
pacman::p_load(
fs,
here,
stringr,
purrr,
furrr
)
# Point to where the root where the models are located
old_path <- here("../../../Tisc_Models")
# Provide the files the user wants to use
wanted_files <- c("pfl", "UNIT", "PRM", "SLV")
# Loop through each each file type and collect those files from the models
map(wanted_files, function(x){
new_path <- here(sprintf("data/raw/%s", x)) # Very likely data/raw
file_paths <- if (x == "pfl") {
dir_ls(
path = old_path,
recurse = TRUE,
all = TRUE,
regexp = sprintf("%s$", x)
) %>%
str_subset(pattern = sprintf("NS\\.%s$", x), negate = TRUE)
} else {
dir_ls(
path = old_path,
recurse = TRUE,
all = TRUE,
regexp = sprintf("%s$", x)
)
}
# Create model folders ----
model_dir_names <- sort(unique(basename(dirname(file_paths))))
if (x == "pfl") {
unwanted_dir_names <- future_map(model_dir_names, ~ str_subset(string = file_paths, pattern = .x)) %>%
keep(~ length(.x) == 1) %>%
map_chr(~ basename(dirname(.x)))
model_dir_names <- setdiff(model_dir_names, unwanted_dir_names)
if (length(unwanted_dir_names) == 0) {
print(" no unwanted_dir_names")
} else {
file_paths <- str_remove(string = file_paths, pattern = paste0(unwanted_dir_names, collapse = "|"))
}
}
dir_create(path = new_path, model_dir_names)
model_dir_paths <- sort(dir_ls(path = new_path))
# Copy files to respective folders
future_map(model_dir_names, ~ str_subset(string = file_paths, pattern = .x)) %>%
future_walk2(model_dir_paths, ~ file_copy(path = .x, new_path = .y, overwrite = TRUE))
})
# Clears the _targets directory so it is ready to call on the newest
# data set that was just collected
unlink(x = here("_targets/meta"), recursive = T)
unlink(x = here("_targets/objects"), recursive = T)