Skip to content

false output in function assert() #293

Open

Description

@grewered

The function assert() doesn't seem to be doing what is described in the documentation: The resulting assertion is successful, if combine is “or” (default) and at least one check evaluates to TRUE or combine is “and” and all checks evaluate to TRUE. Otherwise, assert throws an informative error message. I always get error messages, no matter what I do.
Did I understand something wrong or overlook something, or are theses errors in the function (or in the documentation)?

  idCol <- "ID"
  assert(
    assert_character(idCol),
    assert_numeric(idCol)
  )
# I expect it to pass, but I get this error message:
> Error in eval(dots[[i]], envir = env) : 
> Assertion on 'idCol' failed: Must be of type 'numeric', not 'character'.
  idCol <- "ID"
  assert(
    assert_character(idCol),
    assert_subset(idCol, choices = c("ID", "id", "1"))
  )
# I expect it to pass, but I get this error message:
> Error: Assertion failed. One of the following must apply:
> * assert_character(idCol): ID
> * assert_subset(idCol): ID

  assert(
    assert_character(idCol),
    assert_subset(idCol, choices = c("ID", "id", "1")),
    combine = "and"
  )
# I expect it to pass, but I get this error message:
> Error: Assertion on 'idCol' failed: ID.

Activity

  1. m-muecke commented on Apr 16, 2026

    @m-muecke
    Contributor

    You need to pass check_* functions into assert(), see the docs

    library(checkmate)
    idCol <- "ID"
    assert(
      assert_character(idCol),
      assert_numeric(idCol)
    )
    #> Error:
    #> ! Assertion on 'idCol' failed: Must be of type 'numeric', not 'character'.
    assert(
      assert_character(idCol),
      assert_subset(idCol, choices = c("ID", "id", "1"))
    )
    #> Error:
    #> ! Assertion failed. One of the following must apply:
    #>  * assert_character(idCol): ID
    #>  * assert_subset(idCol): ID

    Created on 2026-04-16 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions