Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default pillar_shaft() method for numbers #273

Merged
merged 6 commits into from
Mar 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions R/conversion.R
Original file line number Diff line number Diff line change
@@ -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}.
5 changes: 2 additions & 3 deletions R/tidyverse.R
Original file line number Diff line number Diff line change
@@ -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, ...) {
1 change: 1 addition & 0 deletions man/units.Rd

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

20 changes: 10 additions & 10 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
@@ -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},
15 changes: 15 additions & 0 deletions tests/testthat/_snaps/tidyverse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# pillar methods are available for units objects

Code
pillar::pillar(x[1])
Output
<pillar>
[km]
1
Code
pillar::pillar(m[1])
Output
<pillar>
<mixed_units>
1 [km]

7 changes: 3 additions & 4 deletions tests/testthat/test_conversion.R
Original file line number Diff line number Diff line change
@@ -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"))

})

13 changes: 7 additions & 6 deletions tests/testthat/test_tidyverse.R
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion tests/testthat/test_user_conversion.R
Original file line number Diff line number Diff line change
@@ -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"))