Skip to content

Commit

Permalink
add protection for horizons<- order with no new data on RHS #163
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Aug 17, 2020
1 parent 836a9f8 commit d83c266
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion R/SoilProfileCollection-setters.R
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,11 @@ setReplaceMethod("horizons", signature(object = "SoilProfileCollection"),
if (all(colnames(value) %in% horizonNames(object)) &
all(c(idname(object), hzidname(object), horizonDepths(object)) %in% colnames(value)) &
nrow(value) == nrow(object)) {
object@horizons <- .as.data.frame.aqp(value, aqp_df_class(object))
target.order <- order(object@horizons[[idname(object)]], object@horizons[[horizonDepths(object)[1]]])
input.order <- order(value[[idname(object)]], value[[horizonDepths(object)[1]]])
idx.order <- match(input.order, target.order)
# print(idx.order)
object@horizons <- .as.data.frame.aqp(value, aqp_df_class(object))[idx.order,]
return(object)
}

Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-DT-tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ res <- lapply(dfclasses, function(use_class) {
expect_equal(horizons(test)[[idname(test)]], as.character(cc(lapply(1:4, rep, 10))))

expect_equal(horizons(test)[[horizonDepths(test)[2]]], cc(rep(1:10, 4)))

# deliberately mess up order without adding anything
expect_silent(horizons(test) <- horizons(test)[c(1:10,20:30,11:19,31:40),])

# horizons fixes the order
expect_true(spc_in_sync(test)$valid)


## make sample data using current class type
Expand Down

0 comments on commit d83c266

Please sign in to comment.