knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "##",
  fig.path = "README-"
)

alike - Verify S3 Object Structure

Project Status: Unsupported – The project has reached a stable, usable state but the author(s) have ceased all work on it.

This project is being integrated into vetr. Please submit but reports, feature requests, etc., on that project's home page.

What is Alikeness?

alike is similar to all.equal from base R except it only compares object structure. As with all.equal, the first argument (target) must be matched by the second (current).

library(alike)
alike(integer(5), 1:5)      # different values, but same structure
alike(integer(5), 1:4)      # wrong size
alike(integer(26), letters) # same size, but different types

alike only compares structural elements that are defined in target (a.k.a. the template). This allows "wildcard" templates. For example, we consider length zero vectors to have undefined length so those match vectors of any length:

alike(integer(), 1:5)
alike(integer(), 1:4)
alike(integer(), letters)  # type is still defined and must match

Similarly, if a template does not specify an attribute, objects with any value for that attribute will match:

alike(list(), data.frame())  # a data frame is a list with a attributes
alike(data.frame(), list())  # but a list does not have the data.frame attributes

As an extension to the wildcard concept, we interpret partially specified core R attributes. Here we allow any three column integer matrix to match:

mx.tpl <- matrix(integer(), ncol=3)          # partially specified matrix
alike(mx.tpl, matrix(sample(1:12), nrow=4))  # any number of rows match
alike(mx.tpl, matrix(sample(1:12), nrow=3))  # but column count must match

or a data frame of arbitrary number of rows, but same column structure as iris:

iris.tpl <- iris[0, ]                        # no rows, but structure is defined
alike(iris.tpl, iris[1:10, ])                # any number of rows match
alike(iris.tpl, CO2)                         # but column structure must match

"alikeness" is complex to describe, but should be intuitive to grasp. We recommend you look at the examples in the documentation for alike to get a sense for alikeness. If you want to understand the specifics, read on.

NOTE: this is a snippet from the vignette; we recommend you look at the actual vignette as it will show the evaluated R expressions and the links will work.

Installation

Currently this package is only available from github:

library(devtools)
install_github("brodieg/cstringr")  # required dependency
install_github("brodieg/alike")


brodieG/alike documentation built on May 13, 2019, 7:44 a.m.