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

vectors with same attributes incompatible #1730

Closed
monkeywithacupcake opened this issue Oct 19, 2022 · 4 comments
Closed

vectors with same attributes incompatible #1730

monkeywithacupcake opened this issue Oct 19, 2022 · 4 comments

Comments

@monkeywithacupcake
Copy link

monkeywithacupcake commented Oct 19, 2022

I don't understand why I get an error from vctrs::vec_ptype2() when comparing the type of two vectors with the same formatting. When they are created in the same way, how can they not be compatible.

library(formattable)
library(vctrs)
curr1 = formattable::currency(1:5)
curr2 = formattable::currency(6:10)

vctrs::vec_ptype2(curr1,curr2)

# Error:
# ! Can't combine `curr1` <formattable> and `curr2` <formattable>.
# ✖ Some attributes are incompatible.
# ℹ The author of the class should implement vctrs methods.
# ℹ See <https://vctrs.r-lib.org/reference/faq-error-incompatible-attributes.html>.
# Run `rlang::last_error()` to see where the error occurred.
# --- 
# Backtrace:
#
#    1. └─vctrs::vec_ptype2(curr1, curr2)
#  2.   └─vctrs (local) `<fn>`()
#  3.     └─vctrs::vec_default_ptype2(...)
#  4.       └─vctrs::stop_incompatible_type(...)
#  5.         └─vctrs:::stop_incompatible(...)
#  6.           └─vctrs:::stop_vctrs(...)
#  7.             └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = vctrs_error_call(call))

Inspired from formattable issue

@lionel-
Copy link
Member

lionel- commented Oct 19, 2022

We have a same-type fallback that kicks in when all attributes are the same, but in this case I see that this attribute contains a function with a varying environment:

$formattable$postproc
function (str, x) 
sprintf("%s%s%s", ifelse(is.na(x), "", symbol), sep, str)
<bytecode: 0x11ca8b0b0>
<environment: 0x1299122d8>

You can fix this by implementing identity self-to-self ptype2 and cast methods. Implementing these methods is a good idea in general because it's also be a bit faster than the fallback paths.

See https://vctrs.r-lib.org/reference/howto-faq-coercion.html for more information.

@monkeywithacupcake
Copy link
Author

monkeywithacupcake commented Oct 20, 2022

I appreciate this comment. I am trying to follow those FAQ to modify the formattable package, but I am not understanding.

I modified my local copy of the formattable package to have

#' @importFrom vctrs vec_ptype2 vec_cast
NULL
#' @export
vec_ptype2.num_currency.num_currency <- function(x, y, ...) {
  x
}

#' @export
vec_cast.num_currency.num_currency  <- function(x, to, ...) {
  x
}

But, after I load_all() and attempt again, I still get the same error.

> curr1 <- num_currency(1:5)
> curr2 <- num_currency(6:10)
> 
> vec_ptype2(curr1,curr2)
Error:
! Can't combine `curr1` <formattable> and `curr2` <formattable>.
✖ Some attributes are incompatible.
ℹ The author of the class should implement vctrs methods.
ℹ See <https://vctrs.r-lib.org/reference/faq-error-incompatible-attributes.html>.
Run `rlang::last_error()` to see where the error occurred.

Likely a continuation of my lack of understanding, but I also tried the following (it also did not work):

#' @importFrom vctrs vec_ptype2 vec_cast
NULL
#' @export
vec_ptype2.formattable.formattable <- function(x, y, ...) {
  x
}

#' @export
vec_cast.formattable.formattable <- function(x, to, ...) {
  x
}

interestingly enough, I can build vectors

vctrs::vec_c(currency(1:3),currency(2:4))
# [1] $1.00 $2.00 $3.00 $2.00 $3.00 $4.00

@lionel-
Copy link
Member

lionel- commented Oct 20, 2022

Did you redocument before loading-all?

@monkeywithacupcake
Copy link
Author

Thank you for helping me through this. It works now. I can run over the conceptual implications with folks on the formattable package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants