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

feat: Math operations on num() objects now pass additional arguments to the mathematical function #660

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

# pillar 1.9.0.9008

- Internal changes only.
## Bug fixes

- Math operations on `num()` objects now pass additional arguments to the mathematical function (@gvelasq, #660).

# pillar 1.9.0.9007

Expand Down
2 changes: 1 addition & 1 deletion R/num.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ vec_arith.numeric.pillar_num <- vec_arith.pillar_num.default
vec_math.pillar_num <- function(.fn, .x, ...) {
"!!!!DEBUG vec_math(`v(.fn)`)"

out <- vec_math_base(.fn, vec_proxy(.x))
out <- vec_math_base(.fn, vec_proxy(.x), ...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do other methods also need to forward the dots?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing. As far as I can tell, I don't believe other methods need to forward the dots. vec_math_base() only takes function names as a character vector.

library(sloop)
library(vctrs)
library(pillar)

vec_math_base("sum", c(1:3, NA), na.rm = TRUE)
#> [1] 6

s3_dispatch(vec_math_base("sum", c(1:3, NA), na.rm = TRUE))
#>    vec_math_base.character
#>    vec_math_base.default

s3_methods_class("pillar_num")
#> # A tibble: 9 × 4
#>   generic            class      visible source             
#>   <chr>              <chr>      <lgl>   <chr>              
#> 1 format             pillar_num FALSE   registered S3method
#> 2 vec_arith.numeric  pillar_num FALSE   registered S3method
#> 3 vec_arith          pillar_num FALSE   registered S3method
#> 4 vec_cast.double    pillar_num FALSE   registered S3method
#> 5 vec_math           pillar_num FALSE   registered S3method
#> 6 vec_ptype_abbr     pillar_num FALSE   registered S3method
#> 7 vec_ptype_full     pillar_num FALSE   registered S3method
#> 8 vec_ptype2.double  pillar_num FALSE   registered S3method
#> 9 vec_ptype2.integer pillar_num FALSE   registered S3method

Created on 2024-06-20 with reprex v2.1.0


if (is.numeric(out)) {
out <- vec_restore(out, .x)
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/_snaps/num.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@
Output
<pillar_num{%}*100[3]>
[1] 84.1 90.9 14.1
Code
sum(num(c(1:3, NA)), na.rm = TRUE)
Output
<pillar_num[1]>
[1] 6

# formatting

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-num.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ test_that("mathematics", {
min(num(1:3, label = "$"))
mean(num(1:3, notation = "eng"))
sin(num(1:3, label = "%", scale = 100))
sum(num(c(1:3, NA)), na.rm = TRUE)
})
})

Expand Down
Loading