From 194e1449d8deba92103f063c57918446fd76752c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 16 Mar 2021 09:17:06 +0100 Subject: [PATCH 1/5] Use default pillar_shaft() method for numbers --- R/tidyverse.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/tidyverse.R b/R/tidyverse.R index 00096b2e..e0057e83 100644 --- a/R/tidyverse.R +++ b/R/tidyverse.R @@ -6,12 +6,11 @@ type_sum.units <- function(x, ...) { } type_sum.mixed_units <- function(x, ...) { - "mixed_units" + "mixed_units" } pillar_shaft.units <- function(x, ...) { - out <- format(unclass(x), ...) - pillar::new_pillar_shaft_simple(out, align = "right", min_width = 8) + pillar::pillar_shaft(unclass(x)) } pillar_shaft.mixed_units <- function(x, ...) { From fc8e5d79092671b2f8b78ace6517250e51d95c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 16 Mar 2021 10:25:49 +0100 Subject: [PATCH 2/5] Use testthat 3 --- DESCRIPTION | 3 ++- R/conversion.R | 1 + man/units.Rd | 1 + tests/testthat/test_conversion.R | 7 +++---- tests/testthat/test_user_conversion.R | 2 +- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 246e0ede..8e65a3ce 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,7 +23,7 @@ Suggests: dplyr (>= 1.0.0), vctrs (>= 0.3.1), knitr, - testthat, + testthat (>= 3.0.0), ggforce, rmarkdown VignetteBuilder: knitr @@ -43,3 +43,4 @@ BugReports: https://github.com/r-quantities/units/issues/ RoxygenNote: 7.1.1 Roxygen: list(old_usage = TRUE) Encoding: UTF-8 +Config/testthat/edition: 3 diff --git a/R/conversion.R b/R/conversion.R index 9496a457..e5c22370 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -14,6 +14,7 @@ #' default it operates with bare expressions, but this #' behavior can be disabled by a specifying \code{mode = "standard"} or setting #' \code{units_options(set_units_mode = "standard")}. +#' If \code{value} is missing or set to \code{1}, the object becomes unitless. #' } #' #' @param x numeric vector, or object of class \code{units}. diff --git a/man/units.Rd b/man/units.Rd index ded70310..643616d4 100644 --- a/man/units.Rd +++ b/man/units.Rd @@ -109,6 +109,7 @@ A number of functions are provided for handling unit objects. default it operates with bare expressions, but this behavior can be disabled by a specifying \code{mode = "standard"} or setting \code{units_options(set_units_mode = "standard")}. + If \code{value} is missing or set to \code{1}, the object becomes unitless. } } \details{ diff --git a/tests/testthat/test_conversion.R b/tests/testthat/test_conversion.R index b8198714..2f741ad1 100644 --- a/tests/testthat/test_conversion.R +++ b/tests/testthat/test_conversion.R @@ -150,11 +150,11 @@ test_that("conversion to dimensionless with prefix works (g/kg) if simplify=TRUE units(a) = as_units("mg/kg") expect_equal(as.numeric(a), a_orig/1e6) units(a) = as_units("kg/mg") - expect_equal(a, a_orig) + expect_equal(a, set_units(a_orig, 1)) units(a) = as_units("g/g") - expect_equal(a, a_orig) + expect_equal(a, set_units(a_orig, 1)) units(a) = as_units("kg/g") - expect_equal(a, a_orig * 1000) + expect_equal(a, set_units(a_orig * 1000, 1)) units_options(simplify = NA) }) @@ -218,4 +218,3 @@ test_that("ud_are_convertible return the expected value", { expect_false(ud_are_convertible("s", "kg")) }) - diff --git a/tests/testthat/test_user_conversion.R b/tests/testthat/test_user_conversion.R index ef52fe44..b5a7cac4 100644 --- a/tests/testthat/test_user_conversion.R +++ b/tests/testthat/test_user_conversion.R @@ -26,7 +26,7 @@ test_that("we can convert between units with a user-defined function", { # check dimensionless install_unit("person", "unitless") persons <- set_units(3, person) - expect_equal(persons, set_units(3, 1)) + expect_true(persons == set_units(3, 1)) # restore remove_unit(c("orange", "apple", "banana", "person")) From 5ffd722038208a8821eddaf8b143da0a9afc2cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 16 Mar 2021 10:26:19 +0100 Subject: [PATCH 3/5] Update generated files --- R/RcppExports.R | 8 ++++---- src/RcppExports.cpp | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/R/RcppExports.R b/R/RcppExports.R index 898c94d4..8e5e4b40 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -1,14 +1,14 @@ # Generated by using Rcpp::compileAttributes() -> do not edit by hand # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 -udunits_init <- function(path) { - invisible(.Call('_units_udunits_init', PACKAGE = 'units', path)) -} - udunits_exit <- function() { invisible(.Call('_units_udunits_exit', PACKAGE = 'units')) } +udunits_init <- function(path) { + invisible(.Call('_units_udunits_init', PACKAGE = 'units', path)) +} + R_ut_parse <- function(name) { .Call('_units_R_ut_parse', PACKAGE = 'units', name) } diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 6f413d67..c604fdb5 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -5,22 +5,22 @@ using namespace Rcpp; -// udunits_init -void udunits_init(CharacterVector path); -RcppExport SEXP _units_udunits_init(SEXP pathSEXP) { +// udunits_exit +void udunits_exit(); +RcppExport SEXP _units_udunits_exit() { BEGIN_RCPP Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< CharacterVector >::type path(pathSEXP); - udunits_init(path); + udunits_exit(); return R_NilValue; END_RCPP } -// udunits_exit -void udunits_exit(); -RcppExport SEXP _units_udunits_exit() { +// udunits_init +void udunits_init(CharacterVector path); +RcppExport SEXP _units_udunits_init(SEXP pathSEXP) { BEGIN_RCPP Rcpp::RNGScope rcpp_rngScope_gen; - udunits_exit(); + Rcpp::traits::input_parameter< CharacterVector >::type path(pathSEXP); + udunits_init(path); return R_NilValue; END_RCPP } @@ -275,8 +275,8 @@ END_RCPP } static const R_CallMethodDef CallEntries[] = { - {"_units_udunits_init", (DL_FUNC) &_units_udunits_init, 1}, {"_units_udunits_exit", (DL_FUNC) &_units_udunits_exit, 0}, + {"_units_udunits_init", (DL_FUNC) &_units_udunits_init, 1}, {"_units_R_ut_parse", (DL_FUNC) &_units_R_ut_parse, 1}, {"_units_R_ut_get_dimensionless_unit_one", (DL_FUNC) &_units_R_ut_get_dimensionless_unit_one, 0}, {"_units_R_ut_are_convertible", (DL_FUNC) &_units_R_ut_are_convertible, 2}, From aae0634b990269cfd2fd18304e3c16d86794596c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 16 Mar 2021 10:26:49 +0100 Subject: [PATCH 4/5] Pass dots --- R/tidyverse.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/tidyverse.R b/R/tidyverse.R index e0057e83..cde1cc27 100644 --- a/R/tidyverse.R +++ b/R/tidyverse.R @@ -10,7 +10,7 @@ type_sum.mixed_units <- function(x, ...) { } pillar_shaft.units <- function(x, ...) { - pillar::pillar_shaft(unclass(x)) + pillar::pillar_shaft(unclass(x), ...) } pillar_shaft.mixed_units <- function(x, ...) { From ec8ff07416e835cc767eee21576d93430247df9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 16 Mar 2021 10:28:09 +0100 Subject: [PATCH 5/5] Fix test --- tests/testthat/_snaps/tidyverse.md | 15 +++++++++++++++ tests/testthat/test_tidyverse.R | 13 +++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 tests/testthat/_snaps/tidyverse.md diff --git a/tests/testthat/_snaps/tidyverse.md b/tests/testthat/_snaps/tidyverse.md new file mode 100644 index 00000000..213ee5e3 --- /dev/null +++ b/tests/testthat/_snaps/tidyverse.md @@ -0,0 +1,15 @@ +# pillar methods are available for units objects + + Code + pillar::pillar(x[1]) + Output + + [km] + 1 + Code + pillar::pillar(m[1]) + Output + + + 1 [km] + diff --git a/tests/testthat/test_tidyverse.R b/tests/testthat/test_tidyverse.R index 715599ad..04f8afe9 100644 --- a/tests/testthat/test_tidyverse.R +++ b/tests/testthat/test_tidyverse.R @@ -2,17 +2,18 @@ context("tidyverse") test_that("pillar methods are available for units objects", { skip_if_not_installed("pillar") - + x = set_units(1:3, km) m = c(x, set_units(4:6, g), allow_mixed = TRUE) - + expect_equal(unclass(pillar::type_sum(x)), "[km]") expect_s3_class(pillar::type_sum(x), "type_sum_units") expect_equal(pillar::type_sum(m), "mixed_units") - - expect_equal(as.character(pillar::pillar_shaft(x[1])), "1") - expect_equal(as.character(pillar::pillar_shaft(m[1])), - paste0(1, pillar::style_subtle(" [km]"))) + + expect_snapshot({ + pillar::pillar(x[1]) + pillar::pillar(m[1]) + }) }) skip_if_not_installed("vctrs", "0.3.1")