GithubHelp home page GithubHelp logo

Comments (9)

hadley avatar hadley commented on July 18, 2024

Potentially AsIs could be used with anything, so I think we just need a .AsIs() method somewhere that just calls NextMethod()

from pillar.

krlmlr avatar krlmlr commented on July 18, 2024

Tried adding pillar_shaft.AsIs() to no avail -- it's handled by pillar_shaft.default() anyway which calls as.character() on the input:

as.character(I(list(1, 1:2, 1:3)))
#> [1] "1"   "1:2" "1:3"

Code:

pillar/R/shaft.R

Lines 134 to 138 in be24a37

#' @export
#' @rdname pillar_shaft
pillar_shaft.default <- function(x, ...) {
pillar_shaft(as.character(x), ...)
}

We'd need to strip the "AsIs" class from the object's classes, but somehow this smells like a bad idea. Please advise.

from pillar.

hadley avatar hadley commented on July 18, 2024

You're sure this doesn't work?

pillar_shaft.AsIs <- function(x, ...) {
  NextMethod()
}

Oh, because list isn't in the class vector?

from pillar.

krlmlr avatar krlmlr commented on July 18, 2024

We could do

#' @export
#' @rdname pillar_shaft
pillar_shaft.AsIs <- function(x, ...) {
  pillar_shaft.list(x, ...)
}

or (more generally)

#' @export
#' @rdname pillar_shaft
pillar_shaft.default <- function(x, ...) {
  if (is.list(x)) pillar_shaft.list(x, ...)
  else pillar_shaft(as.character(x), ...)
}

But then:

pillar::colonnade(list(a = 1:3, b = I(list(1, 1:2, 1:3, c(1:3,3)))))
#>       a b         
#>   <int> <S3: AsIs>
#> 1     1 <S3: AsIs>
#> 2     2 <S3: AsIs>
#> 3     3 <S3: AsIs>

Or we strip the class manually:

#' @export
#' @rdname pillar_shaft
pillar_shaft.AsIs <- function(x, ...) {
  if (all(class(x) == "AsIs")) {
    class(x) <- NULL
  } else {
    class(x) <- setdiff(class(x), "AsIs")
  }
  pillar_shaft(x, ...)
}

That one looks better, but I'm not sure if this is a safe approach:

pillar::colonnade(list(a = 1:3, b = I(list(1, 1:2, 1:3, c(1:3,3)))))
#>       a b         
#>   <int> <S3: AsIs>
#> 1     1 <dbl [1]> 
#> 2     2 <int [2]> 
#> 3     3 <int [3]> 
#> 1     1 <dbl [4]>

The NextMethod() approach doesn't change the output, because the call is forwarded to .default() and not to .list().

from pillar.

hadley avatar hadley commented on July 18, 2024

An even more general approach would be to do manual switchpatch in the default method:

#' @export
#' @rdname pillar_shaft
pillar_shaft.default <- function(x, ...) {
  switch(typeof(x),
    list = pillar_shaft.list(x, ...),
    numeric = pillar_shaft.numeric(x, ...),
    logical = pillar_shaft.logical(x, ...),
    pillar_shaft.character(x, ...),
  )
}

Then we're basically pretending that the final member of the class is always the typeof().

from pillar.

krlmlr avatar krlmlr commented on July 18, 2024

This works if we use switchpatch also in obj_sum.AsIs().

from pillar.

hadley avatar hadley commented on July 18, 2024

Oh yeah, that's a better idea - we either call NextMethod() or do switchpatch depending on the length of the class.

from pillar.

krlmlr avatar krlmlr commented on July 18, 2024

Let's keep switchpatch in mind, I haven't added it yet. Current output:

tibble::as_tibble(data.frame(a = 1:3, b = I(list(1, 1:2, 1:3))))
#> # A tibble: 3 x 2
#>       a b        
#>   <int> <I(list)>
#> 1     1 <dbl [1]>
#> 2     2 <int [2]>
#> 3     3 <int [3]>

from pillar.

github-actions avatar github-actions commented on July 18, 2024

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

from pillar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.