Skip to content

Better display for data.frames #78

Closed

Description

@bhogan-mitre

Related to #76, it would be nice to have a more natural view of where things differ in data.frames and tibbles.

> iris_mod <- iris
> iris_mod[3,2] <- 17
> compare(iris, iris_mod)
`old$Sepal.Width[1:6]`: 4 3  3 3 4 4
`new$Sepal.Width[1:6]`: 4 3 17 3 4 4

Activity

  1. hadley commented on May 3, 2021

    @hadley
    Member

    How could that display be better?

  2. bhogan-mitre commented on May 3, 2021

    @bhogan-mitre
    Author

    I was picturing something more row-based or even rectangular how a git diff might look.

    I suppose whether that would help depends on the shape of the data and the nature of the changes. I got thinking about it when I had something where multiple columns in the same row changed and it was tough to see that at a glance from successive prints of the column-based diff.

    For example

    > iris_mod <- iris
    > iris_mod[3,2] <- 17
    > iris_mod[3,5] <- "versicolor"
    > compare(iris, iris_mod)
    `old$Sepal.Width[1:6]`: 4 3  3 3 4 4
    `new$Sepal.Width[1:6]`: 4 3 17 3 4 4
    
    `old$Species[1:6]`: 1 1 1 1 1 1
    `new$Species[1:6]`: 1 1 2 1 1 1
    

    versus something like

        Sepal.Length Sepal.Width Petal.Length Petal.Width Species
      1          5.1         3.5          1.4         0.2  setosa
      2          4.9         3.0          1.4         0.2  setosa
    -------------------------------------------------------------------------
    < 3          4.7         3.2          1.3         0.2  setosa      (old)
    > 3          4.7        17.0          1.3         0.2  versicolor  (new)
    -------------------------------------------------------------------------
      4          4.6         3.1          1.5         0.2  setosa
      5          5.0         3.6          1.4         0.2  setosa
      6          5.4         3.9          1.7         0.4  setosa
    
  3. hadley commented on Jul 13, 2021

    @hadley
    Member

    One option would be to diff the printed data frame:

    iris_mod <- iris
    iris_mod[3,2] <- 17
    iris_mod[3,5] <- "versicolor"
    
    waldo::compare(
      capture.output(print(iris)), 
      capture.output(print(iris_mod))
    )
    #> old[1:7] vs new[1:7]
    #>   "    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species"
    #>   "1            5.1         3.5          1.4         0.2     setosa"
    #>   "2            4.9         3.0          1.4         0.2     setosa"
    #> - "3            4.7         3.2          1.3         0.2     setosa"
    #> + "3            4.7        17.0          1.3         0.2 versicolor"
    #>   "4            4.6         3.1          1.5         0.2     setosa"
    #>   "5            5.0         3.6          1.4         0.2     setosa"
    #>   "6            5.4         3.9          1.7         0.4     setosa"

    Created on 2021-07-13 by the reprex package (v2.0.0)

    But you'd have to think carefully about when to use this as (e.g.) it doesn't show changes to the attributes of the data frame, and adding/removing columns doesn't generate particularly informative diffs:

    iris2 <- iris
    iris2$new_col <- "x"
    
    waldo::compare(
      capture.output(print(iris)), 
      capture.output(print(iris2))
    )
    #> old vs new
    #> - "    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species"
    #> + "    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species new_col"
    #> - "1            5.1         3.5          1.4         0.2     setosa"
    #> + "1            5.1         3.5          1.4         0.2     setosa       x"
    #> - "2            4.9         3.0          1.4         0.2     setosa"
    #> + "2            4.9         3.0          1.4         0.2     setosa       x"
    #> - "3            4.7         3.2          1.3         0.2     setosa"
    #> + "3            4.7         3.2          1.3         0.2     setosa       x"
    #> - "4            4.6         3.1          1.5         0.2     setosa"
    #> + "4            4.6         3.1          1.5         0.2     setosa       x"
    #> and 141 more ...

    Created on 2021-07-13 by the reprex package (v2.0.0)

    I think you'd probably also want to remove columns that are the same:

    iris_mod <- iris
    iris_mod[3,2] <- 17
    iris_mod[3,5] <- "versicolor"
    
    waldo::compare(
      capture.output(print(iris[c(2, 5)])), 
      capture.output(print(iris_mod[c(2, 5)]))
    )
    #>     old                          | new                             
    #> [1] "    Sepal.Width    Species" | "    Sepal.Width    Species" [1]
    #> [2] "1           3.5     setosa" | "1           3.5     setosa" [2]
    #> [3] "2           3.0     setosa" | "2           3.0     setosa" [3]
    #> [4] "3           3.2     setosa" - "3          17.0 versicolor" [4]
    #> [5] "4           3.1     setosa" | "4           3.1     setosa" [5]
    #> [6] "5           3.6     setosa" | "5           3.6     setosa" [6]
    #> [7] "6           3.9     setosa" | "6           3.9     setosa" [7]

    Created on 2021-07-13 by the reprex package (v2.0.0)

    (but probably always force to use the line-by-line diff)

  4. hadley commented on Jul 13, 2021

    @hadley
    Member

    Would also need to think through the general problem of columns that print the same but have different underlying values:

    iris2 <- iris
    iris2$Species <- as.character(iris$Species)
    
    waldo::compare(
      capture.output(print(iris)), 
      capture.output(print(iris2))
    )
    #> ✓ No differences

    Created on 2021-07-13 by the reprex package (v2.0.0)

    But that's probably mostly resolved by only using this method if column attributes are the same.

  5. hadley commented on Jul 14, 2021

    @hadley
    Member
    • Want to make sure column names are shown whenever there's a diff — need to extract out line-by-line into separate function then provide header arg?
    • Think about numeric tolerance issues — maybe always compute structure and then use if !same_cols(x, y) or the printed diff is empty? Or double check each column with num_equal() ?
  6. hadley commented on Jul 14, 2021

    @hadley
    Member

    Another good case to think about is factor column with same levels but in different order.

  7. hadley commented on Jul 15, 2021

    @hadley
    Member

    I think the way around all these challenges is to display the rowwise diff as well as the column based diffs.

  8. hadley commented on Jul 15, 2021

    @hadley
    Member

    So this is what the motivating example looks like:

    old vs new
               Sepal.Width    Species
       [  1, ]         3.5     setosa
       [  2, ]         3.0     setosa
    -  [  3, ]         3.2     setosa
    +  [  3, ]        17.0 versicolor
       [  4, ]         3.1     setosa
       [  5, ]         3.6     setosa
       [  6, ]         3.9     setosa
    
    `old$Sepal.Width[1:6]`: 4 3  3 3 4 4
    `new$Sepal.Width[1:6]`: 4 3 17 3 4 4
    
    `old$Species[1:6]`: 1 1 1 1 1 1
    `new$Species[1:6]`: 1 1 2 1 1 1
  9. added a commit that references this issue on Jul 15, 2021
    d23ee41
  10. added a commit that references this issue on Jul 16, 2021
    b82de54
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions