Skip to content

Feature request: flexibility of date and time transformations w.r.t. numeric input. #298

Closed

Description

@teunbrand

Originally discussed in tidyverse/ggplot2#980 (comment); it seems as there is some need for flexibility of date and time transformations. These transformations are currently harder to program with than plain numerical transformations, as the date/time transformations have strict requirements on the class of the input data.

@yutannihilation made two excellent suggestions which I'll repeat here.

  1. Let the domain part of the transformation be of the same class as the expected input data. This in particular would allow a solution to a longstanding issue in ggplot2.
  2. Let date_trans()$transform() and time_trans()$transform() accept numeric values. This would solve the aforementioned issue, as well as some others.

I understand that the class requirements protect against unintentional mistakes so I'd like to mention a 3rd idea.

  1. Add ... arguments to all trans$transform() arguments and in addition a force argument to the date/time transforms. The logic then could be as follows:
function(x, force = FALSE, ...)  
{
  if (!inherits(x, "Date") & !force) {
    stop("Invalid input: date_trans works with objects of class Date only", 
         call. = FALSE)
  }
  structure(as.numeric(x), names = names(x))
}

This would allow only the transformation of intentional numeric values while still raising the error upon unintentional input.

Thanks for reading!

Activity

  1. teunbrand commented on Apr 20, 2021

    @teunbrand
    CollaboratorAuthor
  2. added
    feature
    help wanted ❤️
    on Mar 17, 2022
  3. hadley commented on Mar 17, 2022

    @hadley
    Member

    I think I'd need to see a concrete implementation to understand what you're suggesting here. My main worry is (e.g.) accidentally passing a date to date-time transformer, so you get number of days instead of number of seconds. I don't think calling as.Date() or as.POSIXct() on the input is sufficient since they have rather surprising behaviours (e.g. as.Date(ISOdatetime(2021, 1, 1, 20, 59, 00, tz = "America/Chicago")))

  4. added this to the v1.2.0 milestone on Mar 20, 2022
  5. hadley commented on Mar 21, 2022

    @hadley
    Member

    After reading the original issue, I think the easiest fix for the original problem is to give the domain the correct type.

  6. added a commit that references this issue on Mar 21, 2022
    1102c6a
  7. added a commit that references this issue on Mar 24, 2022
    de10911
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurehelp wanted ❤️

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions