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 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
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
84 changes: 40 additions & 44 deletions tests/testthat/_snaps/ggplot2/log-scale.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
2 changes: 1 addition & 1 deletion tests/testthat/test-ggplot2.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test_that("ggplot2 snapshot tests", {
log_scale <-
ggplot2::ggplot(data, ggplot2::aes(x, z)) +
ggplot2::geom_point() +
scale_y_num(trans = "log10") +
scale_y_num(transform = "log10") +
ggplot2::theme_minimal(36)

vdiffr::expect_doppelganger("log_scale", log_scale)
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