From 5b31c433be05950b2b1ca126f71b3650f70d685f Mon Sep 17 00:00:00 2001 From: James Mineau <60365712+jmineau@users.noreply.github.com> Date: Thu, 3 Oct 2024 14:03:56 -0600 Subject: [PATCH] relative linking (#105) * relative linking * no need for stilt_wd as parameter to link_files --- r/dependencies.r | 1 + r/src/link_files.r | 18 ++++++++++++++---- r/src/simulation_step.r | 7 ++----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/r/dependencies.r b/r/dependencies.r index 55eca246..97e1072f 100644 --- a/r/dependencies.r +++ b/r/dependencies.r @@ -16,6 +16,7 @@ libs <- load_libs('dplyr', 'parallel', 'raster', 'rslurm', + 'R.utils', lib.loc = lib.loc) # Load permute fortran dll for footprint matrix permutation diff --git a/r/src/link_files.r b/r/src/link_files.r index d728faf0..61851acf 100644 --- a/r/src/link_files.r +++ b/r/src/link_files.r @@ -1,5 +1,5 @@ -#' link_files symlinks files in exe/* to the target path -#' @author Ben Fasoli +#' link_files symlinks files to the target path using relative paths +#' @author Ben Fasoli & James Mineau #' #' @param from location of files #' @param to location to create links @@ -7,6 +7,16 @@ #' @export link_files <- function(from, to) { - files <- dir(from, full.names = T) - suppressWarnings(file.symlink(files, to)) + require(R.utils) + + # # Get all files in `from` + x <- lapply(from, list.files, full.names = T) + is_file <- sapply(x, function(x) length(x) == 0) + files <- c(from[is_file], unlist(x[!is_file])) + + # Convert to paths relative to stilt_wd + relative_files <- R.utils::getRelativePath(files, to) + + # Create the links + suppressWarnings(file.symlink(relative_files, to)) } diff --git a/r/src/simulation_step.r b/r/src/simulation_step.r index ab980a13..40270e12 100644 --- a/r/src/simulation_step.r +++ b/r/src/simulation_step.r @@ -327,9 +327,7 @@ simulation_step <- function(before_footprint = list(function() {output}), # Save output object to compressed rds file and symlink to out/particles saveRDS(output, output$file) - - link <- file.path(output_wd, 'particles', basename(output$file)) - suppressWarnings(file.symlink(output$file, link)) + link_files(output$file, file.path(output_wd, 'particles')) } else { # If user opted to recycle existing trajectory files, read in the recycled @@ -378,8 +376,7 @@ simulation_step <- function(before_footprint = list(function() {output}), } # Symlink footprint to out/footprints - link <- file.path(output_wd, 'footprints', basename(foot_file)) - suppressWarnings(file.symlink(foot_file, link)) + link_files(foot_file, file.path(output_wd, 'footprints')) return(foot) })