Skip to content

Commit 283b6df

Browse files
committed
sppMatch: bug fix for return of a logical column
1 parent de483d0 commit 283b6df

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

R/CBM-DB_species.R

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,9 @@ sppMatch <- function(species, match = c("LandR", "Latin_full", "EN_generic_short
5555
"column(s) not found in sppEquiv: ",
5656
paste(shQuote(c(match, return)[!colExists]), collapse = ", "))
5757

58+
# Allow duplicate matches if there is a single unique set of return values
5859
if (!is.null(return)){
59-
60-
# Allow duplicate matches if there is a single unique set of return rows
61-
## Create secondary matches from duplicated rows
62-
sppEquivUQ <- sppEquiv[, lapply(.SD, function(x) list(list(x))), by = return]
63-
64-
sppEquiv <- sppEquivUQ[, .SD, .SDcols = c(match, return)]
65-
sppExtras <- sppEquivUQ[, .SD, .SDcols = setdiff(names(sppEquiv), c(match, return))]
66-
if (ncol(sppExtras) == 0) sppExtras <- NULL
67-
68-
}else{
69-
sppExtras <- NULL
60+
sppEquiv <- unique(sppEquiv[, .SD, .SDcols = unique(c(match, return))])
7061
}
7162

7263
# Set function for matching character columns
@@ -113,7 +104,6 @@ sppMatch <- function(species, match = c("LandR", "Latin_full", "EN_generic_short
113104
inputs = speciesUQ,
114105
choices = choices,
115106
choiceTable = sppEquiv,
116-
choiceTableExtra = sppExtras,
117107
identical = TRUE,
118108
funSimplify = .chSimple,
119109
nearMatches = nearMatches,

tests/testthat/test-CBM-DB_species.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ test_that("sppMatch to a chosen column", {
173173
))
174174
expect_equal(sppTable$CBM_speciesID, c(28, 122))
175175

176+
sppTable <- sppMatch(
177+
species = c(301, 2201),
178+
match = "CanfiCode",
179+
return = "Broadleaf",
180+
sppEquiv = rbind(
181+
sppEquiv,
182+
sppEquiv[sppEquiv$CanfiCode %in% 301,]
183+
))
184+
expect_equal(sppTable$Broadleaf, c(FALSE, TRUE))
185+
176186
# Expect error: NAs in input
177187
expect_error(
178188
sppMatch(

0 commit comments

Comments
 (0)