Menu
Coddy logo textTech

Validity Checks

Part of the Object Oriented Programming section of Coddy's R journey. Lesson 24 of 57.

Types are checked by R; rules about values are yours. The validity argument of setClass() is a function of object that returns TRUE when the object is fine, or a character string describing the problem. new() runs it for every object:

setClass("Account", slots = c(owner = "character", balance = "numeric"),
  validity = function(object) {
    if (object@balance < 0) return("balance must not be negative")
    TRUE
  })
ok <- new("Account", owner = "Ada", balance = 10)
msg <- tryCatch(new("Account", owner = "Bo", balance = -5), error = function(e) conditionMessage(e))
cat(msg)

Output:

invalid class “Account” object: balance must not be negative

A validity function can report several problems at once by returning a character vector. R numbers them in the error message:

setClass("Range", slots = c(lo = "numeric", hi = "numeric"),
  validity = function(object) {
    problems <- character(0)
    if (length(object@lo) != 1) problems <- c(problems, "lo must be one number")
    if (length(object@hi) != 1) problems <- c(problems, "hi must be one number")
    if (length(problems) == 0) TRUE else problems
  })
msg <- tryCatch(new("Range", lo = c(1, 2), hi = numeric(0)), error = function(e) conditionMessage(e))
cat(msg)

Output:

invalid class “Range” object: 1: lo must be one number
invalid class “Range” object: 2: hi must be one number

@<- checks the type but does not run the validity function, so an assignment can break a rule. validObject() runs the check again; call it after changing slots:

setClass("Account", slots = c(balance = "numeric"),
  validity = function(object) if (object@balance < 0) "balance must not be negative" else TRUE)
a <- new("Account", balance = 10)
a@balance <- -99
cat("still created:", a@balance, "\n")
msg <- tryCatch(validObject(a), error = function(e) conditionMessage(e))
cat(msg, "\n")

Output:

still created: -99 
invalid class “Account” object: balance must not be negative 

R puts invalid class “Account” object: in front of your text. Code that shows the message to a user can keep only the part after object: :

setClass("Account", slots = c(balance = "numeric"),
  validity = function(object) if (object@balance < 0) "balance must not be negative" else TRUE)
msg <- tryCatch(new("Account", balance = -1), error = function(e) conditionMessage(e))
cat(sub(".*object: ", "", msg))

Output:

balance must not be negative
challenge icon

Challenge

Easy

Declare the class Rating in Rating.R with the slots stars (numeric) and comment (character) and a validity function that returns the first broken rule:

  • stars must be a whole number from 1 to 5 when the stars are not one of 1, 2, 3, 4, 5;
  • comment is too long when the comment has more than 20 characters.

Otherwise it returns TRUE. The supplied code reads lines 4;Great (the comment may be missing), creates each rating and prints ok: 4/5 or the problem text.

Your code goes in Rating.R. main.R holds the supplied input/output code and cannot be edited.

Try it yourself

source("Rating.R")

# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
for (line in input) {
  p <- strsplit(line, ";")[[1]]
  out <- tryCatch({
    r <- new("Rating", stars = as.numeric(p[1]), comment = if (length(p) > 1) p[2] else "")
    paste0("ok: ", r@stars, "/5")
  }, error = function(e) sub(".*object: ", "", conditionMessage(e)))
  cat(out, "\n", sep = "")
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming

Practice on your own: Online R compiler