Skip to content

Commit

Permalink
horizons<-: silent re-application of original order and NULL protection
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Aug 2, 2020
1 parent 7be0b0b commit 46b9d02
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
13 changes: 8 additions & 5 deletions R/SoilProfileCollection-setters.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ setReplaceMethod("depths", "data.frame",

iddata <- data[[nm[1]]]
tdep <- data[[depthcols[1]]]

usortid <- unique(sort(iddata))

idtdepord <- order(as.character(iddata), tdep)
ditd <- data[idtdepord,]
hsorttdep <- all(ditd[[depthcols[1]]] == tdep)

# re-sort horizon data
if (suppressWarnings(any(iddata != usortid) | !hsorttdep)) {
## note: forced character sort on ID -- need to impose some order to check depths
Expand Down Expand Up @@ -460,6 +460,9 @@ if (!isGeneric('horizons<-'))
setReplaceMethod("horizons", signature(object = "SoilProfileCollection"),
function(object, value) {

if (is.null(value))
stop("new horizon data must not be NULL; to remove a site or horizon attribute use `spc$attribute <- NULL`", call.=FALSE)

# testing the class of the horizon data to add to the object
if (!inherits(value, "data.frame"))
stop("new horizon data input value must inherit from data.frame", call.=FALSE)
Expand Down Expand Up @@ -504,9 +507,9 @@ setReplaceMethod("horizons", signature(object = "SoilProfileCollection"),
new.horizon.order <- match(names(original.horizon.order),
horizon.new[[hzidname(object)]])
chnew <- .coalesce.idx(horizon.new[[idname(object)]])
if(length(chnew) != length(original.site.order) |
if (length(chnew) != length(original.site.order) |
suppressWarnings(any(original.site.order != chnew))) {
message("join condition resulted in sorting of horizons, re-applying original order")
# message("join condition resulted in sorting of horizons, re-applying original order")
horizon.new <- horizon.new[new.horizon.order,]
}

Expand Down
2 changes: 1 addition & 1 deletion R/guessColumnNames.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ guessHzTexClName <- function(x) {
possible.name1 <- nm[grep('texcl', nm, ignore.case = TRUE)]

# use the first valid guess matching texcl
if (length(possible.name1) == 1) {
if (length(possible.name1) > 0) {
possible.name1 <- possible.name1[1]
return(possible.name1)
}
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-DT-tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,9 @@ res <- lapply(dfclasses, function(use_class) {
switch(
use_class,
# data.frame and tbl_df
expect_message({
expect_silent({
horizons(test) <- value
} ,
"join condition resulted in sorting of horizons, re-applying original order"),
}),

# note: data.table sorts correctly without invoking re-order
"data.table" = {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-SPC-objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,15 @@ test_that("SPC depth columns get/set ", {
})

test_that("SPC min/max overrides work as expected", {

set.seed(20202)
df <- lapply(1:10, random_profile, SPC=TRUE)
df <- union(df)

## visually inspect output
# profileApply(df, min)
# profileApply(df, max)

# both min and max should return 10cm
expect_equal(min(df), 44)
expect_equal(max(df), 134)
Expand Down Expand Up @@ -536,7 +536,7 @@ test_that("horizons<- left-join", {
hnew$prop[1] <- 50

# utilize horizons<- left join
expect_message(horizons(x) <- hnew, "join condition resulted in sorting of horizons, re-applying original order")
expect_silent(horizons(x) <- hnew)

# verify old columns have same names
# (i.e. no issues with duplication of column names in merge)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-guessColumnNames.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ test_that("basic functionality", {
expect_equal(guessHzTexClName(sp3), "texcl")

# texture
horizons(sp3)$texture <- horizons(sp3)$texcl
horizons(sp3)$texcl <- NULL
sp3$texture <- sp3$texcl
sp3$texcl <- NULL
expect_equal(guessHzTexClName(sp3), "texture")

# descriptive name
Expand Down

0 comments on commit 46b9d02

Please sign in to comment.