From eda386039c0b8ec4479e1773bda01a23bc0ec177 Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Tue, 12 Nov 2024 18:17:22 -0800 Subject: [PATCH 01/29] Updated news to reflect file pathway being added to distanceMatrix() function --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 87fef68..77a2971 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,6 +22,7 @@ Enhancements: * New `convertLotekCDMAFile()` function to prepare lotek txt logs for further analyses (issue [#103](https://github.com/hugomflavio/actel/issues/103)). * New `plotDot()` function to help users inspect their spatial.txt files (issue [#55](https://github.com/hugomflavio/actel/issues/55)). * Improved internal mechanisms for message/warning/debug/stop reporting. new internal function `event()` replaces the old `appendTo()`, `stopAndReport()`, and `emergencyBreak()` (issue [#135](https://github.com/hugomflavio/actel/issues/135)) + * Added the ability to specify a file pathway for `distancesMatrix()` if the 'spatial.csv' is not in the main working directory. (issue [#133](https://github.com/hugomflavio/actel/issues/133)) ## actel 1.3.0 From a240cad39adaa07204d885bbb78b0b44c4abe7ab Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Tue, 12 Nov 2024 18:18:38 -0800 Subject: [PATCH 02/29] Updated documentation code for distanceMatrix() and added the ability to specify a file pathway when the spatial.csv is not in the main working directory. Also compressed the function code to 80c limit --- R/distances.R | 235 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 176 insertions(+), 59 deletions(-) diff --git a/R/distances.R b/R/distances.R index 912c65e..8e283e2 100644 --- a/R/distances.R +++ b/R/distances.R @@ -342,26 +342,39 @@ transitionLayer <- function(x, directions = c(16, 8, 4)){ #' Calculate Distances Matrix #' -#' Using a previously created transition layer (see \code{\link{transitionLayer}}), calculates the distances -#' between spatial points. Adapted from Grant Adams' script "distance to closest mpa". if the argument 'actel' -#' is set to TRUE (default), an actel-compatible matrix is generated, and the user will be asked if they would -#' like to save the matrix as 'distances.csv' in the current directory. -#' -#' It is highly recommended to read the manual page regarding distances matrices before running this function. -#' You can find it here: \href{https://hugomflavio.github.io/actel-website/manual-distances.html}{https://hugomflavio.github.io/actel-website/manual-distances.html} -#' -#' @param t.layer A TransitionLayer object, generated by \code{\link{transitionLayer}}. -#' @param starters A data frame with the points from which to start measuring the distance. Ignored if actel = TRUE (default), as the 'spatial.csv' is loaded as starters. -#' @param targets A data frame with the points to which a way must be found. Ignored if actel = TRUE (default), as the 'spatial.csv' is loaded as targets. -#' @param coord.x,coord.y The names of the columns containing the x and y coordinates in the starters and targets. Must be identical in the starters and targets. -#' @param id.col The name of the column containing the IDs of the points to be used as starters and targets. Must be identical in both files. Ignored if actel = TRUE (default), as the stations' standard names are used. -#' @param actel Logical: Should the distance matrix be optimized for actel? Defaults to TRUE. +#' Using a previously created transition layer (see \code{\link{transitionLayer}}), +#' calculates the distances between spatial points. Adapted from Grant Adams' +#' script "distance to closest mpa". If the argument 'actel' is set to +#' TRUE (default), an actel-compatible matrix is generated, and the user will be +#' asked if they would like to save the matrix as 'distances.csv' in the current +#' directory. +#' +#' It is highly recommended to read the manual page regarding distances matrices +#' before running this function. +#' You can find it here:\href{https://hugomflavio.github.io/actel-website/manual-distances.html}{https://hugomflavio.github.io/actel-website/manual-distances.html} +#' +#' @param t.layer A TransitionLayer object, generated by +#' \code{\link{transitionLayer}}. +#' @param starters A data frame with the points from which to start measuring +#' the distance. Ignored if actel = TRUE (default), as the 'spatial.csv' is +#' loaded as starters. +#' @param targets A data frame with the points to which a way must be found. +#' Ignored if actel = TRUE (default), as the 'spatial.csv' is loaded as targets. +#' @param coord.x,coord.y The names of the columns containing the x and y +#' coordinates in the starters and targets. Must be identical in the starters +#' and targets. +#' @param id.col The name of the column containing the IDs of the points to +#' be used as starters and targets. Must be identical in both files. +#' Ignored if actel = TRUE (default), as the stations' standard names are used. +#' @param file_path Specify the file path to the 'spatial.csv' file if not in +#' the main working directory. +#' @param actel Logical: Should the distance matrix be optimized for actel? +#' Defaults to TRUE. #' #' @examples #' \donttest{ #' # check if R can run the distance functions -#' aux <- c( -#' length(suppressWarnings(packageDescription("raster"))), +#' aux <- c(length(suppressWarnings(packageDescription("raster"))), #' length(suppressWarnings(packageDescription("gdistance"))), #' length(suppressWarnings(packageDescription("sp"))), #' length(suppressWarnings(packageDescription("terra")))) @@ -370,8 +383,10 @@ transitionLayer <- function(x, directions = c(16, 8, 4)){ #' #' if (any(missing.packages)) { #' message("Sorry, this function requires packages '", -#' paste(c("raster", "gdistance", "sp", "terra")[missing.packages], collapse = "', '"), -#' "' to operate. Please install ", ifelse(sum(missing.packages) > 1, "them", "it"), +#' paste(c("raster", "gdistance", "sp", "terra")[missing.packages], +#' collapse = "', '"), +#' "' to operate. Please install ", ifelse(sum(missing.packages) > 1, +#' "them", "it"), #' " before proceeding.") #' } else { #' # move to a temporary directory @@ -410,7 +425,7 @@ transitionLayer <- function(x, directions = c(16, 8, 4)){ #' @export #' distancesMatrix <- function(t.layer, starters = NULL, targets = starters, - coord.x = "x", coord.y = "y", id.col = NULL, actel = TRUE){ + coord.x = "x", coord.y = "y", id.col = NULL, file_path = NULL, actel = TRUE){ # initial checks on package presence aux <- c( length(suppressWarnings(packageDescription("raster"))), @@ -419,41 +434,88 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = starters, length(suppressWarnings(packageDescription("terra")))) missing.packages <- sapply(aux, function(x) x == 1) if (any(missing.packages)) { - stop(paste0("This function requires packages '", paste(c("raster", "gdistance", "sp", "terra")[missing.packages], collapse = "', '"), - "' to operate. Please install ", ifelse(sum(missing.packages) > 1, "them", "it"), " before proceeding.\n"), call. = FALSE) + stop(paste0("This function requires packages '", + paste(c("raster", "gdistance", "sp", "terra")[missing.packages], + collapse = "', '"), + "' to operate. Please install ", + ifelse(sum(missing.packages) > 1, "them", "it"), + " before proceeding.\n"), call. = FALSE) } if (!inherits(t.layer, "TransitionLayer")) - stop("Could not recognise 't.layer' as a TransitionLayer object. Make sure to compile it using the function transitionLayer.\n", call. = FALSE) + stop("Could not recognise 't.layer' as a TransitionLayer object. Make", + " sure to compile it using the function transitionLayer.\n", + call. = FALSE) if (!is.null(id.col) && length(id.col) > 1) stop("Please provide only one column name in 'id.col'", call. = FALSE) if (!is.null(id.col) && is.numeric(id.col)) - stop("Please refer to the column name in 'id.col', rather than the column index.\n", call. = FALSE) + stop("Please refer to the column name in 'id.col', rather than the column", + " index.\n", call. = FALSE) if (actel) { message("M: Creating actel-compatible distances matrix."); flush.console() - if (!is.null(starters) | !is.null(targets)) - warning("starters' or 'targets' were set but will be ignored because 'actel' is set to TRUE. Set 'actel' to FALSE to use the 'starters' and 'targets' arguments.", call. = FALSE, immediate. = TRUE) - starters <- targets <- loadSpatial() - if (!is.null(id.col)) - warning("id.col' was set but will be ignored because 'actel' is set to TRUE. Set 'actel' to FALSE to use the 'id.col' argument.", call. = FALSE, immediate. = TRUE) + if (!is.null(starters) | !is.null(targets)) { + warning("starters' or 'targets' were set but will be ignored because", + " 'actel' is set to TRUE. Set 'actel' to FALSE to use the", + " 'starters' and 'targets' arguments.", + call. = FALSE, immediate. = TRUE) + } + # Added new check for 'file_path' being specified in function + if(!is.null(file_path)) { + + # If a 'file path' is specified, the complete file to the 'spatial.csv' is + # constructed + new_path <- paste0(file_path,"spatial.csv") + + # Added check in case 'file_path' did not lead to the 'spatial.csv' with + # stop event + if(!file.exists(new_path)) { + event(type = "stop", + "'File_path' specified to 'spatial.csv' not found in the", + " working directory.") + } + + # Specify new file path to 'spatial.csv' and run as normal + starters <- targets <- loadSpatial(input = new_path) + } else { + + # If no file path is specified, then 'spatial.csv' is assumed to be in + # the main working directory + starters <- targets <- loadSpatial() + } + + if (!is.null(id.col)) { + warning("id.col' was set but will be ignored because 'actel' is set to", + " TRUE. Set 'actel' to FALSE to use the 'id.col' argument.", + call. = FALSE, immediate. = TRUE) + } id.col <- "Standard.name" } - if (!inherits(starters, "data.frame")) + if (!inherits(starters, "data.frame")) { stop("'starters' must be a data frame.\n", call. = FALSE) - if (!inherits(targets, "data.frame")) + } + if (!inherits(targets, "data.frame")) { stop("'targets' must be a data frame.\n", call. = FALSE) + } - if (is.na(match(coord.x, colnames(starters)))) - stop(paste0("Could not find a column '", coord.x, "' in 'starters'."), call. = FALSE) - if (is.na(match(coord.y, colnames(starters)))) - stop(paste0("Could not find a column '", coord.y, "' in 'starters'."), call. = FALSE) - if (is.na(match(coord.x, colnames(targets)))) - stop(paste0("Could not find a column '", coord.x, "' in 'targets'."), call. = FALSE) - if (is.na(match(coord.y, colnames(targets)))) - stop(paste0("Could not find a column '", coord.y, "' in 'targets'."), call. = FALSE) + if (is.na(match(coord.x, colnames(starters)))) { + stop(paste0("Could not find a column '", + coord.x, "' in 'starters'."), call. = FALSE) + } + if (is.na(match(coord.y, colnames(starters)))) { + stop(paste0("Could not find a column '", + coord.y, "' in 'starters'."), call. = FALSE) + } + if (is.na(match(coord.x, colnames(targets)))) { + stop(paste0("Could not find a column '", + coord.x, "' in 'targets'."), call. = FALSE) + } + if (is.na(match(coord.y, colnames(targets)))) { + stop(paste0("Could not find a column '", + coord.y, "' in 'targets'."), call. = FALSE) + } starters <- starters[, c(id.col, coord.x, coord.y)] colnames(starters) <- c(id.col, "longitude", "latitude") @@ -465,25 +527,33 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = starters, if (!is.na(match(id.col, colnames(starters)))) { outputRows <- starters[, id.col] if (any(duplicated(outputRows))) { - warning("The '", id.col, "' column in 'starters' contains duplicated values; skipping row naming.", immediate. = TRUE, call. = FALSE) + warning("The '", id.col, + "' column in 'starters' contains duplicated values;", + " skipping row naming.", immediate. = TRUE, call. = FALSE) row.rename <- FALSE } else { row.rename <- TRUE } } else { - warning("Could not find a '", id.col, "' column in 'starters'; skipping row naming.", immediate. = TRUE, call. = FALSE) + warning("Could not find a '", id.col, + "' column in 'starters'; skipping row naming.", + immediate. = TRUE, call. = FALSE) row.rename <- FALSE } if (!is.na(match(id.col, colnames(targets)))) { outputCols <- targets[, id.col] if (any(duplicated(outputCols))) { - warning("The '", id.col, "' column in 'targets' contains duplicated values; skipping column naming.", immediate. = TRUE, call. = FALSE) + warning("The '", id.col, + "' column in 'targets' contains duplicated values;", + " skipping column naming.", immediate. = TRUE, call. = FALSE) col.rename <- FALSE } else { col.rename <- TRUE } } else { - warning("Could not find a '", id.col, "' column in 'targets'; skipping column naming.", immediate. = TRUE, call. = FALSE) + warning("Could not find a '", id.col, + "' column in 'targets'; skipping column naming.", + immediate. = TRUE, call. = FALSE) col.rename <- FALSE } } else { @@ -493,45 +563,92 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = starters, #### Create starters and targets spatial dataframes - sp::coordinates(starters) <- ~ longitude + latitude # converts the file to a spatialPoints object - raster::crs(starters) <- raster::crs(t.layer) # sets the crs - - sp::coordinates(targets) <- ~ longitude + latitude # converts the file to a spatialPoints object + # converts the file to a spatialPoints object + sp::coordinates(starters) <- ~ longitude + latitude + + # sets the crs + raster::crs(starters) <- raster::crs(t.layer) + + # converts the file to a spatialPoints object + sp::coordinates(targets) <- ~ longitude + latitude raster::crs(targets) <- raster::crs(t.layer) # NOTE: THE LINES ABOVE COULD BE CHANGED ONCE gdistance'S # FUNCTIONS START LIKING SF OBJECTS LAYER - # starters <- sf::st_as_sf(starters, coords = c("longitude","latitude"), crs = ...) - # targets <- sf::st_as_sf(targets, coords = c("longitude","latitude"), crs = ...) + # starters <- sf::st_as_sf(starters, coords = c("longitude","latitude"), + # crs = ...) + # targets <- sf::st_as_sf(targets, coords = c("longitude","latitude"), + # crs = ...) # NOTE: currently, transition layer objects are not # responding correctly to crs requests (e.g. sf::st_crs) #### Calculate a matrix of distances to each object dist.mat <- data.frame(gdistance::costDistance(t.layer, starters, targets)) if (any(dist.mat == Inf)) { - warning("At least one station is completely blocked off from the remaining stations by land. Filling -the respective fields with NA. If your animals were expected to travel around the areas present -in the shape file, consider applying a 'buffer' when calculating the transition layer. This -will artificially add water space around the shape file.", call. = FALSE) + warning("At least one station is completely blocked off from the remaining", + " stations by land. Filling the respective fields with NA.", + " If your animals were expected to travel around the areas present", + " in the shape file, consider applying a 'buffer' when calculating", + " the transition layer. This will artificially add water space", + " around the shape file.", call. = FALSE) dist.mat[dist.mat == Inf] <- NA } - if (row.rename) + if (row.rename) { rownames(dist.mat) <- outputRows + } - if (col.rename) + if (col.rename) { colnames(dist.mat) <- outputCols + } if (interactive() & actel) { # nocov start - decision <- userInput("Would you like to save an actel-compatible distances matrix as 'distances.csv' in the current work directory?(y/n) ", + decision <- userInput("Would you like to save an actel-compatible", + " distances matrix as 'distances.csv' in the", + " current work directory?(y/n) ", choices = c("y", "n")) + if (decision == "y") { - if (file.exists('distances.csv')) { - warning("A file 'distances.csv' is already present in the current directory.", call. = FALSE, immediate. = TRUE) - decision <- userInput("Continuing will overwrite this file. Would you like to continue?(y/n) ", choices = c("y", "n")) + + if(!is.null(file_path)) { + + # If a 'file_path' has been specified, a new path is created where + # 'distances.csv' will be saved + new_path <- paste0(file_path,"distances.csv") + + # Checks if 'distances.csv' is already in the file path specified + if (file.exists(new_path)) { + + warning("A file 'distances.csv' is already present in the current", + " directory.", call. = FALSE, immediate. = TRUE) + + decision <- userInput("Continuing will overwrite this file. Would", + " you like to continue?(y/n) ", + choices = c("y", "n")) + } + + if (decision == "y") { + + write.csv(round(dist.mat, 0), new_path, row.names = TRUE) + + } + } else { + # If no 'file_path' specified, then 'distances.csv' is saved to the + # current working directory + if (file.exists('distances.csv')) { + warning("A file 'distances.csv' is already present in the current", + " directory.", call. = FALSE, immediate. = TRUE) + + decision <- userInput("Continuing will overwrite this file. Would", + " you like to continue?(y/n) ", + choices = c("y", "n")) } - if (decision == "y") + if (decision == "y") { write.csv(round(dist.mat, 0), "distances.csv", row.names = TRUE) + } + } + + } } # nocov end return(round(dist.mat, 0)) From e35192decc0f8769487d05dd64022dc8e55ae3f1 Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Tue, 12 Nov 2024 18:19:01 -0800 Subject: [PATCH 03/29] Updated documentation for distanceMatrix() function --- man/distancesMatrix.Rd | 49 ++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/man/distancesMatrix.Rd b/man/distancesMatrix.Rd index 5a1beab..eb4b8f9 100644 --- a/man/distancesMatrix.Rd +++ b/man/distancesMatrix.Rd @@ -11,40 +11,55 @@ distancesMatrix( coord.x = "x", coord.y = "y", id.col = NULL, + file_path = NULL, actel = TRUE ) } \arguments{ -\item{t.layer}{A TransitionLayer object, generated by \code{\link{transitionLayer}}.} +\item{t.layer}{A TransitionLayer object, generated by +\code{\link{transitionLayer}}.} -\item{starters}{A data frame with the points from which to start measuring the distance. Ignored if actel = TRUE (default), as the 'spatial.csv' is loaded as starters.} +\item{starters}{A data frame with the points from which to start measuring +the distance. Ignored if actel = TRUE (default), as the 'spatial.csv' is +loaded as starters.} -\item{targets}{A data frame with the points to which a way must be found. Ignored if actel = TRUE (default), as the 'spatial.csv' is loaded as targets.} +\item{targets}{A data frame with the points to which a way must be found. +Ignored if actel = TRUE (default), as the 'spatial.csv' is loaded as targets.} -\item{coord.x, coord.y}{The names of the columns containing the x and y coordinates in the starters and targets. Must be identical in the starters and targets.} +\item{coord.x, coord.y}{The names of the columns containing the x and y +coordinates in the starters and targets. Must be identical in the starters +and targets.} -\item{id.col}{The name of the column containing the IDs of the points to be used as starters and targets. Must be identical in both files. Ignored if actel = TRUE (default), as the stations' standard names are used.} +\item{id.col}{The name of the column containing the IDs of the points to +be used as starters and targets. Must be identical in both files. +Ignored if actel = TRUE (default), as the stations' standard names are used.} -\item{actel}{Logical: Should the distance matrix be optimized for actel? Defaults to TRUE.} +\item{file_path}{Specify the file path to the 'spatial.csv' file if not in +the main working directory.} + +\item{actel}{Logical: Should the distance matrix be optimized for actel? +Defaults to TRUE.} } \value{ A matrix with the distances between each pair of points. } \description{ -Using a previously created transition layer (see \code{\link{transitionLayer}}), calculates the distances -between spatial points. Adapted from Grant Adams' script "distance to closest mpa". if the argument 'actel' -is set to TRUE (default), an actel-compatible matrix is generated, and the user will be asked if they would -like to save the matrix as 'distances.csv' in the current directory. +Using a previously created transition layer (see \code{\link{transitionLayer}}), +calculates the distances between spatial points. Adapted from Grant Adams' +script "distance to closest mpa". If the argument 'actel' is set to +TRUE (default), an actel-compatible matrix is generated, and the user will be +asked if they would like to save the matrix as 'distances.csv' in the current +directory. } \details{ -It is highly recommended to read the manual page regarding distances matrices before running this function. -You can find it here: \href{https://hugomflavio.github.io/actel-website/manual-distances.html}{https://hugomflavio.github.io/actel-website/manual-distances.html} +It is highly recommended to read the manual page regarding distances matrices +before running this function. +You can find it here:\href{https://hugomflavio.github.io/actel-website/manual-distances.html}{https://hugomflavio.github.io/actel-website/manual-distances.html} } \examples{ \donttest{ # check if R can run the distance functions -aux <- c( - length(suppressWarnings(packageDescription("raster"))), +aux <- c(length(suppressWarnings(packageDescription("raster"))), length(suppressWarnings(packageDescription("gdistance"))), length(suppressWarnings(packageDescription("sp"))), length(suppressWarnings(packageDescription("terra")))) @@ -53,8 +68,10 @@ missing.packages <- sapply(aux, function(x) x == 1) if (any(missing.packages)) { message("Sorry, this function requires packages '", - paste(c("raster", "gdistance", "sp", "terra")[missing.packages], collapse = "', '"), - "' to operate. Please install ", ifelse(sum(missing.packages) > 1, "them", "it"), + paste(c("raster", "gdistance", "sp", "terra")[missing.packages], + collapse = "', '"), + "' to operate. Please install ", ifelse(sum(missing.packages) > 1, + "them", "it"), " before proceeding.") } else { # move to a temporary directory From 50fd11b471fb92d9b227c589f7b4cca72905dbbc Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Tue, 12 Nov 2024 18:19:49 -0800 Subject: [PATCH 04/29] Had to adjust distanceMatrix() test since compressing to 80c changed how a check operated when looking for a warning. --- tests/testthat/test_distancesMatrix_functions.R | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test_distancesMatrix_functions.R b/tests/testthat/test_distancesMatrix_functions.R index 4029393..c309365 100644 --- a/tests/testthat/test_distancesMatrix_functions.R +++ b/tests/testthat/test_distancesMatrix_functions.R @@ -103,10 +103,12 @@ if (any(missing.packages)) { test_that("distancesMatrix produces a warning when there are no viable passages between stations", { expect_warning(dist.mat <- distancesMatrix(t.layer = t.layer, coord.x = "x.32632", coord.y = "y.32632", actel = TRUE), - "At least one station is completely blocked off from the remaining stations by land. Filling -the respective fields with NA. If your animals were expected to travel around the areas present -in the shape file, consider applying a 'buffer' when calculating the transition layer. This -will artificially add water space around the shape file.", fixed = TRUE) + paste0("At least one station is completely blocked off from the remaining", + " stations by land. Filling the respective fields with NA.", + " If your animals were expected to travel around the areas present", + " in the shape file, consider applying a 'buffer' when calculating", + " the transition layer. This will artificially add water space", + " around the shape file."), fixed = TRUE) }) # n From 852f215e316c0e22ff2c9a6c629d474288529dd5 Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Wed, 13 Nov 2024 13:17:19 -0800 Subject: [PATCH 05/29] Minor updates to allow checks to pass --- R/distances.R | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/R/distances.R b/R/distances.R index 8e283e2..029d94b 100644 --- a/R/distances.R +++ b/R/distances.R @@ -472,8 +472,7 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = starters, # stop event if(!file.exists(new_path)) { event(type = "stop", - "'File_path' specified to 'spatial.csv' not found in the", - " working directory.") + "'spatial.csv' not found in the 'file_path' specified") } # Specify new file path to 'spatial.csv' and run as normal @@ -603,9 +602,9 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = starters, } if (interactive() & actel) { # nocov start - decision <- userInput("Would you like to save an actel-compatible", + decision <- userInput(paste0("Would you like to save an actel-compatible", " distances matrix as 'distances.csv' in the", - " current work directory?(y/n) ", + " current work directory?(y/n) "), choices = c("y", "n")) if (decision == "y") { @@ -622,8 +621,8 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = starters, warning("A file 'distances.csv' is already present in the current", " directory.", call. = FALSE, immediate. = TRUE) - decision <- userInput("Continuing will overwrite this file. Would", - " you like to continue?(y/n) ", + decision <- userInput(paste0("Continuing will overwrite this file.", + " Would you like to continue?(y/n) "), choices = c("y", "n")) } @@ -639,8 +638,8 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = starters, warning("A file 'distances.csv' is already present in the current", " directory.", call. = FALSE, immediate. = TRUE) - decision <- userInput("Continuing will overwrite this file. Would", - " you like to continue?(y/n) ", + decision <- userInput(paste0("Continuing will overwrite this file.", + " Would you like to continue?(y/n) "), choices = c("y", "n")) } if (decision == "y") { From 42325f8d0acf2bb85ee1ca43aaf6bd2330908bca Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Wed, 13 Nov 2024 13:18:25 -0800 Subject: [PATCH 06/29] added new checks to test `distanceMatrix()` 'file_path' addition. --- .../testthat/test_distancesMatrix_functions.R | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/testthat/test_distancesMatrix_functions.R b/tests/testthat/test_distancesMatrix_functions.R index c309365..05912b4 100644 --- a/tests/testthat/test_distancesMatrix_functions.R +++ b/tests/testthat/test_distancesMatrix_functions.R @@ -112,6 +112,12 @@ if (any(missing.packages)) { }) # n + test_that("distancesMatrix handles missing sptial.csv when file_path used", { + expect_error(output <- distancesMatrix(t.layer = t.layer, coord.x = "x.32632", + coord.y = "y.32632", file_path = "file_test"), + "'spatial.csv' not found in the 'file_path' specified") + }) + test_that("distancesMatrix handles bad data correctly pt1", { expect_error(distancesMatrix(t.layer = t.layer, id.col = 1:2), "Please provide only one column name in 'id.col'", fixed = TRUE) @@ -165,6 +171,23 @@ if (any(missing.packages)) { "Could not find a column 'test' in 'targets'.", fixed = TRUE) }) + dir.create("file_test") + + write.csv(xspatial, "file_test/spatial.csv", row.names = FALSE) + + test_that("distancesMatrix output is as expected when file_path used", { + output <- distancesMatrix(t.layer = t.layer, coord.x = "x.32632", + coord.y = "y.32632", file_path = "file_test/") + expect_equal(colnames(output), paste("St", 1:4, sep = ".")) + expect_equal(rownames(output), paste("St", 1:4, sep = ".")) + expect_equal(output[, 1], c( 0, 586, 934, 1154)) + expect_equal(output[, 2], c( 586, 0, 490, 656)) + expect_equal(output[, 3], c( 934, 490, 0, 237)) + expect_equal(output[, 4], c(1154, 656, 237, 0)) + }) + + unlink("file_test", recursive = TRUE) + test_that("distancesMatrix output is as expected", { output <- distancesMatrix(t.layer = t.layer, coord.x = "x.32632", coord.y = "y.32632") expect_equal(colnames(output), paste("St", 1:4, sep = ".")) From 14290a8df3ce67cfdea25c1e15f596961c4c8a91 Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Tue, 19 Nov 2024 12:28:36 -0800 Subject: [PATCH 07/29] Updated news description based on changes made to 'distancesMatrix()' --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 77a2971..b836340 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,7 +22,7 @@ Enhancements: * New `convertLotekCDMAFile()` function to prepare lotek txt logs for further analyses (issue [#103](https://github.com/hugomflavio/actel/issues/103)). * New `plotDot()` function to help users inspect their spatial.txt files (issue [#55](https://github.com/hugomflavio/actel/issues/55)). * Improved internal mechanisms for message/warning/debug/stop reporting. new internal function `event()` replaces the old `appendTo()`, `stopAndReport()`, and `emergencyBreak()` (issue [#135](https://github.com/hugomflavio/actel/issues/135)) - * Added the ability to specify a file pathway for `distancesMatrix()` if the 'spatial.csv' is not in the main working directory. (issue [#133](https://github.com/hugomflavio/actel/issues/133)) + * Added the ability to use output of `loadSpatial()` as 'starters' for `distancesMatrix()` when 'actel' is 'TRUE'. This allows a user to input their spatial.csv if located in a separate working directory (issue [#133](https://github.com/hugomflavio/actel/issues/133)) ## actel 1.3.0 From ec35ecc9b266a26ddff8438b865ba79c68f973a9 Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Tue, 19 Nov 2024 12:29:53 -0800 Subject: [PATCH 08/29] Updated help documentation for 'distancesMatrix()' where the output of 'loadSpatial()' can now be used in place of starters when actel == TRUE --- man/distancesMatrix.Rd | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/man/distancesMatrix.Rd b/man/distancesMatrix.Rd index eb4b8f9..8e3ca16 100644 --- a/man/distancesMatrix.Rd +++ b/man/distancesMatrix.Rd @@ -7,11 +7,10 @@ distancesMatrix( t.layer, starters = NULL, - targets = starters, + targets = NULL, coord.x = "x", coord.y = "y", id.col = NULL, - file_path = NULL, actel = TRUE ) } @@ -20,11 +19,9 @@ distancesMatrix( \code{\link{transitionLayer}}.} \item{starters}{A data frame with the points from which to start measuring -the distance. Ignored if actel = TRUE (default), as the 'spatial.csv' is -loaded as starters.} +the distance or the output of \code{loadSpatial()}.} -\item{targets}{A data frame with the points to which a way must be found. -Ignored if actel = TRUE (default), as the 'spatial.csv' is loaded as targets.} +\item{targets}{A data frame with the points to which a way must be found.} \item{coord.x, coord.y}{The names of the columns containing the x and y coordinates in the starters and targets. Must be identical in the starters @@ -34,9 +31,6 @@ and targets.} be used as starters and targets. Must be identical in both files. Ignored if actel = TRUE (default), as the stations' standard names are used.} -\item{file_path}{Specify the file path to the 'spatial.csv' file if not in -the main working directory.} - \item{actel}{Logical: Should the distance matrix be optimized for actel? Defaults to TRUE.} } From 46d5e7852efd8a4076722c0c828577a7506090f1 Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Tue, 19 Nov 2024 12:30:46 -0800 Subject: [PATCH 09/29] Some minor check fixes to address new 'distancesMatrix()' changes. Removed old checks related to file_path and removed checks which checked for errors or warning no longer present. --- .../testthat/test_distancesMatrix_functions.R | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/tests/testthat/test_distancesMatrix_functions.R b/tests/testthat/test_distancesMatrix_functions.R index 05912b4..fbd24c4 100644 --- a/tests/testthat/test_distancesMatrix_functions.R +++ b/tests/testthat/test_distancesMatrix_functions.R @@ -111,12 +111,6 @@ if (any(missing.packages)) { " around the shape file."), fixed = TRUE) }) # n - - test_that("distancesMatrix handles missing sptial.csv when file_path used", { - expect_error(output <- distancesMatrix(t.layer = t.layer, coord.x = "x.32632", - coord.y = "y.32632", file_path = "file_test"), - "'spatial.csv' not found in the 'file_path' specified") - }) test_that("distancesMatrix handles bad data correctly pt1", { expect_error(distancesMatrix(t.layer = t.layer, id.col = 1:2), @@ -134,13 +128,9 @@ if (any(missing.packages)) { "'starters' must be a data frame.", fixed = TRUE) file.remove("test.txt") - - expect_warning(distancesMatrix(t.layer = t.layer, - coord.x = "x.32632", coord.y = "y.32632", starters = "test", id.col = "test", actel = TRUE), - "starters' or 'targets' were set but will be ignored because 'actel' is set to TRUE. Set 'actel' to FALSE to use the 'starters' and 'targets' arguments.", fixed = TRUE) expect_warning(distancesMatrix(t.layer = t.layer, - coord.x = "x.32632", coord.y = "y.32632", starters = "test", id.col = "test", actel = TRUE), + coord.x = "x.32632", coord.y = "y.32632", id.col = "test", actel = TRUE), "id.col' was set but will be ignored because 'actel' is set to TRUE. Set 'actel' to FALSE to use the 'id.col' argument.", fixed = TRUE) }) # n @@ -170,14 +160,12 @@ if (any(missing.packages)) { coord.x = "x.32632", coord.y = "test", starters = loadSpatial("spatial2.csv"), targets = loadSpatial(), actel = FALSE), "Could not find a column 'test' in 'targets'.", fixed = TRUE) }) - - dir.create("file_test") - write.csv(xspatial, "file_test/spatial.csv", row.names = FALSE) + test_loadspatial <- loadSpatial() - test_that("distancesMatrix output is as expected when file_path used", { + test_that("distancesMatrix output is as expected when output of loadSpatial() is used", { output <- distancesMatrix(t.layer = t.layer, coord.x = "x.32632", - coord.y = "y.32632", file_path = "file_test/") + coord.y = "y.32632", starters = test_loadspatial) expect_equal(colnames(output), paste("St", 1:4, sep = ".")) expect_equal(rownames(output), paste("St", 1:4, sep = ".")) expect_equal(output[, 1], c( 0, 586, 934, 1154)) @@ -186,7 +174,7 @@ if (any(missing.packages)) { expect_equal(output[, 4], c(1154, 656, 237, 0)) }) - unlink("file_test", recursive = TRUE) + rm(test_loadspatial) test_that("distancesMatrix output is as expected", { output <- distancesMatrix(t.layer = t.layer, coord.x = "x.32632", coord.y = "y.32632") From 4b74870660f8177498d4a2a881a891e70569acc7 Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Tue, 19 Nov 2024 12:32:37 -0800 Subject: [PATCH 10/29] Modified 'distancesMatrix()' function to allow starters and targets to be specified when actel == TRUE. This also allows 'loadSpatial()' to be used in starters to specify if the 'spatial.csv' is in a different working directory. Also updated code to allow 'distances.csv' to be saved to a specific folder if the user desires. --- R/distances.R | 114 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 77 insertions(+), 37 deletions(-) diff --git a/R/distances.R b/R/distances.R index 029d94b..12cb3dd 100644 --- a/R/distances.R +++ b/R/distances.R @@ -356,18 +356,14 @@ transitionLayer <- function(x, directions = c(16, 8, 4)){ #' @param t.layer A TransitionLayer object, generated by #' \code{\link{transitionLayer}}. #' @param starters A data frame with the points from which to start measuring -#' the distance. Ignored if actel = TRUE (default), as the 'spatial.csv' is -#' loaded as starters. +#' the distance or the output of `loadSpatial()`. #' @param targets A data frame with the points to which a way must be found. -#' Ignored if actel = TRUE (default), as the 'spatial.csv' is loaded as targets. #' @param coord.x,coord.y The names of the columns containing the x and y #' coordinates in the starters and targets. Must be identical in the starters #' and targets. #' @param id.col The name of the column containing the IDs of the points to #' be used as starters and targets. Must be identical in both files. #' Ignored if actel = TRUE (default), as the stations' standard names are used. -#' @param file_path Specify the file path to the 'spatial.csv' file if not in -#' the main working directory. #' @param actel Logical: Should the distance matrix be optimized for actel? #' Defaults to TRUE. #' @@ -424,8 +420,8 @@ transitionLayer <- function(x, directions = c(16, 8, 4)){ #' #' @export #' -distancesMatrix <- function(t.layer, starters = NULL, targets = starters, - coord.x = "x", coord.y = "y", id.col = NULL, file_path = NULL, actel = TRUE){ +distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, + coord.x = "x", coord.y = "y", id.col = NULL, actel = TRUE){ # initial checks on package presence aux <- c( length(suppressWarnings(packageDescription("raster"))), @@ -455,35 +451,44 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = starters, if (actel) { message("M: Creating actel-compatible distances matrix."); flush.console() - if (!is.null(starters) | !is.null(targets)) { - warning("starters' or 'targets' were set but will be ignored because", - " 'actel' is set to TRUE. Set 'actel' to FALSE to use the", - " 'starters' and 'targets' arguments.", - call. = FALSE, immediate. = TRUE) - } - # Added new check for 'file_path' being specified in function - if(!is.null(file_path)) { - - # If a 'file path' is specified, the complete file to the 'spatial.csv' is - # constructed - new_path <- paste0(file_path,"spatial.csv") - - # Added check in case 'file_path' did not lead to the 'spatial.csv' with - # stop event - if(!file.exists(new_path)) { - event(type = "stop", - "'spatial.csv' not found in the 'file_path' specified") + + if(!is.null(starters) | !is.null(targets)) { + # distancesMatrix() crashes if actel = TRUE and starters/targets were + # supplied but do not contain a column called "Standard.name" + if(!is.null(starters) & !any(grepl(c("Standard.name"), + colnames(starters)))) { + event("'starters' is missing column called 'Standard.name'", + type = "stop") } - - # Specify new file path to 'spatial.csv' and run as normal - starters <- targets <- loadSpatial(input = new_path) - } else { - # If no file path is specified, then 'spatial.csv' is assumed to be in - # the main working directory - starters <- targets <- loadSpatial() + if(!is.null(targets) & !any(grepl(c("Standard.name"), + colnames(targets)))) { + event("'targets' is missing column called 'Standard.name'", + type = "stop") + } + } + + if (!is.null(starters) & !is.null(targets)) { + warning("'starters' and 'targets' were specified while 'actel'", + " is set to TRUE. Using 'starters' and 'targets' for distances.", + call. = FALSE, immediate. = TRUE) } + if (!is.null(starters) & is.null(targets)) { + warning("'starters' were specified but 'targets' were not while 'actel'", + " is set to TRUE. Using 'starters' for 'targets'.", + call. = FALSE, immediate. = TRUE) + + targets <- starters + } + + if (is.null(starters) & is.null(targets)) { + warning("'starters' and 'targets' were not set while 'actel' is set to", + " TRUE. Creating 'starters' and 'targets' from 'spatial.csv' ", + " in working directory." , call. = FALSE, immediate. = TRUE) + starters <- targets <- loadSpatial() + } + if (!is.null(id.col)) { warning("id.col' was set but will be ignored because 'actel' is set to", " TRUE. Set 'actel' to FALSE to use the 'id.col' argument.", @@ -491,6 +496,14 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = starters, } id.col <- "Standard.name" } + + # Failsafe from old way code worked where targets used starters by default + # when actel == FALSE + if(!actel) { + if(is.null(targets)) { + targets <- starters + } + } if (!inherits(starters, "data.frame")) { stop("'starters' must be a data frame.\n", call. = FALSE) @@ -603,17 +616,44 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = starters, if (interactive() & actel) { # nocov start decision <- userInput(paste0("Would you like to save an actel-compatible", - " distances matrix as 'distances.csv' in the", - " current work directory?(y/n) "), + " distances matrix as 'distances.csv'?(y/n)"), choices = c("y", "n")) - + if (decision == "y") { + dir_decision <- userInput(paste0("Would you like 'distances.csv' saved in", + " the current working directory?(y/n)"), + choices = c("y", "n")) + if (dir_decision == "n") { + + target_dir <- "" + + while(!dir.exists(target_dir)){ + target_dir <- readline(paste0("Specify the folder to save 'distances.csv'", + " to. Hit enter to skip.")) + if(nchar(target_dir) == 0) { + dir_decision <- "y" + {break} + } + + if(!dir.exists(target_dir)) { + dir_decision <- userInput(paste0("Directory specified not found.", + " Do you want to continue?(y/n)"), + choices = c("y", "n")) + if(dir_decision == "n") { + dir_decision <- "y" + {break} + } + } + } + } + } + if (decision == "y") { - if(!is.null(file_path)) { + if(dir_decision == "n") { # If a 'file_path' has been specified, a new path is created where # 'distances.csv' will be saved - new_path <- paste0(file_path,"distances.csv") + new_path <- paste(target_dir,"distances.csv", sep = "/") # Checks if 'distances.csv' is already in the file path specified if (file.exists(new_path)) { From 127d326852610e882e5188a8378509558c6ed991 Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Thu, 5 Dec 2024 10:33:20 -0800 Subject: [PATCH 11/29] Removed curly brackets from break. Condensed write.csv and check for distances.csv existing. Fixed a bracket space. --- R/distances.R | 59 +++++++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/R/distances.R b/R/distances.R index 12cb3dd..b774928 100644 --- a/R/distances.R +++ b/R/distances.R @@ -626,12 +626,12 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, target_dir <- "" - while(!dir.exists(target_dir)){ + while(!dir.exists(target_dir)) { target_dir <- readline(paste0("Specify the folder to save 'distances.csv'", " to. Hit enter to skip.")) if(nchar(target_dir) == 0) { dir_decision <- "y" - {break} + break } if(!dir.exists(target_dir)) { @@ -640,54 +640,43 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, choices = c("y", "n")) if(dir_decision == "n") { dir_decision <- "y" - {break} + break } } } } - } + } if (decision == "y") { if(dir_decision == "n") { - # If a 'file_path' has been specified, a new path is created where - # 'distances.csv' will be saved - new_path <- paste(target_dir,"distances.csv", sep = "/") + # If a different directory has been specified, a new path is created + # where 'distances.csv' will be saved + path <- paste(target_dir,"distances.csv", sep = "/") + + } else { + # If no 'path' specified, then 'distances.csv' is saved to the + # current working directory + + path <- paste("distances.csv", sep = "/") - # Checks if 'distances.csv' is already in the file path specified - if (file.exists(new_path)) { - - warning("A file 'distances.csv' is already present in the current", - " directory.", call. = FALSE, immediate. = TRUE) - - decision <- userInput(paste0("Continuing will overwrite this file.", - " Would you like to continue?(y/n) "), - choices = c("y", "n")) } + # Checks if 'distances.csv' is already in the file path specified + if (file.exists(path)) { - if (decision == "y") { - - write.csv(round(dist.mat, 0), new_path, row.names = TRUE) - - } - } else { - # If no 'file_path' specified, then 'distances.csv' is saved to the - # current working directory - if (file.exists('distances.csv')) { - warning("A file 'distances.csv' is already present in the current", - " directory.", call. = FALSE, immediate. = TRUE) - - decision <- userInput(paste0("Continuing will overwrite this file.", - " Would you like to continue?(y/n) "), - choices = c("y", "n")) + warning("A file 'distances.csv' is already present in the current", + " directory.", call. = FALSE, immediate. = TRUE) + + decision <- userInput(paste0("Continuing will overwrite this file.", + " Would you like to continue?(y/n) "), + choices = c("y", "n")) } if (decision == "y") { - write.csv(round(dist.mat, 0), "distances.csv", row.names = TRUE) - } + + write.csv(round(dist.mat, 0), path, row.names = TRUE) + } - - } } # nocov end return(round(dist.mat, 0)) From 549c3062fd0614423848897835c02ecb8151599d Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:18:20 -0800 Subject: [PATCH 12/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit accepted Hugo's change Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/distances.R b/R/distances.R index b774928..746d491 100644 --- a/R/distances.R +++ b/R/distances.R @@ -655,13 +655,13 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, # where 'distances.csv' will be saved path <- paste(target_dir,"distances.csv", sep = "/") - } else { + } else { # If no 'path' specified, then 'distances.csv' is saved to the # current working directory path <- paste("distances.csv", sep = "/") - } + } # Checks if 'distances.csv' is already in the file path specified if (file.exists(path)) { From 4ca4199249ec0c367c8a4ad58b42738125b2b43f Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:19:02 -0800 Subject: [PATCH 13/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removal of paste0() as per Hugo's requested change. Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/distances.R b/R/distances.R index 746d491..e0e07da 100644 --- a/R/distances.R +++ b/R/distances.R @@ -430,12 +430,12 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, length(suppressWarnings(packageDescription("terra")))) missing.packages <- sapply(aux, function(x) x == 1) if (any(missing.packages)) { - stop(paste0("This function requires packages '", - paste(c("raster", "gdistance", "sp", "terra")[missing.packages], - collapse = "', '"), - "' to operate. Please install ", - ifelse(sum(missing.packages) > 1, "them", "it"), - " before proceeding.\n"), call. = FALSE) + stop("This function requires packages '", + paste(c("raster", "gdistance", "sp", "terra")[missing.packages], + collapse = "', '"), + "' to operate. Please install ", + ifelse(sum(missing.packages) > 1, "them", "it"), + " before proceeding.\n", call. = FALSE) } if (!inherits(t.layer, "TransitionLayer")) From e2e51e39b7f6148d46e59248357eda128b8d75aa Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:19:49 -0800 Subject: [PATCH 14/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Acceptance of Hugo;s change of & to && Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/distances.R b/R/distances.R index e0e07da..c139d61 100644 --- a/R/distances.R +++ b/R/distances.R @@ -455,8 +455,7 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, if(!is.null(starters) | !is.null(targets)) { # distancesMatrix() crashes if actel = TRUE and starters/targets were # supplied but do not contain a column called "Standard.name" - if(!is.null(starters) & !any(grepl(c("Standard.name"), - colnames(starters)))) { + if(!is.null(starters) && !("Standard.name" %in% colnames(starters))) { event("'starters' is missing column called 'Standard.name'", type = "stop") } From 7a9ef9b61c6b768146afe3af3f2139b41a65f905 Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:20:19 -0800 Subject: [PATCH 15/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Retention of stop() instead of event() Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/distances.R b/R/distances.R index c139d61..a9b6160 100644 --- a/R/distances.R +++ b/R/distances.R @@ -456,8 +456,8 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, # distancesMatrix() crashes if actel = TRUE and starters/targets were # supplied but do not contain a column called "Standard.name" if(!is.null(starters) && !("Standard.name" %in% colnames(starters))) { - event("'starters' is missing column called 'Standard.name'", - type = "stop") + stop("'starters' is missing column called 'Standard.name'", + call. = FALSE) } if(!is.null(targets) & !any(grepl(c("Standard.name"), From 21a97c0faa1a126e17a06b0a2fe608ec02f48dbb Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:20:49 -0800 Subject: [PATCH 16/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Retention of stop() instead of event() Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/distances.R b/R/distances.R index a9b6160..4ac9d73 100644 --- a/R/distances.R +++ b/R/distances.R @@ -462,8 +462,8 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, if(!is.null(targets) & !any(grepl(c("Standard.name"), colnames(targets)))) { - event("'targets' is missing column called 'Standard.name'", - type = "stop") + stop("'targets' is missing column called 'Standard.name'", + call. = FALSE) } } From a838e9bd81c775a04cd1654896f53c6691cd7d6a Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:21:40 -0800 Subject: [PATCH 17/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Compression of code Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/distances.R b/R/distances.R index 4ac9d73..8fb1dab 100644 --- a/R/distances.R +++ b/R/distances.R @@ -626,8 +626,9 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, target_dir <- "" while(!dir.exists(target_dir)) { - target_dir <- readline(paste0("Specify the folder to save 'distances.csv'", - " to. Hit enter to skip.")) + target_dir <- readline(paste0("Specify the folder to save", + " 'distances.csv' to.", + " Hit enter to skip.")) if(nchar(target_dir) == 0) { dir_decision <- "y" break From 86ac9d5008149dc3e5dcab8ad0c197418abb6011 Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:22:06 -0800 Subject: [PATCH 18/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addition of missing space Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/distances.R b/R/distances.R index 8fb1dab..cff38dd 100644 --- a/R/distances.R +++ b/R/distances.R @@ -629,7 +629,7 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, target_dir <- readline(paste0("Specify the folder to save", " 'distances.csv' to.", " Hit enter to skip.")) - if(nchar(target_dir) == 0) { + if (nchar(target_dir) == 0) { dir_decision <- "y" break } From 2db30e95377958bd82f5e0849fe30b5962af1b9a Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:22:23 -0800 Subject: [PATCH 19/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Format changes Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/distances.R b/R/distances.R index cff38dd..b1529ac 100644 --- a/R/distances.R +++ b/R/distances.R @@ -634,11 +634,11 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, break } - if(!dir.exists(target_dir)) { + if (!dir.exists(target_dir)) { dir_decision <- userInput(paste0("Directory specified not found.", " Do you want to continue?(y/n)"), - choices = c("y", "n")) - if(dir_decision == "n") { + choices = c("y", "n")) + if (dir_decision == "n") { dir_decision <- "y" break } From 4ec30af73e9520519552e2609279a5cfd264d163 Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:22:50 -0800 Subject: [PATCH 20/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use of paste0() instead of paste() Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/distances.R b/R/distances.R index b1529ac..3dd756e 100644 --- a/R/distances.R +++ b/R/distances.R @@ -653,7 +653,7 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, # If a different directory has been specified, a new path is created # where 'distances.csv' will be saved - path <- paste(target_dir,"distances.csv", sep = "/") + path <- paste0(target_dir, "/distances.csv") } else { # If no 'path' specified, then 'distances.csv' is saved to the From 19e8731deb1636c6149a784f99db5a608480802b Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:23:18 -0800 Subject: [PATCH 21/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Format changes Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/distances.R b/R/distances.R index 3dd756e..5a544c3 100644 --- a/R/distances.R +++ b/R/distances.R @@ -514,11 +514,11 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, if (is.na(match(coord.x, colnames(starters)))) { stop(paste0("Could not find a column '", coord.x, "' in 'starters'."), call. = FALSE) - } + } if (is.na(match(coord.y, colnames(starters)))) { stop(paste0("Could not find a column '", coord.y, "' in 'starters'."), call. = FALSE) - } + } if (is.na(match(coord.x, colnames(targets)))) { stop(paste0("Could not find a column '", coord.x, "' in 'targets'."), call. = FALSE) @@ -526,7 +526,7 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, if (is.na(match(coord.y, colnames(targets)))) { stop(paste0("Could not find a column '", coord.y, "' in 'targets'."), call. = FALSE) - } + } starters <- starters[, c(id.col, coord.x, coord.y)] colnames(starters) <- c(id.col, "longitude", "latitude") From 866c8ea81795227334a0703b4fe8c542eed7743e Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:23:43 -0800 Subject: [PATCH 22/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit minor format changes Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/distances.R b/R/distances.R index 5a544c3..6ffb53b 100644 --- a/R/distances.R +++ b/R/distances.R @@ -574,7 +574,7 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, #### Create starters and targets spatial dataframes - # converts the file to a spatialPoints object + # converts the file to a spatialPoints object sp::coordinates(starters) <- ~ longitude + latitude # sets the crs From e3fdca4afb0c48600705f2a270c836238f653b06 Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:24:00 -0800 Subject: [PATCH 23/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit minor format changes Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/distances.R b/R/distances.R index 6ffb53b..087265b 100644 --- a/R/distances.R +++ b/R/distances.R @@ -611,7 +611,7 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, if (col.rename) { colnames(dist.mat) <- outputCols - } + } if (interactive() & actel) { # nocov start decision <- userInput(paste0("Would you like to save an actel-compatible", From 1c6354722c2d153fd77e0e606cf65268cdab1a9b Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:24:21 -0800 Subject: [PATCH 24/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit code compression Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/R/distances.R b/R/distances.R index 087265b..5f7fd9f 100644 --- a/R/distances.R +++ b/R/distances.R @@ -618,9 +618,10 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, " distances matrix as 'distances.csv'?(y/n)"), choices = c("y", "n")) if (decision == "y") { - dir_decision <- userInput(paste0("Would you like 'distances.csv' saved in", - " the current working directory?(y/n)"), - choices = c("y", "n")) + dir_decision <- userInput(paste0("Would you like 'distances.csv'", + " saved in the current working", + " directory?(y/n)"), + choices = c("y", "n")) if (dir_decision == "n") { target_dir <- "" From 6c5fd8a43a191348b0aba74ae8684806e4953857 Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:24:37 -0800 Subject: [PATCH 25/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit addition of missing space Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/distances.R b/R/distances.R index 5f7fd9f..61c894f 100644 --- a/R/distances.R +++ b/R/distances.R @@ -626,7 +626,7 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, target_dir <- "" - while(!dir.exists(target_dir)) { + while (!dir.exists(target_dir)) { target_dir <- readline(paste0("Specify the folder to save", " 'distances.csv' to.", " Hit enter to skip.")) From 750815a7964bd8baa5351408a264c2a275a224d4 Mon Sep 17 00:00:00 2001 From: Devon Smith <79339504+dsmith-unbc@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:24:55 -0800 Subject: [PATCH 26/29] Update R/distances.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit format changes Co-authored-by: Hugo Flávio <40389021+hugomflavio@users.noreply.github.com> --- R/distances.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/distances.R b/R/distances.R index 61c894f..4f0e17c 100644 --- a/R/distances.R +++ b/R/distances.R @@ -607,7 +607,7 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, if (row.rename) { rownames(dist.mat) <- outputRows - } + } if (col.rename) { colnames(dist.mat) <- outputCols From 6882a8b368ea0a3ef5ba82cf5a7cffaec95a3e2a Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Thu, 5 Dec 2024 17:00:12 -0800 Subject: [PATCH 27/29] Removed actel = TRUE from warnings, changes warnings to messages, and added message about starters being used for targets when actel = FALSE --- R/distances.R | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/R/distances.R b/R/distances.R index 4f0e17c..fb501d0 100644 --- a/R/distances.R +++ b/R/distances.R @@ -452,7 +452,7 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, if (actel) { message("M: Creating actel-compatible distances matrix."); flush.console() - if(!is.null(starters) | !is.null(targets)) { + if (!is.null(starters) | !is.null(targets)) { # distancesMatrix() crashes if actel = TRUE and starters/targets were # supplied but do not contain a column called "Standard.name" if(!is.null(starters) && !("Standard.name" %in% colnames(starters))) { @@ -460,7 +460,7 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, call. = FALSE) } - if(!is.null(targets) & !any(grepl(c("Standard.name"), + if (!is.null(targets) & !any(grepl(c("Standard.name"), colnames(targets)))) { stop("'targets' is missing column called 'Standard.name'", call. = FALSE) @@ -468,22 +468,22 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, } if (!is.null(starters) & !is.null(targets)) { - warning("'starters' and 'targets' were specified while 'actel'", - " is set to TRUE. Using 'starters' and 'targets' for distances.", + message("M: 'starters' and 'targets' were specified.", + " Using 'starters' and 'targets' for distances.", call. = FALSE, immediate. = TRUE) } if (!is.null(starters) & is.null(targets)) { - warning("'starters' were specified but 'targets' were not while 'actel'", - " is set to TRUE. Using 'starters' for 'targets'.", + message("M: 'starters' were specified but 'targets' were not.", + " Using 'starters' for 'targets'.", call. = FALSE, immediate. = TRUE) targets <- starters } if (is.null(starters) & is.null(targets)) { - warning("'starters' and 'targets' were not set while 'actel' is set to", - " TRUE. Creating 'starters' and 'targets' from 'spatial.csv' ", + message("M: 'starters' and 'targets' were not specified.", + " Creating 'starters' and 'targets' from 'spatial.csv' ", " in working directory." , call. = FALSE, immediate. = TRUE) starters <- targets <- loadSpatial() } @@ -492,15 +492,19 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, warning("id.col' was set but will be ignored because 'actel' is set to", " TRUE. Set 'actel' to FALSE to use the 'id.col' argument.", call. = FALSE, immediate. = TRUE) - } + } id.col <- "Standard.name" } # Failsafe from old way code worked where targets used starters by default # when actel == FALSE - if(!actel) { - if(is.null(targets)) { + if (!actel) { + if (is.null(targets)) { targets <- starters + message("M: 'starters' were specified but 'targets' were not.", + " Using 'starters' for 'targets'.", + call. = FALSE, immediate. = TRUE) + } } } From 4ab77490008c9b0fdac300b9f92a1c190a26f7ef Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Thu, 5 Dec 2024 17:02:12 -0800 Subject: [PATCH 28/29] minor fix to if statement --- R/distances.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/distances.R b/R/distances.R index fb501d0..237ab41 100644 --- a/R/distances.R +++ b/R/distances.R @@ -455,13 +455,12 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, if (!is.null(starters) | !is.null(targets)) { # distancesMatrix() crashes if actel = TRUE and starters/targets were # supplied but do not contain a column called "Standard.name" - if(!is.null(starters) && !("Standard.name" %in% colnames(starters))) { + if (!is.null(starters) && !("Standard.name" %in% colnames(starters))) { stop("'starters' is missing column called 'Standard.name'", call. = FALSE) } - if (!is.null(targets) & !any(grepl(c("Standard.name"), - colnames(targets)))) { + if (!is.null(targets) && !("Standard.name" %in% colnames(targets))) { stop("'targets' is missing column called 'Standard.name'", call. = FALSE) } From 4f022f9fada4a7e4b79bc364c414ff86428fc4b5 Mon Sep 17 00:00:00 2001 From: Devon Smith Date: Fri, 6 Dec 2024 11:05:10 -0800 Subject: [PATCH 29/29] fixed bracket issue causing checks to fail. --- R/distances.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/distances.R b/R/distances.R index 237ab41..446016c 100644 --- a/R/distances.R +++ b/R/distances.R @@ -505,7 +505,6 @@ distancesMatrix <- function(t.layer, starters = NULL, targets = NULL, call. = FALSE, immediate. = TRUE) } } - } if (!inherits(starters, "data.frame")) { stop("'starters' must be a data frame.\n", call. = FALSE)