From 2af03f759ab2dde750fd608efc2e0cecac79759e Mon Sep 17 00:00:00 2001 From: Laura Marshall Date: Tue, 5 Dec 2023 14:22:25 +0000 Subject: [PATCH] Add check so that only distbegin and distend OR distance are supplied Also amended tests to comply with this. Reference #144 --- R/checkdata.R | 8 +++++++- tests/testthat/test_ds.R | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/R/checkdata.R b/R/checkdata.R index 173057f..e123225 100644 --- a/R/checkdata.R +++ b/R/checkdata.R @@ -25,10 +25,16 @@ checkdata <- function(data, region.table=NULL, sample.table=NULL, } } + + # Make sure that the user has only specified either distance or distend / distbegin (need to do this check first as then Distance creates the distance column) + if(!is.null(data$distance) && !is.null(data$distbegin) && !is.null(data$distend)){ + stop("You can only specify either a 'distance' column or 'distbegin' and 'distend' columns in your data.", call. = FALSE) + } + # make sure that the data are in the right format first if(is.null(data$distance)){ if(is.null(data$distend) & is.null(data$distbegin)){ - stop("Your data must (at least) have a column called 'distance' or 'distbegin' and 'distend'!") + stop("Your data must (at least) have a column called 'distance' or 'distbegin' and 'distend'!", call. = FALSE) }else{ data$distance <- (data$distend + data$distbegin)/2 } diff --git a/tests/testthat/test_ds.R b/tests/testthat/test_ds.R index 00f45e3..395de3c 100644 --- a/tests/testthat/test_ds.R +++ b/tests/testthat/test_ds.R @@ -176,6 +176,8 @@ test_that("Truncation is handled",{ # largest bin bin.data <- create_bins(egdata,c(0,1,2,3,3.8)) + # Remove distance column + bin.data <- subset(bin.data, select = -c(distance)) expect_equal(suppressMessages(ds(bin.data, key="hn", order=0))$ddf$meta.data$width, 3.8) @@ -226,8 +228,7 @@ test_that("cutpoints work with flatfile", { convert_units = units, cutpoints = c(seq(0,8,1)), formula = ~Region.Label) - pigs2 <- data.frame(distance = (distbegin+distend)/2, - distbegin = distbegin, + pigs2 <- data.frame(distbegin = distbegin, distend = distend, Region.Label = pigs$Region.Label, Area = 0)