Skip to content

Commit

Permalink
Add a script to copy profiles into relevant folders
Browse files Browse the repository at this point in the history
This will create new folders as needed and then copy each HSCPs Locality profile into the relevant folder.
  • Loading branch information
Moohan committed Mar 27, 2024
1 parent 5089473 commit 90e6a52
Showing 1 changed file with 61 additions and 0 deletions.
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)
)

0 comments on commit 90e6a52

Please sign in to comment.