[Rd] all.equal is hard to use

hong shen hshen_1998 at yahoo.com
Sun Mar 22 16:24:34 CET 2009


Hi,

I have extensive programming experience (Winodws, Unix, scripting, compiled languages, you name it) but new to R.

I found that it is quite hard to interpret the results returned by all.equal (base). The main problem is that when attributes are compared, they are sorted in attr.all.equal but in the result, the index of diff component is from the sorted list not the original list. I think that adding the component name to the printout may make users' life a little bit easier like

function (target, current, check.attributes = TRUE, ...) 
{
    msg <- if (check.attributes) 
        # if it is called by attr.all.equal(), target and current
        # are lists returned from attributes(original target | current). 
        # So attributes of target and current are the attributes of attributes,
        # which contains only "names". 
        attr.all.equal(target, current, ...)
    iseq <- if (length(target) == length(current)) {
        # if the length is equal, iseq will be a (1, 2, ... length)
        seq_along(target)
    }
    else {
        if (!is.null(msg)) 
            # remove old msg about "Lengths"
            msg <- msg[-grep("\\bLengths\\b", msg)]
        nc <- min(length(target), length(current))
        msg <- c(msg, paste("Length mismatch: comparison on first", 
            nc, "components"))
        # iseq is (1,2, ..., shorter of two lengthes)
        seq_len(nc)
    }
    for (i in iseq) {
        # compare each element in the list with all.equal.
        mi <- all.equal(target[[i]], current[[i]], check.attributes = check.attributes, 
            ...)
        if (is.character(mi)) {
           ############################################################
           #### print out name if possible ############################
           if (!is.null(names(target)[i]) && !is.null(names(current)[i]))
             msg <- c(msg, paste("Component ", i, ": ", mi, "with target name: ", names(target)[i], ", current name: ", 

names(current)[i], sep = ""))
           else if (!is.null(names(target)[i]))
             msg <- c(msg, paste("Component ", i, ": ", mi, "with target name: ", names(target)[i], sep = ""))
           else if (!is.null(names(current)[i]))
             msg <- c(msg, paste("Component ", i, ": ", mi, "with current name: ", names(current)[i], sep = ""))
           else
	     msg <- c(msg, paste("Component ", i, ": ", mi, sep = ""))
           ############################################################
        }
    }
    if (is.null(msg)) 
        TRUE
    else msg
}

Hong Shen



More information about the R-devel mailing list