Git Product home page Git Product logo

waldo's Introduction

waldo

Codecov test coverage R build status

The goal of waldo is to find and concisely describe the difference between a pair of R objects, with the primary goal of making it easier to figure out what’s gone wrong in your unit tests.

waldo::compare() is inspired by all.equal(), but takes additional care to generate actionable insights by:

  • Ordering the differences from most important to least important.
  • Displaying the values of atomic vectors that are actually different.
  • Carefully using colour to emphasise changes (while still being readable when colour isn’t available).
  • Using R code (not a text description) to show where differences arise.
  • Where possible, comparing elements by name, rather than by position.
  • Erring on the side of producing too much output, rather than too little.

Installation

You can install the released version of waldo from CRAN with:

install.packages("waldo")

Comparisons

library(waldo)

When comparing atomic vectors, compare() produces diffs (thanks to diffobj) that highlight additions, deletions, and changes, along with a little context:

  • Addition

    compare(c("a", "b", "c"), c("a", "b"))
    
  • Deletion

    compare(c("a", "b"), c("a", "b", "c"))
    
  • Change

    compare(c("a", "b", "c"), c("a", "B", "c"))
    
  • Long vectors with short differences only show local context around changes, not everything that’s the same.

    compare(c("X", letters), c(letters, "X"))
    

Depending on the relative size of the differences and the width of your console you’ll get one of three displays:

  • The default display is to show the vectors one atop the other:

    compare(letters[1:5], letters[1:6])
    
  • If there’s not enough room for that, the two vectors are shown side-by-side:

    options(width = 20)
    compare(letters[1:5], letters[1:6])
    
  • And if there’s still not enough room for side-by-side, the each element is given its own line:

    options(width = 10)
    compare(letters[1:5], letters[1:6])
    

When comparing more complex objects, waldo creates an executable code path telling you where the differences lie:

  • Unnamed lists are compared by position:

    compare(list(factor("x")), list(1L))
    
  • Named lists, including data frames, are compared by name. For example, note that the following comparison reports a difference in the class and names, but not the values of the columns.

    df1 <- data.frame(x = 1:3, y = 3:1)
    df2 <- tibble::tibble(rev(df1))
    compare(df1, df2)
    
  • Recursion can be arbitrarily deep:

    x <- list(a = list(b = list(c = list(structure(1, e = 1)))))
    y <- list(a = list(b = list(c = list(structure(1, e = "a")))))
    compare(x, y)
    

waldo's People

Contributors

batpigandme avatar brodieg avatar davisvaughan avatar hadley avatar krlmlr avatar mgirlich avatar michaelquinn32 avatar mnazarov avatar torockel avatar

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.