Skip to content

Commit

Permalink
Merge pull request #28 from VEuPathDB/fix-27-asnumeric-bins
Browse files Browse the repository at this point in the history
add as.numeric for bins
  • Loading branch information
asizemore authored Sep 6, 2023
2 parents 36bbbc8 + d7fcea3 commit b2249b6
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ exportClasses(VariableMetadataList)
exportClasses(VariableSpec)
exportClasses(VariableSpecList)
exportMethods(as.data.table)
exportMethods(as.numeric)
exportMethods(asJSON)
exportMethods(findAllColNames)
exportMethods(findColNamesByPredicate)
Expand Down
33 changes: 33 additions & 0 deletions R/methods-Bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,37 @@ setMethod("whichValuesInBinList", signature("BinList"), function(values, binList
inBin <- Reduce(`+`, lapply(binList, whichValuesInBin, values = values))
return (as.logical(inBin))

})


#' Convert a Bin with character start and end values to numeric
#'
#' Given a Bin object with binStart and binEnd of class character,
#' convert both to numeric and return the Bin.
#'
#' @param x Bin
#' @return Bin
#' @export
setMethod("as.numeric", signature("Bin"), function(x) {
binNumeric <- Bin(
binStart = as.numeric(x@binStart),
binEnd = as.numeric(x@binEnd),
binLabel = x@binLabel,
value = x@value
)
return(binNumeric)
})


#' Convert bins in a BinList with character start and end values to numeric
#'
#' Given a BinList object containing Bins that may have a character binStart or binEnd,
#' convert all binStarts and binEnds to numeric and return the BinList.
#'
#' @param x BinList
#' @return BinList
#' @export
setMethod("as.numeric", signature("BinList"), function(x) {
binListNumeric <- BinList(lapply(x, as.numeric))
return(binListNumeric)
})
18 changes: 18 additions & 0 deletions man/as.numeric-Bin-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/as.numeric-BinList-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions tests/testthat/test-methods-Bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,38 @@ test_that("whichValuesInBinList works", {
expect_equal(inBin, c(FALSE, TRUE, FALSE, TRUE, FALSE, FALSE))

})

test_that("converting string bins to numeric bins is successful", {


# Test for regular bins
stringBin1 <- Bin(binStart='1', binEnd='2', binLabel='1-2', value = 2)

numericBin1 <- as.numeric(stringBin1)
expect_equal(class(numericBin1@binStart), 'numeric')
expect_equal(class(numericBin1@binEnd), 'numeric')
expect_equal(numericBin1@binLabel, stringBin1@binLabel)
expect_equal(numericBin1@value, stringBin1@value)



stringBin2 <- Bin(binStart='2.00001', binEnd='2e10', binLabel='2-2e10')

numericBin2 <- as.numeric(stringBin2)
expect_equal(class(numericBin2@binStart), 'numeric')
expect_equal(class(numericBin2@binEnd), 'numeric')
expect_equal(numericBin2@binLabel, stringBin2@binLabel)
expect_equal(numericBin2@value, stringBin2@value)


# Test for BinLists
stringBin3 <- Bin(binStart='2e11', binEnd='2e12', binLabel='2e11-2e12')
stringBinList1 <- BinList(S4Vectors::SimpleList(c(stringBin2, stringBin3)))

numericBinList1 <- as.numeric(stringBinList1)
expect_equal(unlist(lapply(numericBinList1, function(x) {class(x@binStart)})), c('numeric','numeric'))
expect_equal(unlist(lapply(numericBinList1, function(x) {class(x@binEnd)})), c('numeric','numeric'))


})

0 comments on commit b2249b6

Please sign in to comment.