check_crucial_names: Check if a named object contains expected names

View source: R/check_crucial_names.R

check_crucial_namesR Documentation

Check if a named object contains expected names

Description

It is based on: https://www.rdocumentation.org/packages/fgeo.tool/versions/1.2.5/topics/check_crucial_names.

Usage

check_crucial_names(x, expected_names)

Arguments

x

A named object.

expected_names

String; expected names of x.

Value

Invisible x, or an error with informative message.

See Also

Other miscellaneous utility functions: degrees(), dropbox_exists(), path_examples(), r2dii_packages()

Examples

x <- c(a = 1)
check_crucial_names(x, "a")
try(check_crucial_names(x, "bad"))

data <- data.frame(a = 1)
check_crucial_names(data, "a")
try(check_crucial_names(data, "bad"))

# Applications for the error class "missing_names" ---------------------

tryCatch(
  check_crucial_names(x, "bad"),
  error = function(e) class(e)
)

# Wrapping in try() to allow running examples with no failure
try(
  # What's interesting is this
  tryCatch(
    check_crucial_names(x, "bad"),
    missing_names = function(e) {
      stop(
        "A different error message",
        call. = FALSE
      )
    }
  )
)

testthat::expect_error(
  check_crucial_names(x, "bad"),
  class = "missing_names"
)

2DegreesInvesting/r2dii.utils documentation built on March 31, 2022, 11:20 p.m.