-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a script to copy profiles into relevant folders. (#47)
This will create new folders as needed and then copy each HSCPs Locality profile into the relevant folder.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
Master RMarkdown Document & Render Code/Copy profiles to final folder.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
library(fs) | ||
library(dplyr) | ||
library(tidyr) | ||
library(stringr) | ||
|
||
# Set top level file path | ||
lp_path <- path("/conf/LIST_analytics/West Hub/02 - Scaled Up Work/RMarkdown/Locality Profiles") | ||
|
||
# Set paths for the existing / new locations of the profiles | ||
output_dir <- path(lp_path, "Master RMarkdown Document & Render Code", "Output") | ||
# Change the year as needed | ||
final_dir <- path(lp_path, "Final Profiles", "2023 Final Profiles") | ||
|
||
# Source in functions code | ||
source("Master RMarkdown Document & Render Code/Global Script.R") | ||
|
||
# Get the HSCP <> Locality lookup | ||
locality_lookup <- read_in_localities() |> | ||
# Fix for one locality which seems to be renamed (probably due to the '/') | ||
mutate(hscp_locality = case_match(hscp_locality, | ||
"Dumbarton/Alexandria" ~ "Alexandria", | ||
.default = hscp_locality | ||
)) | ||
|
||
# Create a dataframe with some details about the files | ||
profile_lookup <- tibble( | ||
path = dir_ls(path = output_dir, glob = "* - Locality Profile.docx$"), | ||
file_name = path_file(path), | ||
locality = str_extract( | ||
string = file_name, | ||
# Regular expresion, the brackets create a 'capture group' | ||
pattern = "^([A-Z].+?) - Locality Profile.docx$", | ||
# We only want 'group 1' i.e. the bit in the brackets | ||
group = 1 | ||
) | ||
) |> | ||
# Drop any rows which didn't match a locality (usually temp files etc.) | ||
tidylog::drop_na(locality) |> | ||
# Join on the relevant HSCP using the Locality name | ||
left_join( | ||
locality_lookup, | ||
by = join_by(locality == hscp_locality), | ||
unmatched = "error" | ||
) | ||
|
||
# Add columns for the new directory (HSCP name) and the new path | ||
profile_lookup <- profile_lookup |> | ||
mutate( | ||
new_dir = path(final_dir, hscp2019name), | ||
new_path = path(new_dir, file_name) | ||
) | ||
|
||
# Create the new directories (if needed) | ||
# Set the permissions correctly so we can edit the files if needed | ||
dir_create(unique(pull(profile_lookup, new_dir)), mode = "ug=rwx,o=rx") | ||
|
||
# Copy files to the relevant new directory | ||
file_copy( | ||
path = pull(profile_lookup, path), | ||
new_path = pull(profile_lookup, new_path) | ||
) |