Movatterモバイル変換


[0]ホーム

URL:


Type:Package
Title:Fast and Versatile Argument Checks
Description:Tests and assertions to perform frequent argument checks. A substantial part of the package was written in C to minimize any worries about execution time overhead.
Version:2.3.4
URL:https://mllg.github.io/checkmate/,https://github.com/mllg/checkmate
URLNote:https://github.com/mllg/checkmate
BugReports:https://github.com/mllg/checkmate/issues
NeedsCompilation:yes
ByteCompile:yes
Encoding:UTF-8
Depends:R (≥ 3.0.0)
Imports:backports (≥ 1.1.0), utils
Suggests:R6, fastmatch, data.table (≥ 1.9.8), devtools, ggplot2,knitr, magrittr, microbenchmark, rmarkdown, testthat (≥3.0.4), tinytest (≥ 1.1.0), tibble
License:BSD_3_clause + file LICENSE
VignetteBuilder:knitr
RoxygenNote:7.3.3
Collate:'AssertCollection.R' 'allMissing.R' 'anyInfinite.R''anyMissing.R' 'anyNaN.R' 'asInteger.R' 'assert.R' 'helper.R''makeExpectation.R' 'makeTest.R' 'makeAssertion.R''checkAccess.R' 'checkArray.R' 'checkAtomic.R''checkAtomicVector.R' 'checkCharacter.R' 'checkChoice.R''checkClass.R' 'checkComplex.R' 'checkCount.R''checkDataFrame.R' 'checkDataTable.R' 'checkDate.R''checkDirectoryExists.R' 'checkDisjunct.R' 'checkDouble.R''checkEnvironment.R' 'checkFALSE.R' 'checkFactor.R''checkFileExists.R' 'checkFlag.R' 'checkFormula.R''checkFunction.R' 'checkInt.R' 'checkInteger.R''checkIntegerish.R' 'checkList.R' 'checkLogical.R''checkMatrix.R' 'checkMultiClass.R' 'checkNamed.R''checkNames.R' 'checkNull.R' 'checkNumber.R' 'checkNumeric.R''checkOS.R' 'checkPOSIXct.R' 'checkPathForOutput.R''checkPermutation.R' 'checkR6.R' 'checkRaw.R' 'checkScalar.R''checkScalarNA.R' 'checkSetEqual.R' 'checkString.R''checkSubset.R' 'checkTRUE.R' 'checkTibble.R' 'checkVector.R''coalesce.R' 'isIntegerish.R' 'matchArg.R' 'qassert.R''qassertr.R' 'vname.R' 'wfwl.R' 'zzz.R'
Packaged:2026-02-02 10:59:55 UTC; michel
Author:Michel LangORCID iD [cre, aut], Bernd Bischl [ctb], Dénes TóthORCID iD [ctb]
Maintainer:Michel Lang <michellang@gmail.com>
Repository:CRAN
Date/Publication:2026-02-03 08:40:02 UTC

checkmate: Fast and Versatile Argument Checks

Description

Tests and assertions to perform frequent argument checks. A substantial part of the package was written in C to minimize any worries about execution time overhead.

Check scalars

Check vectors

Check attributes

Check compound types

Check other built-in R types

Check sets

File IO

Popular data types of third party packages

Safe coercion to integer

Quick argument checks using a DSL

Misc

Author(s)

Maintainer: Michel Langmichellang@gmail.com (ORCID)

Other contributors:

See Also

Useful links:


Coalesce operator

Description

Returns the left hand side if not missing norNULL, andthe right hand side otherwise.

Usage

lhs %??% rhs

Arguments

lhs

[any]
Left hand side of the operator. Is returned if not missing orNULL.

rhs

[any]
Right hand side of the operator. Is returned iflhs is missing orNULL.

Value

Eitherlhs orrhs.

Examples

print(NULL %??% 1 %??% 2)print(names(iris) %??% letters[seq_len(ncol(iris))])

Collect multiple assertions

Description

The functionmakeAssertCollection() returns a simple stack-likeclosure you can pass to all functions of theassert*-family.All messages get collected and can be reported withreportAssertions().Alternatively, you can easily write your own report function or customize the the output ofthe report function to a certain degree.See the example on how to push custom messages or retrieve all stored messages.

Usage

makeAssertCollection()reportAssertions(collection)

Arguments

collection

[AssertCollection]
Object of type “AssertCollection” (constructed viamakeAssertCollection).

Value

makeAssertCollection() returns an object of class “AssertCollection” andreportCollection returns invisiblyTRUE if no error is thrown (i.e., no message wascollected).

Examples

x = "a"coll = makeAssertCollection()print(coll$isEmpty())assertNumeric(x, add = coll)coll$isEmpty()coll$push("Custom error message")coll$getMessages()## Not run:   reportAssertions(coll)## End(Not run)

Check if an object contains missing values

Description

anyMissing checks for the presence of at least one missing value,allMissing checks for the presence of at least one non-missing value.Supported are atomic types (seeis.atomic), lists and data frames.Missingness is defined asNA orNaN for atomic types and data frame columns,NULL is defined as missing for lists.
allMissing applied to adata.frame returnsTRUE if at least one column hasonly non-missing values. If you want to perform the less frequent check that there is at leasta single non-missing observation present in thedata.frame, useall(sapply(df, allMissing)) instead.

Usage

allMissing(x)anyMissing(x)

Arguments

x

[any]
Object to check.

Value

[logical(1)] ReturnsTRUE if any (anyMissing) or all (allMissing)elements ofx are missing (see details),FALSE otherwise.

Examples

allMissing(1:2)allMissing(c(1, NA))allMissing(c(NA, NA))x = data.frame(a = 1:2, b = NA)# Note how allMissing combines the results for data frames:allMissing(x)all(sapply(x, allMissing))anyMissing(c(1, 1))anyMissing(c(1, NA))anyMissing(list(1, NULL))x = irisx[, "Species"] = NAanyMissing(x)allMissing(x)

Check if an object contains infinite values

Description

Supported are atomic types (seeis.atomic), lists and data frames.

Usage

anyInfinite(x)

Arguments

x

[any]
Object to check.

Value

[logical(1)] ReturnsTRUE if any element is-Inf orInf.

Examples

anyInfinite(1:10)anyInfinite(c(1:10, Inf))iris[3, 3] = InfanyInfinite(iris)

Check if an object contains NaN values

Description

Supported are atomic types (seeis.atomic), lists and data frames.

Usage

anyNaN(x)

Arguments

x

[any]
Object to check.

Value

[logical(1)] ReturnsTRUE if any element isNaN.

Examples

anyNaN(1:10)anyNaN(c(1:10, NaN))iris[3, 3] = NaNanyNaN(iris)

Convert an argument to an integer

Description

asInteger is intended to be used for vectors whileasInt isa specialization for scalar integers andasCount for scalarnon-negative integers.Convertible are (a) atomic vectors with all elementsNAand (b) double vectors with all elements being withintolrange of an integer.

Note that these functions may be deprecated in the future.Instead, it is advised to useassertCount,assertInt orassertIntegerish withargumentcoerce set toTRUE instead.

Usage

asInteger(  x,  tol = sqrt(.Machine$double.eps),  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  .var.name = vname(x))asCount(  x,  na.ok = FALSE,  positive = FALSE,  tol = sqrt(.Machine$double.eps),  .var.name = vname(x))asInt(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  tol = sqrt(.Machine$double.eps),  .var.name = vname(x))

Arguments

x

[any]
Object to convert.

tol

[double(1)]
Numerical tolerance used to check whether a double or complex can be converted.Default issqrt(.Machine$double.eps).

lower

[numeric(1)]
Lower value all elements ofx must be greater than or equal to.

upper

[numeric(1)]
Upper value all elements ofx must be lower than or equal to.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

sorted

[logical(1)]
Elements must be sorted in ascending order. Missing values are ignored.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

.var.name

[character(1)]
Name of the checked object to print in error messages. Defaults tothe heuristic implemented invname.

na.ok

[logical(1)]
Are missing values allowed? Default isFALSE.

positive

[logical(1)]
Mustx be positive (>= 1)?Default isFALSE.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Convertedx.

Examples

asInteger(c(1, 2, 3))asCount(1)asInt(1)

Combine multiple checks into one assertion

Description

You can call this function with an arbitrary number of ofcheck*functions, i.e. functions provided by this package or your own functions whichreturnTRUE on success and the error message ascharacter(1) otherwise.The resulting assertion is successful, ifcombine is“or” (default) and at least one check evaluates toTRUE orcombine is “and” and all checks evaluate toTRUE.Otherwise,assert throws an informative error message.

Usage

assert(..., combine = "or", .var.name = NULL, add = NULL)

Arguments

...

[any]
List of calls to check functions.

combine

[character(1)]
“or” or “and” to combine the check functions with an ORor AND, respectively.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

Value

Throws an error (or pushes the error message to anAssertCollection ifadd is notNULL)if the checks fail and invisibly returnsTRUE otherwise.

Examples

x = 1:10assert(checkNull(x), checkInteger(x, any.missing = FALSE))collection <- makeAssertCollection()assert(checkChoice(x, c("a", "b")), checkDataFrame(x), add = collection)collection$getMessages()

Check file system access rights

Description

Check file system access rights

Usage

checkAccess(x, access = "")check_access(x, access = "")assertAccess(x, access = "", .var.name = vname(x), add = NULL)assert_access(x, access = "", .var.name = vname(x), add = NULL)testAccess(x, access = "")test_access(x, access = "")expect_access(x, access = "", info = NULL, label = vname(x))

Arguments

x

[any]
Object to check.

access

[character(1)]
Single string containing possible characters ‘r’, ‘w’ and ‘x’ toforce a check for read, write or execute access rights, respectively.Write and executable rights are not checked on Windows.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertAccess/assert_access returnx invisibly, whereascheckAccess/check_access andtestAccess/test_access returnTRUE.If the check is not successful,assertAccess/assert_accessthrows an error message,testAccess/test_accessreturnsFALSE,andcheckAccess/check_access return a string with the error message.The functionexpect_access always returns anexpectation.

See Also

Other filesystem:checkDirectoryExists(),checkFileExists(),checkPathForOutput()

Examples

# Is R's home directory readable?testAccess(R.home(), "r")# Is R's home directory writeable?testAccess(R.home(), "w")

Check if an argument is an array

Description

Check if an argument is an array

Usage

checkArray(  x,  mode = NULL,  any.missing = TRUE,  d = NULL,  min.d = NULL,  max.d = NULL,  null.ok = FALSE)check_array(  x,  mode = NULL,  any.missing = TRUE,  d = NULL,  min.d = NULL,  max.d = NULL,  null.ok = FALSE)assertArray(  x,  mode = NULL,  any.missing = TRUE,  d = NULL,  min.d = NULL,  max.d = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_array(  x,  mode = NULL,  any.missing = TRUE,  d = NULL,  min.d = NULL,  max.d = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testArray(  x,  mode = NULL,  any.missing = TRUE,  d = NULL,  min.d = NULL,  max.d = NULL,  null.ok = FALSE)test_array(  x,  mode = NULL,  any.missing = TRUE,  d = NULL,  min.d = NULL,  max.d = NULL,  null.ok = FALSE)expect_array(  x,  mode = NULL,  any.missing = TRUE,  d = NULL,  min.d = NULL,  max.d = NULL,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

mode

[character(1)]
Storage mode of the array. Arrays can hold vectors, i.e. “logical”,“integer”, “integerish”, “double”, “numeric”, “complex”,“character” and “list”. You can also specify “atomic”here to explicitly prohibit lists. Default isNULL (no check).If all values ofx are missing, this check is skipped.

any.missing

[logical(1)]
Are missing values allowed? Default isTRUE.

d

[integer(1)]
Exact number of dimensions of arrayx.Default isNULL (no check).

min.d

[integer(1)]
Minimum number of dimensions of arrayx.Default isNULL (no check).

max.d

[integer(1)]
Maximum number of dimensions of arrayx.Default isNULL (no check).

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertArray/assert_array returnx invisibly, whereascheckArray/check_array andtestArray/test_array returnTRUE.If the check is not successful,assertArray/assert_arraythrows an error message,testArray/test_arrayreturnsFALSE,andcheckArray/check_array return a string with the error message.The functionexpect_array always returns anexpectation.

See Also

Other basetypes:checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Other compound:checkDataFrame(),checkDataTable(),checkMatrix(),checkTibble()

Examples

checkArray(array(1:27, dim = c(3, 3, 3)), d = 3)

Check that an argument is an atomic vector

Description

For the definition of “atomic”, seeis.atomic.

Note that 'NULL' is recognized as a valid atomic value, as in R versions up to version 4.3.x.For details, seehttps://stat.ethz.ch/pipermail/r-devel/2023-September/082892.html.

Usage

checkAtomic(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL)check_atomic(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL)assertAtomic(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  .var.name = vname(x),  add = NULL)assert_atomic(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  .var.name = vname(x),  add = NULL)testAtomic(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL)test_atomic(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL)expect_atomic(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertAtomic/assert_atomic returnx invisibly, whereascheckAtomic/check_atomic andtestAtomic/test_atomic returnTRUE.If the check is not successful,assertAtomic/assert_atomicthrows an error message,testAtomic/test_atomicreturnsFALSE,andcheckAtomic/check_atomic return a string with the error message.The functionexpect_atomic always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Other atomicvector:checkAtomicVector(),checkVector()

Examples

testAtomic(letters, min.len = 1L, any.missing = FALSE)

Check that an argument is an atomic vector

Description

An atomic vector is defined slightly different from specifications inis.atomic andis.vector:An atomic vector is eitherlogical,integer,numeric,complex,character orraw and can have any attributes except adimension attribute (like matrices).I.e., afactor is an atomic vector, but a matrix orNULL are not.In short, this is basically equivalent tois.atomic(x) && !is.null(x) && is.null(dim(x)).

Usage

checkAtomicVector(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL)check_atomic_vector(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL)assertAtomicVector(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  .var.name = vname(x),  add = NULL)assert_atomic_vector(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  .var.name = vname(x),  add = NULL)testAtomicVector(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL)test_atomic_vector(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL)expect_atomic_vector(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with only missing values allowed? Default isTRUE.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertAtomicVector/assert_atomic_vector returnx invisibly, whereascheckAtomicVector/check_atomic_vector andtestAtomicVector/test_atomic_vector returnTRUE.If the check is not successful,assertAtomicVector/assert_atomic_vectorthrows an error message,testAtomicVector/test_atomic_vectorreturnsFALSE,andcheckAtomicVector/check_atomic_vector return a string with the error message.The functionexpect_atomic_vector always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Other atomicvector:checkAtomic(),checkVector()

Examples

testAtomicVector(letters, min.len = 1L, any.missing = FALSE)

Check if an argument is a vector of type character

Description

To check for scalar strings, seecheckString.

Usage

checkCharacter(  x,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)check_character(  x,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)assertCharacter(  x,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_character(  x,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testCharacter(  x,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)test_character(  x,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)expect_character(  x,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

n.chars

[integer(1)]
Exact number of characters for each element ofx.

min.chars

[integer(1)]
Minimum number of characters for each element ofx.

max.chars

[integer(1)]
Maximum number of characters for each element ofx.

pattern

[character(1)]
Regular expression as used ingrepl.All non-missing elements ofx must comply to this pattern.

fixed

[character(1)]
Substring to detect inx. Will be used aspattern ingreplwith optionfixed set toTRUE.All non-missing elements ofx must contain this substring.

ignore.case

[logical(1)]
Seegrepl. Default isFALSE.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

sorted

[logical(1)]
Elements must be sorted in ascending order. Missing values are ignored.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

typed.missing

[logical(1)]
If set toFALSE (default), all types of missing values (NA,NA_integer_,NA_real_,NA_character_ orNA_character_) as well as empty vectors are allowedwhile type-checking atomic input.Set toTRUE to enable strict type checking.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertCharacter/assert_character returnx invisibly, whereascheckCharacter/check_character andtestCharacter/test_character returnTRUE.If the check is not successful,assertCharacter/assert_characterthrows an error message,testCharacter/test_characterreturnsFALSE,andcheckCharacter/check_character return a string with the error message.The functionexpect_character always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

testCharacter(letters, min.len = 1, any.missing = FALSE)testCharacter(letters, min.chars = 2)testCharacter("example", pattern = "xa")

Check if an object is an element of a given set

Description

Check if an object is an element of a given set

Usage

checkChoice(x, choices, null.ok = FALSE, fmatch = FALSE)check_choice(x, choices, null.ok = FALSE, fmatch = FALSE)assertChoice(  x,  choices,  null.ok = FALSE,  fmatch = FALSE,  .var.name = vname(x),  add = NULL)assert_choice(  x,  choices,  null.ok = FALSE,  fmatch = FALSE,  .var.name = vname(x),  add = NULL)testChoice(x, choices, null.ok = FALSE, fmatch = FALSE)test_choice(x, choices, null.ok = FALSE, fmatch = FALSE)expect_choice(  x,  choices,  null.ok = FALSE,  fmatch = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

choices

[atomic]
Set of possible values.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

fmatch

[logical(1)]
Use the set operations implemented infmatch in packagefastmatch.Iffastmatch is not installed, this silently falls back tomatch.fmatch modifiesy by reference:A hash table is added as attribute which is used in subsequent calls.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertChoice/assert_choice returnx invisibly, whereascheckChoice/check_choice andtestChoice/test_choice returnTRUE.If the check is not successful,assertChoice/assert_choicethrows an error message,testChoice/test_choicereturnsFALSE,andcheckChoice/check_choice return a string with the error message.The functionexpect_choice always returns anexpectation.

Note

The objectx must be of the same type as the set w.r.t.typeof.Integers and doubles are both treated as numeric.

See Also

Other set:checkDisjunct(),checkPermutation(),checkSetEqual(),checkSubset()

Examples

testChoice("x", letters)# x is not converted before the comparison (except for numerics)testChoice(factor("a"), "a")testChoice(1, "1")testChoice(1, as.integer(1))

Check the class membership of an argument

Description

Check the class membership of an argument

Usage

checkClass(x, classes, ordered = FALSE, null.ok = FALSE)check_class(x, classes, ordered = FALSE, null.ok = FALSE)assertClass(  x,  classes,  ordered = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_class(  x,  classes,  ordered = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testClass(x, classes, ordered = FALSE, null.ok = FALSE)test_class(x, classes, ordered = FALSE, null.ok = FALSE)expect_class(  x,  classes,  ordered = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

classes

[character]
Class names to check for inheritance withinherits.x must inherit from all specified classes.

ordered

[logical(1)]
Expectx to be specialized in provided order.Default isFALSE.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertClass/assert_class returnx invisibly, whereascheckClass/check_class andtestClass/test_class returnTRUE.If the check is not successful,assertClass/assert_classthrows an error message,testClass/test_classreturnsFALSE,andcheckClass/check_class return a string with the error message.The functionexpect_class always returns anexpectation.

See Also

Other attributes:checkMultiClass(),checkNamed(),checkNames()

Other classes:checkMultiClass(),checkR6()

Examples

# Create an object with classes "foo" and "bar"x = 1class(x) = c("foo", "bar")# is x of class "foo"?testClass(x, "foo")# is x of class "foo" and "bar"?testClass(x, c("foo", "bar"))# is x of class "foo" or "bar"?## Not run: assert(  checkClass(x, "foo"),  checkClass(x, "bar"))## End(Not run)# is x most specialized as "bar"?testClass(x, "bar", ordered = TRUE)

Check if an argument is a vector of type complex

Description

Check if an argument is a vector of type complex

Usage

checkComplex(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)check_complex(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)assertComplex(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_complex(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testComplex(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)test_complex(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)expect_complex(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

typed.missing

[logical(1)]
If set toFALSE (default), all types of missing values (NA,NA_integer_,NA_real_,NA_character_ orNA_character_) as well as empty vectors are allowedwhile type-checking atomic input.Set toTRUE to enable strict type checking.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertComplex/assert_complex returnx invisibly, whereascheckComplex/check_complex andtestComplex/test_complex returnTRUE.If the check is not successful,assertComplex/assert_complexthrows an error message,testComplex/test_complexreturnsFALSE,andcheckComplex/check_complex return a string with the error message.The functionexpect_complex always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

testComplex(1)testComplex(1+1i)

Check if an argument is a count

Description

A count is defined as non-negative integerish value.

Usage

checkCount(  x,  na.ok = FALSE,  positive = FALSE,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE)check_count(  x,  na.ok = FALSE,  positive = FALSE,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE)assertCount(  x,  na.ok = FALSE,  positive = FALSE,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE,  coerce = FALSE,  .var.name = vname(x),  add = NULL)assert_count(  x,  na.ok = FALSE,  positive = FALSE,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE,  coerce = FALSE,  .var.name = vname(x),  add = NULL)testCount(  x,  na.ok = FALSE,  positive = FALSE,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE)test_count(  x,  na.ok = FALSE,  positive = FALSE,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE)expect_count(  x,  na.ok = FALSE,  positive = FALSE,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

na.ok

[logical(1)]
Are missing values allowed? Default isFALSE.

positive

[logical(1)]
Mustx be positive (>= 1)?Default isFALSE, allowing 0.

tol

[double(1)]
Numerical tolerance used to check whether a double or complex can be converted.Default issqrt(.Machine$double.eps).

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

coerce

[logical(1)]
IfTRUE, the inputx is returned as integer after an successful assertion.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertCount/assert_count returnx invisibly, whereascheckCount/check_count andtestCount/test_count returnTRUE.If the check is not successful,assertCount/assert_countthrows an error message,testCount/test_countreturnsFALSE,andcheckCount/check_count return a string with the error message.The functionexpect_count always returns anexpectation.

Note

To perform an assertion and then convert to integer, useasCount.assertCount will not convert numerics to integer.

See Also

Other scalars:checkFlag(),checkInt(),checkNumber(),checkScalar(),checkScalarNA(),checkString()

Examples

testCount(1)testCount(-1)

Check if an argument is a data frame

Description

Check if an argument is a data frame

Usage

checkDataFrame(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)check_data_frame(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)assertDataFrame(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_data_frame(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testDataFrame(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)test_data_frame(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)expect_data_frame(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

types

[character]
Character vector of class names. Each list element must inheritfrom at least one of the provided types.The types “logical”, “integer”, “integerish”, “double”,“numeric”, “complex”, “character”, “factor”, “atomic”, “vector”“atomicvector”, “array”, “matrix”, “list”, “function”,“environment” and “null” are supported.For other typesinherits is used as a fallback to checkx's inheritance.Defaults tocharacter(0) (no check).

any.missing

[logical(1)]
Are missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are columns with only missing values allowed? Default isTRUE.

min.rows

[integer(1)]
Minimum number of rows.

max.rows

[integer(1)]
Maximum number of rows.

min.cols

[integer(1)]
Minimum number of columns.

max.cols

[integer(1)]
Maximum number of columns.

nrows

[integer(1)]
Exact number of rows.

ncols

[integer(1)]
Exact number of columns.

row.names

[character(1)]
Check for row names. Default is “NULL” (no check).SeecheckNamed for possible values.Note that you can usecheckSubset to check for a specific set of names.

col.names

[character(1)]
Check for column names. Default is “NULL” (no check).SeecheckNamed for possible values.Note that you can usecheckSubset to test for a specific set of names.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertDataFrame/assert_data_frame returnx invisibly, whereascheckDataFrame/check_data_frame andtestDataFrame/test_data_frame returnTRUE.If the check is not successful,assertDataFrame/assert_data_framethrows an error message,testDataFrame/test_data_framereturnsFALSE,andcheckDataFrame/check_data_frame return a string with the error message.The functionexpect_data_frame always returns anexpectation.

See Also

Other compound:checkArray(),checkDataTable(),checkMatrix(),checkTibble()

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

testDataFrame(iris)testDataFrame(iris, types = c("numeric", "factor"), min.rows = 1, col.names = "named")

Check if an argument is a data table

Description

Check if an argument is a data table

Usage

checkDataTable(  x,  key = NULL,  index = NULL,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)check_data_table(  x,  key = NULL,  index = NULL,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)assertDataTable(  x,  key = NULL,  index = NULL,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_data_table(  x,  key = NULL,  index = NULL,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testDataTable(  x,  key = NULL,  index = NULL,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)test_data_table(  x,  key = NULL,  index = NULL,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)expect_data_table(  x,  key = NULL,  index = NULL,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

key

[character]
Expected primary key(s) of the data table.

index

[character]
Expected secondary key(s) of the data table.

types

[character]
Character vector of class names. Each list element must inheritfrom at least one of the provided types.The types “logical”, “integer”, “integerish”, “double”,“numeric”, “complex”, “character”, “factor”, “atomic”, “vector”“atomicvector”, “array”, “matrix”, “list”, “function”,“environment” and “null” are supported.For other typesinherits is used as a fallback to checkx's inheritance.Defaults tocharacter(0) (no check).

any.missing

[logical(1)]
Are missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are matrices with only missing values allowed? Default isTRUE.

min.rows

[integer(1)]
Minimum number of rows.

max.rows

[integer(1)]
Maximum number of rows.

min.cols

[integer(1)]
Minimum number of columns.

max.cols

[integer(1)]
Maximum number of columns.

nrows

[integer(1)]
Exact number of rows.

ncols

[integer(1)]
Exact number of columns.

row.names

[character(1)]
Check for row names. Default is “NULL” (no check).SeecheckNamed for possible values.Note that you can usecheckSubset to check for a specific set of names.

col.names

[character(1)]
Check for column names. Default is “NULL” (no check).SeecheckNamed for possible values.Note that you can usecheckSubset to test for a specific set of names.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertDataTable/assert_data_table returnx invisibly, whereascheckDataTable/check_data_table andtestDataTable/test_data_table returnTRUE.If the check is not successful,assertDataTable/assert_data_tablethrows an error message,testDataTable/test_data_tablereturnsFALSE,andcheckDataTable/check_data_table return a string with the error message.The functionexpect_data_table always returns anexpectation.

See Also

Other compound:checkArray(),checkDataFrame(),checkMatrix(),checkTibble()

Examples

library(data.table)dt = as.data.table(iris)setkeyv(dt, "Species")setkeyv(dt, "Sepal.Length", physical = FALSE)testDataTable(dt)testDataTable(dt, key = "Species", index = "Sepal.Length", any.missing = FALSE)

Check that an argument is a Date

Description

Checks that an object is of classDate.

Usage

checkDate(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  null.ok = FALSE)check_date(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  null.ok = FALSE)assertDate(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_date(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testDate(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  null.ok = FALSE)test_date(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  null.ok = FALSE)expect_date(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

lower

[Date]
All non-missing dates inx must be >= this date. Comparison is done viaOps.Date.

upper

[Date]
All non-missing dates inx must be before <= this date. Comparison is done viaOps.Date.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertAtomic/assert_atomic returnx invisibly, whereascheckAtomic/check_atomic andtestAtomic/test_atomic returnTRUE.If the check is not successful,assertAtomic/assert_atomicthrows an error message,testAtomic/test_atomicreturnsFALSE,andcheckAtomic/check_atomic return a string with the error message.The functionexpect_atomic always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()


Check for existence and access rights of directories

Description

Check for existence and access rights of directories

Usage

checkDirectoryExists(x, access = "")check_directory_exists(x, access = "")assertDirectoryExists(x, access = "", .var.name = vname(x), add = NULL)assert_directory_exists(x, access = "", .var.name = vname(x), add = NULL)testDirectoryExists(x, access = "")test_directory_exists(x, access = "")expect_directory_exists(x, access = "", info = NULL, label = vname(x))checkDirectory(x, access = "")assertDirectory(x, access = "", .var.name = vname(x), add = NULL)assert_directory(x, access = "", .var.name = vname(x), add = NULL)testDirectory(x, access = "")test_directory(x, access = "")expect_directory(x, access = "", info = NULL, label = vname(x))

Arguments

x

[any]
Object to check.

access

[character(1)]
Single string containing possible characters ‘r’, ‘w’ and ‘x’ toforce a check for read, write or execute access rights, respectively.Write and executable rights are not checked on Windows.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertDirectoryExists/assert_directory_exists returnx invisibly, whereascheckDirectoryExists/check_directory_exists andtestDirectoryExists/test_directory_exists returnTRUE.If the check is not successful,assertDirectoryExists/assert_directory_existsthrows an error message,testDirectoryExists/test_directory_existsreturnsFALSE,andcheckDirectoryExists/check_directory_exists return a string with the error message.The functionexpect_directory_exists always returns anexpectation.

Note

The functions without the suffix “exists” are deprecated and will be removedfrom the package in a future version due to name clashes.

See Also

Other filesystem:checkAccess(),checkFileExists(),checkPathForOutput()

Examples

# Is R's home directory readable?testDirectory(R.home(), "r")# Is R's home directory readable and writable?testDirectory(R.home(), "rw")

Check if an argument is disjunct from a given set

Description

Check if an argument is disjunct from a given set

Usage

checkDisjunct(x, y, fmatch = FALSE)check_disjunct(x, y, fmatch = FALSE)assertDisjunct(x, y, fmatch = FALSE, .var.name = vname(x), add = NULL)assert_disjunct(x, y, fmatch = FALSE, .var.name = vname(x), add = NULL)testDisjunct(x, y, fmatch = FALSE)test_disjunct(x, y, fmatch = FALSE)expect_disjunct(x, y, fmatch = FALSE, info = NULL, label = vname(x))

Arguments

x

[any]
Object to check.

y

[atomic]
Other Set.

fmatch

[logical(1)]
Use the set operations implemented infmatch in packagefastmatch.Iffastmatch is not installed, this silently falls back tomatch.fmatch modifiesy by reference:A hash table is added as attribute which is used in subsequent calls.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertDisjunct/assert_disjunct returnx invisibly, whereascheckDisjunct/check_disjunct andtestDisjunct/test_disjunct returnTRUE.If the check is not successful,assertDisjunct/assert_disjunctthrows an error message,testDisjunct/test_disjunctreturnsFALSE,andcheckDisjunct/check_disjunct return a string with the error message.The functionexpect_disjunct always returns anexpectation.

Note

The objectx must be of the same type as the set w.r.t.typeof.Integers and doubles are both treated as numeric.

See Also

Other set:checkChoice(),checkPermutation(),checkSetEqual(),checkSubset()

Examples

testDisjunct(1L, letters)testDisjunct(c("a", "z"), letters)# x is not converted before the comparison (except for numerics)testDisjunct(factor("a"), "a")testDisjunct(1, "1")testDisjunct(1, as.integer(1))

Check that an argument is a vector of type double

Description

Check that an argument is a vector of type double

Usage

checkDouble(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)check_double(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)assertDouble(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_double(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testDouble(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)test_double(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)expect_double(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

lower

[numeric(1)]
Lower value all elements ofx must be greater than or equal to.

upper

[numeric(1)]
Upper value all elements ofx must be lower than or equal to.

finite

[logical(1)]
Check for only finite values? Default isFALSE.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

sorted

[logical(1)]
Elements must be sorted in ascending order. Missing values are ignored.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

typed.missing

[logical(1)]
If set toFALSE (default), all types of missing values (NA,NA_integer_,NA_real_,NA_character_ orNA_character_) as well as empty vectors are allowedwhile type-checking atomic input.Set toTRUE to enable strict type checking.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertDouble/assert_double returnx invisibly, whereascheckDouble/check_double andtestDouble/test_double returnTRUE.If the check is not successful,assertDouble/assert_doublethrows an error message,testDouble/test_doublereturnsFALSE,andcheckDouble/check_double return a string with the error message.The functionexpect_double always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

testDouble(1)testDouble(1L)testDouble(1, min.len = 1, lower = 0)

Check if an argument is an environment

Description

Check if an argument is an environment

Usage

checkEnvironment(x, contains = character(0L), null.ok = FALSE)check_environment(x, contains = character(0L), null.ok = FALSE)assertEnvironment(  x,  contains = character(0L),  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_environment(  x,  contains = character(0L),  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testEnvironment(x, contains = character(0L), null.ok = FALSE)test_environment(x, contains = character(0L), null.ok = FALSE)expect_environment(  x,  contains = character(0L),  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

contains

[character]
Vector of object names expected in the environment.Defaults tocharacter(0).

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertEnvironment/assert_environment returnx invisibly, whereascheckEnvironment/check_environment andtestEnvironment/test_environment returnTRUE.If the check is not successful,assertEnvironment/assert_environmentthrows an error message,testEnvironment/test_environmentreturnsFALSE,andcheckEnvironment/check_environment return a string with the error message.The functionexpect_environment always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

ee = as.environment(list(a = 1))testEnvironment(ee)testEnvironment(ee, contains = "a")

Check if an argument is FALSE

Description

Simply checks if an argument isFALSE.

Usage

checkFALSE(x, na.ok = FALSE)check_false(x, na.ok = FALSE)assertFALSE(x, na.ok = FALSE, .var.name = vname(x), add = NULL)assert_false(x, na.ok = FALSE, .var.name = vname(x), add = NULL)testFALSE(x, na.ok = FALSE)test_false(x, na.ok = FALSE)

Arguments

x

[any]
Object to check.

na.ok

[logical(1)]
Are missing values allowed? Default isFALSE.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

Value

Depending on the function prefix:If the check is successful, the functionsassertFALSE./assert_false. returnx invisibly, whereascheckFALSE./check_false. andtestFALSE./test_false. returnTRUE.If the check is not successful,assertFALSE./assert_false.throws an error message,testFALSE./test_false.returnsFALSE,andcheckFALSE./check_false. return a string with the error message.The functionexpect_false. always returns anexpectation.

Examples

testFALSE(FALSE)testFALSE(TRUE)

Check if an argument is a factor

Description

Check if an argument is a factor

Usage

checkFactor(  x,  levels = NULL,  ordered = NA,  empty.levels.ok = TRUE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  n.levels = NULL,  min.levels = NULL,  max.levels = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)check_factor(  x,  levels = NULL,  ordered = NA,  empty.levels.ok = TRUE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  n.levels = NULL,  min.levels = NULL,  max.levels = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)assertFactor(  x,  levels = NULL,  ordered = NA,  empty.levels.ok = TRUE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  n.levels = NULL,  min.levels = NULL,  max.levels = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_factor(  x,  levels = NULL,  ordered = NA,  empty.levels.ok = TRUE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  n.levels = NULL,  min.levels = NULL,  max.levels = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testFactor(  x,  levels = NULL,  ordered = NA,  empty.levels.ok = TRUE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  n.levels = NULL,  min.levels = NULL,  max.levels = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)test_factor(  x,  levels = NULL,  ordered = NA,  empty.levels.ok = TRUE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  n.levels = NULL,  min.levels = NULL,  max.levels = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)expect_factor(  x,  levels = NULL,  ordered = NA,  empty.levels.ok = TRUE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  n.levels = NULL,  min.levels = NULL,  max.levels = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

levels

[character]
Vector of allowed factor levels.

ordered

[logical(1)]
Check for an ordered factor? IfFALSE orTRUE, checks explicitlyfor an unordered or ordered factor, respectively.Default isNA which does not perform any additional check.

empty.levels.ok

[logical(1)]
Are empty levels allowed?Default isTRUE.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

n.levels

[integer(1)]
Exact number of factor levels.Default isNULL (no check).

min.levels

[integer(1)]
Minimum number of factor levels.Default isNULL (no check).

max.levels

[integer(1)]
Maximum number of factor levels.Default isNULL (no check).

unique

[logical(1)]
Must all values be unique? Default isFALSE.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertFactor/assert_factor returnx invisibly, whereascheckFactor/check_factor andtestFactor/test_factor returnTRUE.If the check is not successful,assertFactor/assert_factorthrows an error message,testFactor/test_factorreturnsFALSE,andcheckFactor/check_factor return a string with the error message.The functionexpect_factor always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

x = factor("a", levels = c("a", "b"))testFactor(x)testFactor(x, empty.levels.ok = FALSE)

Check existence and access rights of files

Description

Check existence and access rights of files

Usage

checkFileExists(x, access = "", extension = NULL)check_file_exists(x, access = "", extension = NULL)assertFileExists(  x,  access = "",  extension = NULL,  .var.name = vname(x),  add = NULL)assert_file_exists(  x,  access = "",  extension = NULL,  .var.name = vname(x),  add = NULL)testFileExists(x, access = "", extension = NULL)test_file_exists(x, access = "", extension = NULL)expect_file_exists(  x,  access = "",  extension = NULL,  info = NULL,  label = vname(x))checkFile(x, access = "", extension = NULL)assertFile(x, access = "", extension = NULL, .var.name = vname(x), add = NULL)assert_file(x, access = "", extension = NULL, .var.name = vname(x), add = NULL)testFile(x, access = "", extension = NULL)expect_file(x, access = "", extension = NULL, info = NULL, label = vname(x))

Arguments

x

[any]
Object to check.

access

[character(1)]
Single string containing possible characters ‘r’, ‘w’ and ‘x’ toforce a check for read, write or execute access rights, respectively.Write and executable rights are not checked on Windows.

extension

[character]
Vector of allowed file extensions, matched case insensitive.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertFileExists/assert_file_exists returnx invisibly, whereascheckFileExists/check_file_exists andtestFileExists/test_file_exists returnTRUE.If the check is not successful,assertFileExists/assert_file_existsthrows an error message,testFileExists/test_file_existsreturnsFALSE,andcheckFileExists/check_file_exists return a string with the error message.The functionexpect_file_exists always returns anexpectation.

Note

The functions without the suffix “exists” are deprecated and will be removedfrom the package in a future version due to name clashes.test_file has been unexported already.

See Also

Other filesystem:checkAccess(),checkDirectoryExists(),checkPathForOutput()

Examples

# Check if R's COPYING file is readabletestFileExists(file.path(R.home(), "COPYING"), access = "r")# Check if R's COPYING file is readable and writabletestFileExists(file.path(R.home(), "COPYING"), access = "rw")

Check if an argument is a flag

Description

A flag is defined as single logical value.

Usage

checkFlag(x, na.ok = FALSE, null.ok = FALSE)check_flag(x, na.ok = FALSE, null.ok = FALSE)assertFlag(x, na.ok = FALSE, null.ok = FALSE, .var.name = vname(x), add = NULL)assert_flag(  x,  na.ok = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testFlag(x, na.ok = FALSE, null.ok = FALSE)test_flag(x, na.ok = FALSE, null.ok = FALSE)expect_flag(x, na.ok = FALSE, null.ok = FALSE, info = NULL, label = vname(x))

Arguments

x

[any]
Object to check.

na.ok

[logical(1)]
Are missing values allowed? Default isFALSE.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertFlag/assert_flag returnx invisibly, whereascheckFlag/check_flag andtestFlag/test_flag returnTRUE.If the check is not successful,assertFlag/assert_flagthrows an error message,testFlag/test_flagreturnsFALSE,andcheckFlag/check_flag return a string with the error message.The functionexpect_flag always returns anexpectation.

See Also

Other scalars:checkCount(),checkInt(),checkNumber(),checkScalar(),checkScalarNA(),checkString()

Examples

testFlag(TRUE)testFlag(1)

Check if an argument is a formula

Description

Check if an argument is a formula

Usage

checkFormula(x, null.ok = FALSE)check_formula(x, null.ok = FALSE)assertFormula(x, null.ok = FALSE, .var.name = vname(x), add = NULL)assert_formula(x, null.ok = FALSE, .var.name = vname(x), add = NULL)testFormula(x, null.ok = FALSE)test_formula(x, null.ok = FALSE)expect_formula(x, null.ok = FALSE, info = NULL, label = vname(x))

Arguments

x

[any]
Object to check.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertFormula/assert_formula returnx invisibly, whereascheckFormula/check_formula andtestFormula/test_formula returnTRUE.If the check is not successful,assertFormula/assert_formulathrows an error message,testFormula/test_formulareturnsFALSE,andcheckFormula/check_formula return a string with the error message.The functionexpect_formula always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

f = Species ~ Sepal.Length + Sepal.WidthcheckFormula(f)

Check if an argument is a function

Description

Check if an argument is a function

Usage

checkFunction(x, args = NULL, ordered = FALSE, nargs = NULL, null.ok = FALSE)check_function(x, args = NULL, ordered = FALSE, nargs = NULL, null.ok = FALSE)assertFunction(  x,  args = NULL,  ordered = FALSE,  nargs = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_function(  x,  args = NULL,  ordered = FALSE,  nargs = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testFunction(x, args = NULL, ordered = FALSE, nargs = NULL, null.ok = FALSE)test_function(x, args = NULL, ordered = FALSE, nargs = NULL, null.ok = FALSE)expect_function(  x,  args = NULL,  ordered = FALSE,  nargs = NULL,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

args

[character]
Expected formal arguments. Checks that a function has no arguments ifset tocharacter(0).Default isNULL (no check).

ordered

[logical(1)]
Flag whether the arguments provided inargs must be the firstlength(args) arguments of the function in the specified order.Default isFALSE.

nargs

[integer(1)]
Required number of arguments, without....Default isNULL (no check).

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertFunction/assert_function returnx invisibly, whereascheckFunction/check_function andtestFunction/test_function returnTRUE.If the check is not successful,assertFunction/assert_functionthrows an error message,testFunction/test_functionreturnsFALSE,andcheckFunction/check_function return a string with the error message.The functionexpect_function always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

testFunction(mean)testFunction(mean, args = "x")

Check if an argument is a single integerish value

Description

Check if an argument is a single integerish value

Usage

checkInt(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE)check_int(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE)assertInt(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE,  coerce = FALSE,  .var.name = vname(x),  add = NULL)assert_int(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE,  coerce = FALSE,  .var.name = vname(x),  add = NULL)testInt(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE)test_int(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE)expect_int(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  tol = sqrt(.Machine$double.eps),  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

na.ok

[logical(1)]
Are missing values allowed? Default isFALSE.

lower

[numeric(1)]
Lower value all elements ofx must be greater than or equal to.

upper

[numeric(1)]
Upper value all elements ofx must be lower than or equal to.

tol

[double(1)]
Numerical tolerance used to check whether a double or complex can be converted.Default issqrt(.Machine$double.eps).

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

coerce

[logical(1)]
IfTRUE, the inputx is returned as integer after an successful assertion.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertInt/assert_int returnx invisibly, whereascheckInt/check_int andtestInt/test_int returnTRUE.If the check is not successful,assertInt/assert_intthrows an error message,testInt/test_intreturnsFALSE,andcheckInt/check_int return a string with the error message.The functionexpect_int always returns anexpectation.

Note

To perform an assertion and then convert to integer, useasInt.assertInt will not convert numerics to integer.

See Also

Other scalars:checkCount(),checkFlag(),checkNumber(),checkScalar(),checkScalarNA(),checkString()

Examples

testInt(1)testInt(-1, lower = 0)

Check if an argument is vector of type integer

Description

Check if an argument is vector of type integer

Usage

checkInteger(  x,  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)check_integer(  x,  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)assertInteger(  x,  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_integer(  x,  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testInteger(  x,  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)test_integer(  x,  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)expect_integer(  x,  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

lower

[numeric(1)]
Lower value all elements ofx must be greater than or equal to.

upper

[numeric(1)]
Upper value all elements ofx must be lower than or equal to.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

sorted

[logical(1)]
Elements must be sorted in ascending order. Missing values are ignored.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

typed.missing

[logical(1)]
If set toFALSE (default), all types of missing values (NA,NA_integer_,NA_real_,NA_character_ orNA_character_) as well as empty vectors are allowedwhile type-checking atomic input.Set toTRUE to enable strict type checking.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertInteger/assert_integer returnx invisibly, whereascheckInteger/check_integer andtestInteger/test_integer returnTRUE.If the check is not successful,assertInteger/assert_integerthrows an error message,testInteger/test_integerreturnsFALSE,andcheckInteger/check_integer return a string with the error message.The functionexpect_integer always returns anexpectation.

See Also

asInteger

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

testInteger(1L)testInteger(1.)testInteger(1:2, lower = 1, upper = 2, any.missing = FALSE)

Check if an object is an integerish vector

Description

An integerish value is defined as value safely convertible to integer.This includes integers and numeric values which sufficiently close to aninteger w.r.t. a numeric tolerance 'tol'.

Usage

checkIntegerish(  x,  tol = sqrt(.Machine$double.eps),  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)check_integerish(  x,  tol = sqrt(.Machine$double.eps),  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)assertIntegerish(  x,  tol = sqrt(.Machine$double.eps),  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  coerce = FALSE,  .var.name = vname(x),  add = NULL)assert_integerish(  x,  tol = sqrt(.Machine$double.eps),  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  coerce = FALSE,  .var.name = vname(x),  add = NULL)testIntegerish(  x,  tol = sqrt(.Machine$double.eps),  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)test_integerish(  x,  tol = sqrt(.Machine$double.eps),  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)expect_integerish(  x,  tol = sqrt(.Machine$double.eps),  lower = -Inf,  upper = Inf,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

tol

[double(1)]
Numerical tolerance used to check whether a double or complex can be converted.Default issqrt(.Machine$double.eps).

lower

[numeric(1)]
Lower value all elements ofx must be greater than or equal to.

upper

[numeric(1)]
Upper value all elements ofx must be lower than or equal to.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

sorted

[logical(1)]
Elements must be sorted in ascending order. Missing values are ignored.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

typed.missing

[logical(1)]
If set toFALSE (default), all types of missing values (NA,NA_integer_,NA_real_,NA_character_ orNA_character_) as well as empty vectors are allowedwhile type-checking atomic input.Set toTRUE to enable strict type checking.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

coerce

[logical(1)]
IfTRUE, the inputx is returned as integer after an successful assertion.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertIntegerish/assert_integerish returnx invisibly, whereascheckIntegerish/check_integerish andtestIntegerish/test_integerish returnTRUE.If the check is not successful,assertIntegerish/assert_integerishthrows an error message,testIntegerish/test_integerishreturnsFALSE,andcheckIntegerish/check_integerish return a string with the error message.The functionexpect_integerish always returns anexpectation.

Note

To convert from integerish to integer, useasInteger.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

testIntegerish(1L)testIntegerish(1.)testIntegerish(1:2, lower = 1L, upper = 2L, any.missing = FALSE)

Check if an argument is a list

Description

Check if an argument is a list

Usage

checkList(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)check_list(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)assertList(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_list(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testList(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)test_list(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)expect_list(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

types

[character]
Character vector of class names. Each list element must inheritfrom at least one of the provided types.The types “logical”, “integer”, “integerish”, “double”,“numeric”, “complex”, “character”, “factor”, “atomic”, “vector”“atomicvector”, “array”, “matrix”, “list”, “function”,“environment” and “null” are supported.For other typesinherits is used as a fallback to checkx's inheritance.Defaults tocharacter(0) (no check).

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertList/assert_list returnx invisibly, whereascheckList/check_list andtestList/test_list returnTRUE.If the check is not successful,assertList/assert_listthrows an error message,testList/test_listreturnsFALSE,andcheckList/check_list return a string with the error message.The functionexpect_list always returns anexpectation.

Note

Contrary to R'sis.list, objects of typedata.frameandpairlist are not recognized as list.

Missingness is defined here as elements of the list beingNULL, analogously toanyMissing.

The test for uniqueness does differentiate between the different NA types which are built-in in R.This is required to be consistent withunique while checkingscalar missing values. Also see the example.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

testList(list())testList(as.list(iris), types = c("numeric", "factor"))# MissingnesstestList(list(1, NA), any.missing = FALSE)testList(list(1, NULL), any.missing = FALSE)# Uniqueness differentiates between different NA types:testList(list(NA, NA), unique = TRUE)testList(list(NA, NA_real_), unique = TRUE)

Check if an argument is a vector of type logical

Description

Check if an argument is a vector of type logical

Usage

checkLogical(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)check_logical(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)assertLogical(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_logical(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testLogical(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)test_logical(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)expect_logical(  x,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

typed.missing

[logical(1)]
If set toFALSE (default), all types of missing values (NA,NA_integer_,NA_real_,NA_character_ orNA_character_) as well as empty vectors are allowedwhile type-checking atomic input.Set toTRUE to enable strict type checking.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertLogical/assert_logical returnx invisibly, whereascheckLogical/check_logical andtestLogical/test_logical returnTRUE.If the check is not successful,assertLogical/assert_logicalthrows an error message,testLogical/test_logicalreturnsFALSE,andcheckLogical/check_logical return a string with the error message.The functionexpect_logical always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

testLogical(TRUE)testLogical(TRUE, min.len = 1)

Check if an argument is a matrix

Description

Check if an argument is a matrix

Usage

checkMatrix(  x,  mode = NULL,  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)check_matrix(  x,  mode = NULL,  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)assertMatrix(  x,  mode = NULL,  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_matrix(  x,  mode = NULL,  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testMatrix(  x,  mode = NULL,  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)test_matrix(  x,  mode = NULL,  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)expect_matrix(  x,  mode = NULL,  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

mode

[character(1)]
Storage mode of the array. Arrays can hold vectors, i.e. “logical”,“integer”, “integerish”, “double”, “numeric”, “complex”,“character” and “list”. You can also specify “atomic”here to explicitly prohibit lists. Default isNULL (no check).If all values ofx are missing, this check is skipped.

any.missing

[logical(1)]
Are missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are matrices with only missing values allowed? Default isTRUE.

min.rows

[integer(1)]
Minimum number of rows.

max.rows

[integer(1)]
Maximum number of rows.

min.cols

[integer(1)]
Minimum number of columns.

max.cols

[integer(1)]
Maximum number of columns.

nrows

[integer(1)]
Exact number of rows.

ncols

[integer(1)]
Exact number of columns.

row.names

[character(1)]
Check for row names. Default is “NULL” (no check).SeecheckNamed for possible values.Note that you can usecheckSubset to check for a specific set of names.

col.names

[character(1)]
Check for column names. Default is “NULL” (no check).SeecheckNamed for possible values.Note that you can usecheckSubset to test for a specific set of names.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertMatrix/assert_matrix returnx invisibly, whereascheckMatrix/check_matrix andtestMatrix/test_matrix returnTRUE.If the check is not successful,assertMatrix/assert_matrixthrows an error message,testMatrix/test_matrixreturnsFALSE,andcheckMatrix/check_matrix return a string with the error message.The functionexpect_matrix always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Other compound:checkArray(),checkDataFrame(),checkDataTable(),checkTibble()

Examples

x = matrix(1:9, 3)colnames(x) = letters[1:3]testMatrix(x, nrows = 3, min.cols = 1, col.names = "named")

Check the class membership of an argument

Description

Check the class membership of an argument

Usage

checkMultiClass(x, classes, null.ok = FALSE)check_multi_class(x, classes, null.ok = FALSE)assertMultiClass(x, classes, null.ok = FALSE, .var.name = vname(x), add = NULL)assert_multi_class(  x,  classes,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testMultiClass(x, classes, null.ok = FALSE)test_multi_class(x, classes, null.ok = FALSE)expect_multi_class(x, classes, null.ok = FALSE, info = NULL, label = vname(x))

Arguments

x

[any]
Object to check.

classes

[character]
Class names to check for inheritance withinherits.x must inherit from any of the specified classes.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertMultiClass/assert_multi_class returnx invisibly, whereascheckMultiClass/check_multi_class andtestMultiClass/test_multi_class returnTRUE.If the check is not successful,assertMultiClass/assert_multi_classthrows an error message,testMultiClass/test_multi_classreturnsFALSE,andcheckMultiClass/check_multi_class return a string with the error message.The functionexpect_multi_class always returns anexpectation.

See Also

Other attributes:checkClass(),checkNamed(),checkNames()

Other classes:checkClass(),checkR6()

Examples

x = 1class(x) = "bar"checkMultiClass(x, c("foo", "bar"))checkMultiClass(x, c("foo", "foobar"))

Check if an argument is named

Description

Check if an argument is named

Usage

checkNamed(x, type = "named")check_named(x, type = "named")assertNamed(x, type = "named", .var.name = vname(x), add = NULL)assert_named(x, type = "named", .var.name = vname(x), add = NULL)testNamed(x, type = "named")test_named(x, type = "named")

Arguments

x

[any]
Object to check.

type

[character(1)]
Select the check(s) to perform.“unnamed” checksx to be unnamed.“named” (default) checksx to be named which excludes names to beNA or empty ("").“unique” additionally tests for non-duplicated names.“strict” checks for unique names which comply to R's variable name restrictions.Note that for zero-lengthx every name check evaluates toTRUE.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

Value

Depending on the function prefix:If the check is successful, the functionsassertNamed/assert_named returnx invisibly, whereascheckNamed/check_named andtestNamed/test_named returnTRUE.If the check is not successful,assertNamed/assert_namedthrows an error message,testNamed/test_namedreturnsFALSE,andcheckNamed/check_named return a string with the error message.The functionexpect_named always returns anexpectation.

Note

These function are deprecated and will be removed in a future version.Please usecheckNames instead.

See Also

Other attributes:checkClass(),checkMultiClass(),checkNames()

Examples

x = 1:3testNamed(x, "unnamed")names(x) = letters[1:3]testNamed(x, "unique")

Check names to comply to specific rules

Description

Performs various checks on character vectors, usually names.

Usage

checkNames(  x,  type = "named",  subset.of = NULL,  must.include = NULL,  permutation.of = NULL,  identical.to = NULL,  disjunct.from = NULL,  what = "names")check_names(  x,  type = "named",  subset.of = NULL,  must.include = NULL,  permutation.of = NULL,  identical.to = NULL,  disjunct.from = NULL,  what = "names")assertNames(  x,  type = "named",  subset.of = NULL,  must.include = NULL,  permutation.of = NULL,  identical.to = NULL,  disjunct.from = NULL,  what = "names",  .var.name = vname(x),  add = NULL)assert_names(  x,  type = "named",  subset.of = NULL,  must.include = NULL,  permutation.of = NULL,  identical.to = NULL,  disjunct.from = NULL,  what = "names",  .var.name = vname(x),  add = NULL)testNames(  x,  type = "named",  subset.of = NULL,  must.include = NULL,  permutation.of = NULL,  identical.to = NULL,  disjunct.from = NULL,  what = "names")test_names(  x,  type = "named",  subset.of = NULL,  must.include = NULL,  permutation.of = NULL,  identical.to = NULL,  disjunct.from = NULL,  what = "names")expect_names(  x,  type = "named",  subset.of = NULL,  must.include = NULL,  permutation.of = NULL,  identical.to = NULL,  disjunct.from = NULL,  what = "names",  info = NULL,  label = vname(x))

Arguments

x

[character ||NULL]
Names to check using rules defined viatype.

type

[character(1)]
Type of formal check(s) to perform on the names.

unnamed:

Checksx to beNULL.

named:

Checksx for regular names which excludes names to beNA or empty ("").

unique:

Performs checks like with “named” and additionally tests for non-duplicated names.

strict:

Performs checks like with “unique” and additionally fails for names with UTF-8 characters and names which do not comply to R's variable name restrictions.As regular expression, this is “^[.]*[a-zA-Z]+[a-zA-Z0-9._]*$”.

ids:

Same as “strict”, but does not enforce uniqueness.

Note that for zero-lengthx, all these name checks evaluate toTRUE.

subset.of

[character]
Names provided inx must be subset of the setsubset.of.

must.include

[character]
Names provided inx must be a superset of the setmust.include.

permutation.of

[character]
Names provided inx must be a permutation of the setpermutation.of.Duplicated names inpermutation.of are stripped out and duplicated names inxthus lead to a failed check.Use this argument instead ofidentical.to if the order of the names is not relevant.

identical.to

[character]
Names provided inx must be identical to the vectoridentical.to.Use this argument instead ofpermutation.of if the order of the names is relevant.

disjunct.from

[character]
Names provided inx must may not be present in the vectordisjunct.from.

what

[character(1)]
Type of name vector to check, e.g. “names” (default), “colnames” or “rownames”.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertNames/assert_names returnx invisibly, whereascheckNames/check_names andtestNames/test_names returnTRUE.If the check is not successful,assertNames/assert_namesthrows an error message,testNames/test_namesreturnsFALSE,andcheckNames/check_names return a string with the error message.The functionexpect_names always returns anexpectation.

See Also

Other attributes:checkClass(),checkMultiClass(),checkNamed()

Examples

x = 1:3testNames(names(x), "unnamed")names(x) = letters[1:3]testNames(names(x), "unique")cn = c("Species", "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")assertNames(names(iris), permutation.of = cn)

Check if an argument is NULL

Description

Check if an argument is NULL

Usage

checkNull(x)check_null(x)assertNull(x, .var.name = vname(x), add = NULL)assert_null(x, .var.name = vname(x), add = NULL)testNull(x)test_null(x)

Arguments

x

[any]
Object to check.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

Value

Depending on the function prefix:If the check is successful, the functionsassertNull/assert_null returnx invisibly, whereascheckNull/check_null andtestNull/test_null returnTRUE.If the check is not successful,assertNull/assert_nullthrows an error message,testNull/test_nullreturnsFALSE,andcheckNull/check_null return a string with the error message.The functionexpect_null always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNumeric(),checkPOSIXct(),checkRaw(),checkVector()

Examples

testNull(NULL)testNull(1)

Check if an argument is a single numeric value

Description

Check if an argument is a single numeric value

Usage

checkNumber(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  finite = FALSE,  null.ok = FALSE)check_number(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  finite = FALSE,  null.ok = FALSE)assertNumber(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  finite = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_number(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  finite = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testNumber(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  finite = FALSE,  null.ok = FALSE)test_number(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  finite = FALSE,  null.ok = FALSE)expect_number(  x,  na.ok = FALSE,  lower = -Inf,  upper = Inf,  finite = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

na.ok

[logical(1)]
Are missing values allowed? Default isFALSE.

lower

[numeric(1)]
Lower value all elements ofx must be greater than or equal to.

upper

[numeric(1)]
Upper value all elements ofx must be lower than or equal to.

finite

[logical(1)]
Check for only finite values? Default isFALSE.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertNumber/assert_number returnx invisibly, whereascheckNumber/check_number andtestNumber/test_number returnTRUE.If the check is not successful,assertNumber/assert_numberthrows an error message,testNumber/test_numberreturnsFALSE,andcheckNumber/check_number return a string with the error message.The functionexpect_number always returns anexpectation.

See Also

Other scalars:checkCount(),checkFlag(),checkInt(),checkScalar(),checkScalarNA(),checkString()

Examples

testNumber(1)testNumber(1:2)

Check that an argument is a vector of type numeric

Description

Vectors of storage type “integer” and “double” count as “numeric”, c.f.is.numeric.To explicitly check for real integer or double vectors, seecheckInteger,checkIntegerish orcheckDouble.

Usage

checkNumeric(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)check_numeric(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)assertNumeric(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_numeric(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testNumeric(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)test_numeric(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE)expect_numeric(  x,  lower = -Inf,  upper = Inf,  finite = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  names = NULL,  typed.missing = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

lower

[numeric(1)]
Lower value all elements ofx must be greater than or equal to.

upper

[numeric(1)]
Upper value all elements ofx must be lower than or equal to.

finite

[logical(1)]
Check for only finite values? Default isFALSE.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

sorted

[logical(1)]
Elements must be sorted in ascending order. Missing values are ignored.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

typed.missing

[logical(1)]
If set toFALSE (default), all types of missing values (NA,NA_integer_,NA_real_,NA_character_ orNA_character_) as well as empty vectors are allowedwhile type-checking atomic input.Set toTRUE to enable strict type checking.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertNumeric/assert_numeric returnx invisibly, whereascheckNumeric/check_numeric andtestNumeric/test_numeric returnTRUE.If the check is not successful,assertNumeric/assert_numericthrows an error message,testNumeric/test_numericreturnsFALSE,andcheckNumeric/check_numeric return a string with the error message.The functionexpect_numeric always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkPOSIXct(),checkRaw(),checkVector()

Examples

testNumeric(1)testNumeric(1, min.len = 1, lower = 0)

Check the operating system

Description

Check the operating system

Usage

checkOS(os)check_os(os)assertOS(os, add = NULL, .var.name = NULL)assert_os(os, add = NULL, .var.name = NULL)testOS(os)test_os(os)expect_os(os, info = NULL, label = NULL)

Arguments

os

[character]
Check the operating system to be in a set with possible elements “windows”,“mac”, “linux” and “solaris”.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertOS/assert_os returnx invisibly, whereascheckOS/check_os andtestOS/test_os returnTRUE.If the check is not successful,assertOS/assert_osthrows an error message,testOS/test_osreturnsFALSE,andcheckOS/check_os return a string with the error message.The functionexpect_os always returns anexpectation.

Examples

testOS("linux")

Check that an argument is a date/time object in POSIXct format

Description

Checks that an object is of classPOSIXct.

Usage

checkPOSIXct(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  null.ok = FALSE)check_posixct(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  null.ok = FALSE)assertPOSIXct(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_posixct(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testPOSIXct(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  null.ok = FALSE)test_posixct(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  null.ok = FALSE)expect_posixct(  x,  lower = NULL,  upper = NULL,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  sorted = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

lower

[Date]
All non-missing dates inx must be >= this POSIXct time. Must be provided in the same timezone asx.

upper

[Date]
All non-missing dates inx must be <= this POSIXct time. Must be provided in the same timezone asx.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

sorted

[logical(1)]
Elements must be sorted in ascending order. Missing values are ignored.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertAtomic/assert_atomic returnx invisibly, whereascheckAtomic/check_atomic andtestAtomic/test_atomic returnTRUE.If the check is not successful,assertAtomic/assert_atomicthrows an error message,testAtomic/test_atomicreturnsFALSE,andcheckAtomic/check_atomic return a string with the error message.The functionexpect_atomic always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkRaw(),checkVector()


Check if a path is suited for creating an output file

Description

Check if a file path can be used safely to create a file and write to it.

This is checked:

Paths are relative to the current working directory.

Usage

checkPathForOutput(x, overwrite = FALSE, extension = NULL)check_path_for_output(x, overwrite = FALSE, extension = NULL)assertPathForOutput(  x,  overwrite = FALSE,  extension = NULL,  .var.name = vname(x),  add = NULL)assert_path_for_output(  x,  overwrite = FALSE,  extension = NULL,  .var.name = vname(x),  add = NULL)testPathForOutput(x, overwrite = FALSE, extension = NULL)test_path_for_output(x, overwrite = FALSE, extension = NULL)expect_path_for_output(  x,  overwrite = FALSE,  extension = NULL,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

overwrite

[logical(1)]
IfTRUE, an existing file in place is allowed if itit is both readable and writable.Default isFALSE.

extension

[character(1)]
Extension of the file, e.g. “txt” or “tar.gz”.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertPathForOutput/assert_path_for_output returnx invisibly, whereascheckPathForOutput/check_path_for_output andtestPathForOutput/test_path_for_output returnTRUE.If the check is not successful,assertPathForOutput/assert_path_for_outputthrows an error message,testPathForOutput/test_path_for_outputreturnsFALSE,andcheckPathForOutput/check_path_for_output return a string with the error message.The functionexpect_path_for_output always returns anexpectation.

See Also

Other filesystem:checkAccess(),checkDirectoryExists(),checkFileExists()

Examples

# Can we create a file in the tempdir?testPathForOutput(file.path(tempdir(), "process.log"))

Check if the arguments are permutations of each other.

Description

In contrast tocheckSetEqual, the function tests for a truepermutation of the two vectors and also considers duplicated values.Missing values are being treated as actual values by default.Does not work on raw values.

Usage

checkPermutation(x, y, na.ok = TRUE)check_permutation(x, y, na.ok = TRUE)assertPermutation(x, y, na.ok = TRUE, .var.name = vname(x), add = NULL)assert_permutation(x, y, na.ok = TRUE, .var.name = vname(x), add = NULL)testPermutation(x, y, na.ok = TRUE)test_permutation(x, y, na.ok = TRUE)expect_permutation(x, y, na.ok = TRUE, info = NULL, label = vname(x))

Arguments

x

[any]
Object to check.

y

[atomic]
Vector to compare with. Atomic vector of type other than raw.

na.ok

[logical(1)]
Are missing values allowed? Default isTRUE.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertPermutation/assert_permutation returnx invisibly, whereascheckPermutation/check_permutation andtestPermutation/test_permutation returnTRUE.If the check is not successful,assertPermutation/assert_permutationthrows an error message,testPermutation/test_permutationreturnsFALSE,andcheckPermutation/check_permutation return a string with the error message.The functionexpect_permutation always returns anexpectation.

Note

The objectx must be of the same type as the set w.r.t.typeof.Integers and doubles are both treated as numeric.

See Also

Other set:checkChoice(),checkDisjunct(),checkSetEqual(),checkSubset()

Examples

testPermutation(letters[1:2], letters[2:1])testPermutation(letters[c(1, 1, 2)], letters[1:2])testPermutation(c(NA, 1, 2), c(1, 2, NA))testPermutation(c(NA, 1, 2), c(1, 2, NA), na.ok = FALSE)

Check if an argument is an R6 class

Description

Check if an argument is an R6 class

Usage

checkR6(  x,  classes = NULL,  ordered = FALSE,  cloneable = NULL,  public = NULL,  private = NULL,  null.ok = FALSE)check_r6(  x,  classes = NULL,  ordered = FALSE,  cloneable = NULL,  public = NULL,  private = NULL,  null.ok = FALSE)assertR6(  x,  classes = NULL,  ordered = FALSE,  cloneable = NULL,  public = NULL,  private = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_r6(  x,  classes = NULL,  ordered = FALSE,  cloneable = NULL,  public = NULL,  private = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testR6(  x,  classes = NULL,  ordered = FALSE,  cloneable = NULL,  public = NULL,  private = NULL,  null.ok = FALSE)test_r6(  x,  classes = NULL,  ordered = FALSE,  cloneable = NULL,  public = NULL,  private = NULL,  null.ok = FALSE)expect_r6(  x,  classes = NULL,  ordered = FALSE,  cloneable = NULL,  public = NULL,  private = NULL,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

classes

[character]
Class names to check for inheritance withinherits.x must inherit from all specified classes.

ordered

[logical(1)]
Expectx to be specialized in provided order.Default isFALSE.

cloneable

[logical(1)]
IfTRUE, check thatx has aclone method. IfFALSE, ensure thatx is not cloneable.

public

[character]
Names of expected public slots. This includes active bindings.

private

[character]
Names of expected private slots.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertClass/assert_class returnx invisibly, whereascheckClass/check_class andtestClass/test_class returnTRUE.If the check is not successful,assertClass/assert_classthrows an error message,testClass/test_classreturnsFALSE,andcheckClass/check_class return a string with the error message.The functionexpect_class always returns anexpectation.

See Also

Other classes:checkClass(),checkMultiClass()

Examples

library(R6)generator = R6Class("Bar",  public = list(a = 5),  private = list(b = 42),  active = list(c = function() 99))x = generator$new()checkR6(x, "Bar", cloneable = TRUE, public = "a")

Check if an argument is a raw vector

Description

Check if an argument is a raw vector

Usage

checkRaw(  x,  len = NULL,  min.len = NULL,  max.len = NULL,  names = NULL,  null.ok = FALSE)check_raw(  x,  len = NULL,  min.len = NULL,  max.len = NULL,  names = NULL,  null.ok = FALSE)assertRaw(  x,  len = NULL,  min.len = NULL,  max.len = NULL,  names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_raw(  x,  len = NULL,  min.len = NULL,  max.len = NULL,  names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testRaw(  x,  len = NULL,  min.len = NULL,  max.len = NULL,  names = NULL,  null.ok = FALSE)test_raw(  x,  len = NULL,  min.len = NULL,  max.len = NULL,  names = NULL,  null.ok = FALSE)expect_raw(  x,  len = NULL,  min.len = NULL,  max.len = NULL,  names = NULL,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertRaw/assert_raw returnx invisibly, whereascheckRaw/check_raw andtestRaw/test_raw returnTRUE.If the check is not successful,assertRaw/assert_rawthrows an error message,testRaw/test_rawreturnsFALSE,andcheckRaw/check_raw return a string with the error message.The functionexpect_raw always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkVector()

Examples

testRaw(as.raw(2), min.len = 1L)

Check if an argument is a single atomic value

Description

Check if an argument is a single atomic value

Usage

checkScalar(x, na.ok = FALSE, null.ok = FALSE)check_scalar(x, na.ok = FALSE, null.ok = FALSE)assertScalar(  x,  na.ok = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_scalar(  x,  na.ok = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testScalar(x, na.ok = FALSE, null.ok = FALSE)test_scalar(x, na.ok = FALSE, null.ok = FALSE)expect_scalar(x, na.ok = FALSE, null.ok = FALSE, info = NULL, label = vname(x))

Arguments

x

[any]
Object to check.

na.ok

[logical(1)]
Are missing values allowed? Default isFALSE.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertScalar/assert_scalar returnx invisibly, whereascheckScalar/check_scalar andtestScalar/test_scalar returnTRUE.If the check is not successful,assertScalar/assert_scalarthrows an error message,testScalar/test_scalarreturnsFALSE,andcheckScalar/check_scalar return a string with the error message.The functionexpect_scalar always returns anexpectation.

See Also

Other scalars:checkCount(),checkFlag(),checkInt(),checkNumber(),checkScalarNA(),checkString()

Examples

testScalar(1)testScalar(1:10)

Check if an argument is a single missing value

Description

Check if an argument is a single missing value

Usage

checkScalarNA(x, null.ok = FALSE)check_scalar_na(x, null.ok = FALSE)assertScalarNA(x, null.ok = FALSE, .var.name = vname(x), add = NULL)assert_scalar_na(x, null.ok = FALSE, .var.name = vname(x), add = NULL)testScalarNA(x, null.ok = FALSE)test_scalar_na(x, null.ok = FALSE)expect_scalar_na(x, null.ok = FALSE, info = NULL, label = vname(x))

Arguments

x

[any]
Object to check.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertScalarNA/assert_scalar_na returnx invisibly, whereascheckScalarNA/check_scalar_na andtestScalarNA/test_scalar_na returnTRUE.If the check is not successful,assertScalarNA/assert_scalar_nathrows an error message,testScalarNA/test_scalar_nareturnsFALSE,andcheckScalarNA/check_scalar_na return a string with the error message.The functionexpect_scalar_na always returns anexpectation.

See Also

Other scalars:checkCount(),checkFlag(),checkInt(),checkNumber(),checkScalar(),checkString()

Examples

testScalarNA(1)testScalarNA(NA_real_)testScalarNA(rep(NA, 2))

Check if an argument is equal to a given set

Description

Check if an argument is equal to a given set

Usage

checkSetEqual(x, y, ordered = FALSE, fmatch = FALSE)check_set_equal(x, y, ordered = FALSE, fmatch = FALSE)assertSetEqual(  x,  y,  ordered = FALSE,  fmatch = FALSE,  .var.name = vname(x),  add = NULL)assert_set_equal(  x,  y,  ordered = FALSE,  fmatch = FALSE,  .var.name = vname(x),  add = NULL)testSetEqual(x, y, ordered = FALSE, fmatch = FALSE)test_set_equal(x, y, ordered = FALSE, fmatch = FALSE)expect_set_equal(  x,  y,  ordered = FALSE,  fmatch = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

y

[atomic]
Set to compare with.

ordered

[logical(1)]
Checkx to have the same length and order asy, i.e.check using “==” while handlingNAs nicely.Default isFALSE.

fmatch

[logical(1)]
Use the set operations implemented infmatch in packagefastmatch.Iffastmatch is not installed, this silently falls back tomatch.fmatch modifiesy by reference:A hash table is added as attribute which is used in subsequent calls.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertSubset/assert_subset returnx invisibly, whereascheckSubset/check_subset andtestSubset/test_subset returnTRUE.If the check is not successful,assertSubset/assert_subsetthrows an error message,testSubset/test_subsetreturnsFALSE,andcheckSubset/check_subset return a string with the error message.The functionexpect_subset always returns anexpectation.

Note

The objectx must be of the same type as the set w.r.t.typeof.Integers and doubles are both treated as numeric.

See Also

Other set:checkChoice(),checkDisjunct(),checkPermutation(),checkSubset()

Examples

testSetEqual(c("a", "b"), c("a", "b"))testSetEqual(1:3, 1:4)# x is not converted before the comparison (except for numerics)testSetEqual(factor("a"), "a")testSetEqual(1, "1")testSetEqual(1, as.integer(1))

Check if an argument is a string

Description

A string is defined as a scalar character vector.To check for vectors of arbitrary length, seecheckCharacter.

Usage

checkString(  x,  na.ok = FALSE,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  null.ok = FALSE)check_string(  x,  na.ok = FALSE,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  null.ok = FALSE)assertString(  x,  na.ok = FALSE,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_string(  x,  na.ok = FALSE,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testString(  x,  na.ok = FALSE,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  null.ok = FALSE)test_string(  x,  na.ok = FALSE,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  null.ok = FALSE)expect_string(  x,  na.ok = FALSE,  n.chars = NULL,  min.chars = NULL,  max.chars = NULL,  pattern = NULL,  fixed = NULL,  ignore.case = FALSE,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

na.ok

[logical(1)]
Are missing values allowed? Default isFALSE.

n.chars

[integer(1)]
Exact number of characters for each element ofx.

min.chars

[integer(1)]
Minimum number of characters for each element ofx.

max.chars

[integer(1)]
Maximum number of characters for each element ofx.

pattern

[character(1)]
Regular expression as used ingrepl.All non-missing elements ofx must comply to this pattern.

fixed

[character(1)]
Substring to detect inx. Will be used aspattern ingreplwith optionfixed set toTRUE.All non-missing elements ofx must contain this substring.

ignore.case

[logical(1)]
Seegrepl. Default isFALSE.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

This function does not distinguish betweenNA,NA_integer_,NA_real_,NA_complex_NA_character_ andNaN.

Value

Depending on the function prefix:If the check is successful, the functionsassertString/assert_string returnx invisibly, whereascheckString/check_string andtestString/test_string returnTRUE.If the check is not successful,assertString/assert_stringthrows an error message,testString/test_stringreturnsFALSE,andcheckString/check_string return a string with the error message.The functionexpect_string always returns anexpectation.

See Also

Other scalars:checkCount(),checkFlag(),checkInt(),checkNumber(),checkScalar(),checkScalarNA()

Examples

testString("a")testString(letters)

Check if an argument is a subset of a given set

Description

Check if an argument is a subset of a given set

Usage

checkSubset(x, choices, empty.ok = TRUE, fmatch = FALSE)check_subset(x, choices, empty.ok = TRUE, fmatch = FALSE)assertSubset(  x,  choices,  empty.ok = TRUE,  fmatch = FALSE,  .var.name = vname(x),  add = NULL)assert_subset(  x,  choices,  empty.ok = TRUE,  fmatch = FALSE,  .var.name = vname(x),  add = NULL)testSubset(x, choices, empty.ok = TRUE, fmatch = FALSE)test_subset(x, choices, empty.ok = TRUE, fmatch = FALSE)expect_subset(  x,  choices,  empty.ok = TRUE,  fmatch = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

choices

[atomic]
Set of possible values. May be empty.

empty.ok

[logical(1)]
Treat zero-lengthx as subset of any setchoices (this includesNULL)?Default isTRUE.

fmatch

[logical(1)]
Use the set operations implemented infmatch in packagefastmatch.Iffastmatch is not installed, this silently falls back tomatch.fmatch modifiesy by reference:A hash table is added as attribute which is used in subsequent calls.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertSubset/assert_subset returnx invisibly, whereascheckSubset/check_subset andtestSubset/test_subset returnTRUE.If the check is not successful,assertSubset/assert_subsetthrows an error message,testSubset/test_subsetreturnsFALSE,andcheckSubset/check_subset return a string with the error message.The functionexpect_subset always returns anexpectation.

Note

The objectx must be of the same type as the set w.r.t.typeof.Integers and doubles are both treated as numeric.

See Also

Other set:checkChoice(),checkDisjunct(),checkPermutation(),checkSetEqual()

Examples

testSubset(c("a", "z"), letters)testSubset("ab", letters)testSubset("Species", names(iris))# x is not converted before the comparison (except for numerics)testSubset(factor("a"), "a")testSubset(1, "1")testSubset(1, as.integer(1))

Check if an argument is TRUE

Description

Simply checks if an argument isTRUE.

Usage

checkTRUE(x, na.ok = FALSE)check_true(x, na.ok = FALSE)assertTRUE(x, na.ok = FALSE, .var.name = vname(x), add = NULL)assert_true(x, na.ok = FALSE, .var.name = vname(x), add = NULL)testTRUE(x, na.ok = FALSE)test_true(x, na.ok = FALSE)

Arguments

x

[any]
Object to check.

na.ok

[logical(1)]
Are missing values allowed? Default isFALSE.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

Value

Depending on the function prefix:If the check is successful, the functionsassertTRUE./assert_true. returnx invisibly, whereascheckTRUE./check_true. andtestTRUE./test_true. returnTRUE.If the check is not successful,assertTRUE./assert_true.throws an error message,testTRUE./test_true.returnsFALSE,andcheckTRUE./check_true. return a string with the error message.The functionexpect_true. always returns anexpectation.

Examples

testTRUE(TRUE)testTRUE(FALSE)

Check if an argument is a tibble

Description

Check if an argument is a tibble

Usage

checkTibble(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)check_tibble(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)assertTibble(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_tibble(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testTibble(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)test_tibble(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE)expect_tibble(  x,  types = character(0L),  any.missing = TRUE,  all.missing = TRUE,  min.rows = NULL,  max.rows = NULL,  min.cols = NULL,  max.cols = NULL,  nrows = NULL,  ncols = NULL,  row.names = NULL,  col.names = NULL,  null.ok = FALSE,  info = NULL,  label = vname(x))

Arguments

x

[any]
Object to check.

types

[character]
Character vector of class names. Each list element must inheritfrom at least one of the provided types.The types “logical”, “integer”, “integerish”, “double”,“numeric”, “complex”, “character”, “factor”, “atomic”, “vector”“atomicvector”, “array”, “matrix”, “list”, “function”,“environment” and “null” are supported.For other typesinherits is used as a fallback to checkx's inheritance.Defaults tocharacter(0) (no check).

any.missing

[logical(1)]
Are missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are matrices with only missing values allowed? Default isTRUE.

min.rows

[integer(1)]
Minimum number of rows.

max.rows

[integer(1)]
Maximum number of rows.

min.cols

[integer(1)]
Minimum number of columns.

max.cols

[integer(1)]
Maximum number of columns.

nrows

[integer(1)]
Exact number of rows.

ncols

[integer(1)]
Exact number of columns.

row.names

[character(1)]
Check for row names. Default is “NULL” (no check).SeecheckNamed for possible values.Note that you can usecheckSubset to check for a specific set of names.

col.names

[character(1)]
Check for column names. Default is “NULL” (no check).SeecheckNamed for possible values.Note that you can usecheckSubset to test for a specific set of names.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Depending on the function prefix:If the check is successful, the functionsassertTibble/assert_tibble returnx invisibly, whereascheckTibble/check_tibble andtestTibble/test_tibble returnTRUE.If the check is not successful,assertTibble/assert_tibblethrows an error message,testTibble/test_tibblereturnsFALSE,andcheckTibble/check_tibble return a string with the error message.The functionexpect_tibble always returns anexpectation.

See Also

Other compound:checkArray(),checkDataFrame(),checkDataTable(),checkMatrix()

Examples

library(tibble)x = as_tibble(iris)testTibble(x)testTibble(x, nrow = 150, any.missing = FALSE)

Check if an argument is a vector

Description

Check if an argument is a vector

Usage

checkVector(  x,  strict = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)check_vector(  x,  strict = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)assertVector(  x,  strict = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)assert_vector(  x,  strict = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE,  .var.name = vname(x),  add = NULL)testVector(  x,  strict = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)test_vector(  x,  strict = FALSE,  any.missing = TRUE,  all.missing = TRUE,  len = NULL,  min.len = NULL,  max.len = NULL,  unique = FALSE,  names = NULL,  null.ok = FALSE)

Arguments

x

[any]
Object to check.

strict

[logical(1)]
May the vector have additional attributes? IfTRUE, mimics the behavior ofis.vector.Default isFALSE which allows e.g.factors ordata.framesto be recognized as vectors.

any.missing

[logical(1)]
Are vectors with missing values allowed? Default isTRUE.

all.missing

[logical(1)]
Are vectors with no non-missing values allowed? Default isTRUE.Note that empty vectors do not have non-missing values.

len

[integer(1)]
Exact expected length ofx.

min.len

[integer(1)]
Minimal length ofx.

max.len

[integer(1)]
Maximal length ofx.

unique

[logical(1)]
Must all values be unique? Default isFALSE.

names

[character(1)]
Check for names. SeecheckNamed for possible values.Default is “any” which performs no check at all.Note that you can usecheckSubset to check for a specific set of names.

null.ok

[logical(1)]
If set toTRUE,x may also beNULL.In this case only a type check ofx is performed, all additional checks are disabled.

.var.name

[character(1)]
Name of the checked object to print in assertions. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertion messages. SeeAssertCollection.

Value

Depending on the function prefix:If the check is successful, the functionsassertVector/assert_vector returnx invisibly, whereascheckVector/check_vector andtestVector/test_vector returnTRUE.If the check is not successful,assertVector/assert_vectorthrows an error message,testVector/test_vectorreturnsFALSE,andcheckVector/check_vector return a string with the error message.The functionexpect_vector always returns anexpectation.

See Also

Other basetypes:checkArray(),checkAtomic(),checkAtomicVector(),checkCharacter(),checkComplex(),checkDataFrame(),checkDate(),checkDouble(),checkEnvironment(),checkFactor(),checkFormula(),checkFunction(),checkInteger(),checkIntegerish(),checkList(),checkLogical(),checkMatrix(),checkNull(),checkNumeric(),checkPOSIXct(),checkRaw()

Other atomicvector:checkAtomic(),checkAtomicVector()

Examples

testVector(letters, min.len = 1L, any.missing = FALSE)

Turn a Check into an Assertion

Description

makeAssertion is the internal function used to evaluate the result of acheck and throw an exception if necessary.makeAssertionFunction can be used to automatically create an assertionfunction based on a check function (see example).

Usage

makeAssertion(x, res, var.name, collection)makeAssertionFunction(  check.fun,  c.fun = NULL,  use.namespace = TRUE,  coerce = FALSE,  env = parent.frame())

Arguments

x

[any]
Object to check.

res

[TRUE |character(1)]
The result of a check function:TRUE for successful checks,and an error message as string otherwise.

var.name

[character(1)]
The custom name forx as passed to anyassert* function.Defaults to a heuristic name lookup.

collection

[AssertCollection]
If anAssertCollection is provided, the error message is storedin it. IfNULL, an exception is raised ifres is notTRUE.

check.fun

[function]
Function which checks the input. Must returnTRUE on success and a string with the error message otherwise.

c.fun

[character(1)]
If notNULL, instead of calling the functioncheck.fun, use.Call to call a C function “c.fun” with the identicalset of parameters. The C function must be registered as a native symbol, see.Call.Useful ifcheck.fun is just a simple wrapper.

use.namespace

[logical(1)]
Call functions ofcheckmate using its namespace explicitly.Can be set toFALSE so save some microseconds, but the checkmate package needs to be imported.Default isTRUE.

coerce

[logical(1)]
IfTRUE, injects some lines of code to convert numeric values to integer after an successful assertion.Currently used inassertCount,assertInt andassertIntegerish.

env

[environment]
The environment of the created function. Default is theparent.frame.

Value

makeAssertion invisibly returns the checked object if the check was successful,and an exception is raised (or its message stored in the collection) otherwise.makeAssertionFunction returns afunction.

See Also

Other CustomConstructors:makeExpectation(),makeTest()

Examples

# Simple custom check functioncheckFalse = function(x) if (!identical(x, FALSE)) "Must be FALSE" else TRUE# Create the respective assert functionassertFalse = function(x, .var.name = vname(x), add = NULL) {  res = checkFalse(x)  makeAssertion(x, res, .var.name, add)}# Alternative: Automatically create such a functionassertFalse = makeAssertionFunction(checkFalse)print(assertFalse)

Turn a Check into an Expectation

Description

makeExpectation is the internal function used to evaluate the result of acheck and turn it into anexpectation.makeExceptionFunction can be used to automatically create an expectationfunction based on a check function (see example).

Usage

makeExpectation(x, res, info, label)makeExpectationFunction(  check.fun,  c.fun = NULL,  use.namespace = FALSE,  env = parent.frame())

Arguments

x

[any]
Object to check.

res

[TRUE |character(1)]
The result of a check function:TRUE for successful checks,and an error message as string otherwise.

info

[character(1)]
Seeexpect_that

label

[character(1)]
Seeexpect_that

check.fun

[function]
Function which checks the input. Must returnTRUE on success and a string with the error message otherwise.

c.fun

[character(1)]
If notNULL, instead of calling the functioncheck.fun, use.Call to call a C function “c.fun” with the identicalset of parameters. The C function must be registered as a native symbol, see.Call.Useful ifcheck.fun is just a simple wrapper.

use.namespace

[logical(1)]
Call functions ofcheckmate using its namespace explicitly.Can be set toFALSE so save some microseconds, but the checkmate package needs to be imported.Default isTRUE.

env

[environment]
The environment of the created function. Default is theparent.frame.

Value

makeExpectation invisibly returns the checked object.makeExpectationFunction returns afunction.

See Also

Other CustomConstructors:makeAssertion(),makeTest()

Examples

# Simple custom check functioncheckFalse = function(x) if (!identical(x, FALSE)) "Must be FALSE" else TRUE# Create the respective expect functionexpect_false = function(x, info = NULL, label = vname(x)) {  res = checkFalse(x)  makeExpectation(x, res, info = info, label = label)}# Alternative: Automatically create such a functionexpect_false = makeExpectationFunction(checkFalse)print(expect_false)

Turn a Check into a Test

Description

makeTest is the internal function used to evaluate the result of acheck and throw an exception if necessary.This function is currently only a stub and just callsisTRUE.makeTestFunction can be used to automatically create an assertionfunction based on a check function (see example).

Usage

makeTest(res)makeTestFunction(check.fun, c.fun = NULL, env = parent.frame())

Arguments

res

[TRUE |character(1)]
The result of a check function:TRUE for successful checks,and an error message as string otherwise.

check.fun

[function]
Function which checks the input. Must returnTRUE on success and a string with the error message otherwise.

c.fun

[character(1)]
If notNULL, instead of calling the functioncheck.fun, use.Call to call a C function “c.fun” with the identicalset of parameters. The C function must be registered as a native symbol, see.Call.Useful ifcheck.fun is just a simple wrapper.

env

[environment]
The environment of the created function. Default is theparent.frame.

Value

makeTest returnsTRUE if the check is successful andFALSE otherwise.makeTestFunction returns afunction.

See Also

Other CustomConstructors:makeAssertion(),makeExpectation()

Examples

# Simple custom check functioncheckFalse = function(x) if (!identical(x, FALSE)) "Must be FALSE" else TRUE# Create the respective test functiontestFalse = function(x) {  res = checkFalse(x)  makeTest(res)}# Alternative: Automatically create such a functiontestFalse = makeTestFunction(checkFalse)print(testFalse)

Partial Argument Matching

Description

This is an extensions tomatch.arg with support forAssertCollection.The behavior is very similar tomatch.arg, except thatNULL is nota valid value forx.

Usage

matchArg(x, choices, several.ok = FALSE, .var.name = vname(x), add = NULL)

Arguments

x

[character]
User provided argument to match.

choices

[character]
Candidates to matchx with.

several.ok

[logical(1)]
IfTRUE, multiple matches are allowed, cf.match.arg.

.var.name

[character(1)]
Name of the checked object to print in error messages. Defaults tothe heuristic implemented invname.

add

[AssertCollection]
Collection to store assertions. SeeAssertCollection.

Value

Subset ofchoices.

Examples

matchArg("k", choices = c("kendall", "pearson"))

Quick argument checks on (builtin) R types

Description

The provided functions parse rules which allow to express some of the mostfrequent argument checks by typing just a few letters.

Usage

qassert(x, rules, .var.name = vname(x))qtest(x, rules)qexpect(x, rules, info = NULL, label = vname(x))

Arguments

x

[any]
Object the check.

rules

[character]
Set of rules. See details.

.var.name

[character(1)]
Name of the checked object to print in error messages. Defaults tothe heuristic implemented invname.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Details

The rule is specified in up to three parts.

  1. Class and missingness check.The first letter is an abbreviation for the class. If it isprovided uppercase, missing values are prohibited.Supported abbreviations:

    [bB] Bool / logical.
    [iI] Integer.
    [xX] Integerish (numeric convertible to integer, seecheckIntegerish).
    [rR] Real / double.
    [cC] Complex.
    [nN] Numeric (integer or double).
    [sS] String / character.
    [fF] Factor
    [aA] Atomic.
    [vV] Atomic vector (seecheckAtomicVector).
    [lL] List. Missingness is defined asNULL element.
    [mM] Matrix.
    [dD] Data.frame. Missingness is checked recursively on columns.
    [pP] POSIXct date.
    [e] Environment.
    [0]NULL.
    [*] placeholder to allow any type.

    Note that the check for missingness does not distinguish betweenNaN andNA. Infinite values are not treated as missing, butcan be caught using boundary checks (part 3).

  2. Length definition. This can be one of

    [*] any length,
    [?] length of zero or one,
    [+] length of at least one, or
    [0-9]+ exact length specified as integer.

    Preceding the exact length with one of the comparison operators=/==,<,<=,>= or> is also supported.

  3. Range check as two numbers separated by a comma, enclosed by square brackets(endpoint included) or parentheses (endpoint excluded).For example, “[0, 3)” results inall(x >= 0 & x < 3).The lower and upper bound may be omitted which is the equivalent of a negative orpositive infinite bound, respectively.By definition[0,] containsInf, while[0,) does not.The same holds for the left (lower) boundary and-Inf.E.g., the rule “N1()” checks for a single finite numeric which is not NA,while “N1[)” allows-Inf.

Value

qassert throws anR exception if objectx doesnot comply to at least one of therules and returns the tested object invisiblyotherwise.qtest behaves the same way but returnsFALSE if none of therules comply.qexpect is intended to be inside the unit test frameworktestthat andreturns anexpectation.

Note

The functions are inspired by the blog post of Bogumił Kamiński:http://rsnippets.blogspot.de/2013/06/testing-function-agruments-in-gnu-r.html.The implementation is mostly written in C to minimize the overhead.

See Also

qtestr andqassertr for efficient checksof list elements and data frame columns.

Examples

# logical of length 1qtest(NA, "b1")# logical of length 1, NA not allowedqtest(NA, "B1")# logical of length 0 or 1, NA not allowedqtest(TRUE, "B?")# numeric with length > 0qtest(runif(10), "n+")# integer with length > 0, NAs not allowed, all integers >= 0 and < Infqtest(1:3, "I+[0,)")# either an emtpy list or a character vector with <=5 elementsqtest(1, c("l0", "s<=5"))# data frame with at least one column and no missing value in any columnqtest(iris, "D+")

Quick recursive arguments checks on lists and data frames

Description

These functions are the tuned counterparts ofqtest,qassert andqexpect tailored for recursivechecks of list elements or data frame columns.

Usage

qassertr(x, rules, .var.name = vname(x))qtestr(x, rules, depth = 1L)qexpectr(x, rules, info = NULL, label = vname(x))

Arguments

x

[list ordata.frame]
List or data frame to check for compliance with at least one ofrules.See details ofqtest for rule explanation.

rules

[character]
Set of rules. Seeqtest

.var.name

[character(1)]
Name of the checked object to print in error messages. Defaults tothe heuristic implemented invname.

depth

[integer(1)]
Maximum recursion depth. Defaults to “1” to directly check list elements ordata frame columns. Set to a higher value to check lists of lists of elements.

info

[character(1)]
Extra information to be included in the message for the testthat reporter.Seeexpect_that.

label

[character(1)]
Name of the checked object to print in messages. Defaults tothe heuristic implemented invname.

Value

Seeqassert.

See Also

qtest,qassert

Examples

# All list elements are integers with length >= 1?qtestr(as.list(1:10), "i+")# All list elements (i.e. data frame columns) are numeric?qtestr(iris, "n")# All list elements are numeric, w/o NAs?qtestr(list(a = 1:3, b = rnorm(1), c = letters), "N+")# All list elements are numeric OR characterqtestr(list(a = 1:3, b = rnorm(1), c = letters), c("N+", "S+"))

Select Backend for Unit Tests

Description

Allows to explicitly select a backend for the unit tests.Currently supported are"testthat" and"tinytest".The respective package must be installed and are loaded (but not attached).

If this function is not explicitly called, defaults to"testthat" unlessthe"tinytest"'s namespace is loaded.

Usage

register_test_backend(name)

Arguments

name

[character(1)]
"testthat" or"tinytest".

Value

NULL (invisibly).


Lookup a variable name

Description

Tries to heuristically determine the variable name ofx in the parent framewith a combination ofdeparse andsubstitute.Used for checkmate's error messages.

Usage

vname(x)

Arguments

x

[any]
Object.

Value

[character(1)] Variable name.


Get the index of the first/last TRUE

Description

A quick C implementation for “which.first” (head(which(x), 1)) and“which.last” (tail(which(x), 1)).

Usage

wf(x, use.names = TRUE)wl(x, use.names = TRUE)

Arguments

x

[logical]
Logical vector.

use.names

[logical(1)]
IfTRUE andx is named, the result is alsonamed.

Value

[integer(1) |integer(0)].Returns the index of the first/lastTRUE value inx oran empty integer vector if none is found. NAs are ignored.

Examples

wf(c(FALSE, TRUE))wl(c(FALSE, FALSE))wf(NA)

[8]ページ先頭

©2009-2026 Movatter.jp