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
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