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

support for "AsIs" #906

Closed
romainfrancois opened this issue Mar 11, 2020 · 2 comments · Fixed by #910
Closed

support for "AsIs" #906

romainfrancois opened this issue Mar 11, 2020 · 2 comments · Fixed by #910
Assignees

Comments

@romainfrancois
Copy link
Contributor

This breaks packages biotmle, FindMyFriends, incadata, radiant.multivariate, sitar on dplyr 1.0.0 rev deps.

library(vctrs)
 
x <- I("")
vec_c(x)
#> Error: Can't find vctrs or base methods for concatenation.
#> vctrs methods must be implemented for class `AsIs`.
#> See <https://vctrs.r-lib.org/articles/s3-vector.html>.

chops <- vec_chop(x, list(1L))
vec_unchop(chops, list(1L))
#> Error: Can't find vctrs or base methods for concatenation.
#> vctrs methods must be implemented for class `AsIs`.
#> See <https://vctrs.r-lib.org/articles/s3-vector.html>.

Created on 2020-03-11 by the reprex package (v0.3.0)

@DavisVaughan
Copy link
Member

DavisVaughan commented Mar 11, 2020

If we don't want to propagate AsIs, one idea is to add ptype2 and cast methods that strip it and then re-call vec_ptype2() and vec_cast() with the underlying data. We'd need to add vec_ptype2.<type>.AsIs() methods to everything though, but maybe there is a better idea for that.

library(vctrs)

x <- I("asis")
vec_c(x)
#> Error: Can't find vctrs or base methods for concatenation.
#> vctrs methods must be implemented for class `AsIs`.
#> See <https://vctrs.r-lib.org/articles/s3-vector.html>.

strip_as_is <- function(x) {
  class(x) <- setdiff(class(x), "AsIs")
  x
}

vec_ptype2.AsIs <- function(x, y, ..., x_arg = "x", y_arg = "y") {
  vec_ptype2(strip_as_is(x), y)
}

vec_ptype2.character.AsIs <- function(x, y, ..., x_arg = "x", y_arg = "y") {
  vec_ptype2(x, strip_as_is(y))
}

vec_ptype2(x, x)
#> character(0)
vec_ptype2(x, "x")
#> character(0)
vec_ptype2("x", x)
#> character(0)


vec_cast.AsIs <- function(x, to, ...) {
  vec_cast(x, strip_as_is(to))
}

vec_cast.character.AsIs <- function(x, to, ...) {
  vec_cast(strip_as_is(x), to)
}

vec_cast(x, x)
#> [1] "asis"
class(vec_cast(x, x))
#> [1] "character"

vec_cast(x, "x")
#> [1] "asis"
vec_cast("x", x)
#> [1] "x"


vec_c(x,x)
#> [1] "asis" "asis"
class(vec_c(x,x))
#> [1] "character"

vec_c(x, "x")
#> [1] "asis" "x"
vec_c("x", x)
#> [1] "x"    "asis"

Created on 2020-03-11 by the reprex package (v0.3.0)

@hadley
Copy link
Member

hadley commented Mar 11, 2020

Hmmm, this is going to require a little thought because AsIs behaves like a subclass of any existing class, which because we no longer rely on inheritance in vctrs is going to require an arbitrary and unknown number of methods.

Should we add something in vec_default_ptype2() or vec_ptype()?

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

Successfully merging a pull request may close this issue.

3 participants