1

I don't understand why the following stopifnot does not error out:

> suppressMessages(library(tidyverse))
> print(.packages())
 [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
 [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
[13] "grDevices" "utils"     "datasets"  "methods"   "base"     
> stopifnot(.packages()==c())
> # The preceding stopifnot should have errored out
1
  • 1
    Using %in% in place of == will error out. Commented Dec 25, 2024 at 7:09

1 Answer 1

2

c() returns a NULL. .packages() == c() returns logical vector of length 0 since no comparisons performed. stopifnot function checks its arguments with all function, and apparently all(NULL) returns TRUE (none of its input is FALSE).

If you need to check if your variable is null, use is.null or is_empty function:

is.null(c())
#> [1] TRUE
is_empty(c())
#> [1] TRUE

If you want to check if all required packages are loaded use %in% operator with all function:

stopifnot(all(required_packages %in% .packages()))

or with setdiff:

length(setdiff(required_packages, .packages())) == 0
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I agree with the comment. I tried to illustrate comparison with NULL but will thing how to rephrase it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.