contains: Query an object with another object

Description Usage Arguments Details Value See Also Examples

Description

One use of contains is to test whether all names of a query list occur as names in a data list and optionally also whether they point to the same elements; this principle is applied recursively to all contained lists. The check methods apply various tests to objects.

Usage

1
2
3
4
5
6
7
  ## S4 method for signature 'character,missing'
check(object, against) 
  ## S4 method for signature 'list,character'
check(object, against) 

  ## S4 method for signature 'list,list'
contains(object, other, values = TRUE, exact = FALSE, ...)

Arguments

object

List or data frame containing the data, or character vector describing problems, if any.

against

Character vector whose names indicate column names of object and whose values indicate types or classes to assert.

other

List used as query.

values

Logical scalar. Compare also the values or only the keys? If FALSE, exact is ignored.

exact

Logical scalar. If FALSE, the data value(s) might by any of the query value(s), and some coercion is done before comparing (see match for details.

If TRUE, the data value(s) must exactly correspond to the query value(s), and no coercion is done (see identical) for details). This might be too strict for most applications.

...

Optional arguments passed to identical from the base package, allowing for fine-control of identity. Has no effect unless exact is TRUE.

Details

Non-list elements are ignored by contains if values is FALSE. Otherwise the comparison is done using identical if exact is TRUE. If exact is FALSE, the value(s) in the data list can be any of the values at the corresponding position in the query list, and the comparison is done by coercion to character vectors. An empty query list results in TRUE. Missing names in a non-empty query list result in FALSE.

The check method for data frames tests for the presence of each column listed by against in object. For the columns found, it checks whether is.<name> returns TRUE, which <name> given by the according element of against. It is an error if the function is.<name> does not exist.

The check method for character vectors is a simple helper method that raises an error unless the vector is empty.

Value

contains yields a logical scalar, check a (potentially empty) character vector describing each failed assertion when object is a list.

See Also

base::list base::as.list base::'[' base::'[[' base::match

base::identity

Other coding-functions: L, LL, assert, case, collect, flatten, listing, map_names, map_values, must, set, sql, unnest

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# contains() list/list method
x <- list(a = 1:8, c = 9, d = list(d1 = 'x', d2 = 'y'))
y <- list(a = 1:10, c = "9", d = list(d1 = "x"))
stopifnot(contains(x, y))
stopifnot(!contains(x, y, exact = TRUE))
stopifnot(contains(x, y, exact = TRUE, values = FALSE))

# check() data.frame/character method
(x <- check(Puromycin,
  c(conc = "numeric", rate = "numeric", state = "factor")))
(y <- check(Puromycin,
  c(missing = "numeric", rate = "numeric", state = "character")))
stopifnot(is.character(x), is.character(y))

pkgutils documentation built on May 2, 2019, 5:49 p.m.

Related to contains in pkgutils...