Skip to content

Commit

Permalink
Merge pull request #89 from isoverse/dev
Browse files Browse the repository at this point in the history
fix problem with double to iso_double_with_units cast for ggplot
  • Loading branch information
sebkopf authored Nov 14, 2019
2 parents c3138a4 + cbef7db commit 6a00f37
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: isoreader
Title: Read IRMS data files
Description: R interface to IRMS (isotope ratio mass spectrometry) file formats typically used in stable isotope geochemistry.
Version: 1.0.11
Version: 1.0.12
Authors@R: person("Sebastian", "Kopf", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-2044-0201"))
URL: https://github.com/isoverse/isoreader
BugReports: https://github.com/isoverse/isoreader/issues
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ S3method(vec_cast.double,iso_double_with_units)
S3method(vec_cast.factor,iso_double_with_units)
S3method(vec_cast.integer,iso_double_with_units)
S3method(vec_cast.iso_double_with_units,default)
S3method(vec_cast.iso_double_with_units,double)
S3method(vec_cast.iso_double_with_units,integer)
S3method(vec_cast.iso_double_with_units,iso_double_with_units)
S3method(vec_cast.logical,iso_double_with_units)
S3method(vec_ptype2,iso_double_with_units)
Expand Down
14 changes: 14 additions & 0 deletions R/units.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ vec_ptype2.integer.iso_double_with_units <- function(x, y, ...) double()
# no other vec_c combinations are formally allowed for now
# since double with units + character/logical/factor should really not happen


# cast from integer or double to double with units takes the units of the object being cast to
#' @method vec_cast.iso_double_with_units double
#' @export
vec_cast.iso_double_with_units.double <- function(x, to, ...) {
iso_double_with_units(x, units = iso_get_units(to))
}
#' @method vec_cast.iso_double_with_units integer
#' @export
vec_cast.iso_double_with_units.integer <- function(x, to, ...) {
iso_double_with_units(x, units = iso_get_units(to))
}


# cast from double with units to any other format makes it behave like a double
# this allows c() to work as if it was a double (but other vec_c combinations are not allowed)
#' @importFrom vctrs vec_cast.double
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-units.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ test_that("test that units class works properly", {
expect_is(vctrs::vec_ptype2(x, double()), "numeric")
expect_is(vctrs::vec_ptype2(double(), x), "numeric")
expect_is(vctrs::vec_cast(x, double()), "numeric")
expect_equal(vctrs::vec_cast(pi, x), iso_double_with_units(pi, "permil"))
expect_equal(vctrs::vec_c(x, 4.2), c(as.numeric(x), 4.2))
expect_equal(vctrs::vec_c(4.2, x), c(4.2, as.numeric(x)))
# integer
expect_is(vctrs::vec_ptype2(x, integer()), "numeric")
expect_is(vctrs::vec_ptype2(integer(), x), "numeric")
expect_is(vctrs::vec_cast(x, integer()), "integer")
expect_equal(vctrs::vec_cast(42L, x), iso_double_with_units(42, "permil"))
expect_equal(vctrs::vec_c(x, 42L), c(as.numeric(x), 42.))
expect_equal(vctrs::vec_c(42L, x), c(42., as.numeric(x)))
# others
Expand Down

0 comments on commit 6a00f37

Please sign in to comment.