Skip to content

Commit

Permalink
Add check so that only distbegin and distend OR distance are supplied
Browse files Browse the repository at this point in the history
Also amended tests to comply with this.
Reference #144
  • Loading branch information
LHMarshall committed Dec 5, 2023
1 parent 9ab6efa commit 2af03f7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 7 additions & 1 deletion R/checkdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test_ds.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2af03f7

Please sign in to comment.