-
Notifications
You must be signed in to change notification settings - Fork 28
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
[ and [[ keep attributes #275
Conversation
Codecov Report
@@ Coverage Diff @@
## master #275 +/- ##
=======================================
Coverage 94.40% 94.41%
=======================================
Files 18 18
Lines 876 877 +1
=======================================
+ Hits 827 828 +1
Misses 49 49
Continue to review full report at Codecov.
|
Thanks. What is the interest in preserving other attributes? This PR breaks About importing |
I agree that minimizing the number of hard dependencies is critical for this package. Also, vctrs seems to be pretty dynamic so this would keep everyone quite busy. Once vctrs has reached stability and is rock-solid, and importing it would bring strong advantages (simplifying code could be one!) we could reconsider this. |
Fair enough. The motivation here is to support custom formatting for numbers with units, as a follow-up to #273. See https://pillar.r-lib.org/dev/articles/numbers.html#units for a specific example. I wasn't aware that the |
I see. And wouldn't make more sense to prepend a new class---say, |
We could also treat units and quantities as main classes if they support wrapping of classed objects. This means replacing (at least in units) most of not all instances of strip_class <- function(x) {
if (identical(x, "units")) {
unclass(x)
} else {
class(x) <- setdiff(class(x), "units")
x
}
} Would you support a change like this? The alternative -- prepending a class -- doesn't look useful at the first glance. In a branch in pillar I have: library(pillar)
library(units)
#> udunits database from /usr/share/xml/udunits/udunits2.xml
set_units(1:3, km) + set_units(1:3, m)
#> Units: [km]
#> [1] 1.001 2.002 3.003
num(1:3, notation = "sci") + 1:3
#> <tibble_num(sci)[3]>
#> [1] 2.00e0 4.00e0 6.00e0
# Doesn't work, looks difficult to fix
num(set_units(1:3, km), notation = "sci") + set_units(1:3, m)
#> Warning: Incompatible methods ("+.vctrs_vctr", "Ops.units") for "+"
#> <tibble_num(sci)[3]>
#> [1] 2.00e0 4.00e0 6.00e0
# Maybe...
set_units.tibble_num <- function(x, ...) {
unclassed <- x
class(unclassed) <- NULL
out <- set_units(unclassed, ...)
class(out) <- c(class(out), class(x))
out
}
set_units(num(1:3, notation = "sci"), km)
#> Units: [km]
#> <tibble_num(sci)[3]>
#> [1] 1.00e0 2.00e0 3.00e0
# Ops.units uses unclass() but shouldn't?
set_units(num(1:3), km) + set_units(1:3, m)
#> Units: [km]
#> [1] 1.001 2.002 3.003 Created on 2021-03-24 by the reprex package (v1.0.0) It would be much easier if units preserved the "pillar" attribute on subsetting, which contains only information related to display. Not perfect object-oriented programming, just much easier and achieves the desired result. I'll update the PR. |
Co-authored-by: Iñaki Ucar <iucar@fedoraproject.org>
Where is this num(1:3, label = "€") + num(1:3, label = "$") which makes no sense to me. |
Thanks. It's in the docs-num branch in pillar. I'd like to argue that applying formatting early in the analysis solves a few problems:
When combining two |
Ok, being aware of the
If @edzer has no further concerns/comments, I can merge this. |
|
Merging, thanks! For errors, some guidance on how a compound pillar looks like would be helpful. |
See |
Thanks for taking this! The |
Unless we want something fancier, just dropping the attribute before calling |
This would be easier and faster if vctrs was an imported package, because we could then use
vec_restore()
.Do you see strong reasons against importing vctrs? We don't necessarily need to change the class hierarchy, although by inheriting from
"vctrs_vctr"
a lot of code here would be obsolete or could be changed.