Learn R Programming

cleanr (version 1.1.3)

function_checks: Function Checks

Description

A set of tiny functions to check that functions adhere to a layout style. A function should have a clear layout, it should
  • not have too many arguments,
  • not have too many levels of nesting,
  • neither have too many lines nor
  • have too many lines of code,
  • not have lines too wide and
  • explicitly return an object.

Usage

check_num_arguments(object, max_num_arguments = gco("max_num_arguments"))
check_nesting_depth(object, max_nesting_depth = gco("max_nesting_depth"))
check_num_lines(object, max_lines = gco("max_lines"))
check_num_lines_of_code(object, max_lines_of_code = gco("max_lines_of_code"))
check_line_width(object, max_line_width = gco("max_line_width"))
check_return(object, check_return = gco("check_return"))

Arguments

object
The function to be checked. Should have been sourced with keep.source = TRUE (see get_function_body).
max_num_arguments
The maximum number of arguments accepted. Set (preferably via set_cleanr_options) to NULL or FALSE to disable the check.
max_nesting_depth
The maximum nesting depth accepted. Set (preferably via set_cleanr_options) to NULL or FALSE to disable the check.
max_lines
The maximum number of lines accepted. Set (preferably via set_cleanr_options) to NULL or FALSE to disable the check.
max_lines_of_code
The maximum number of lines of code accepted. Set (preferably via set_cleanr_options) to NULL or FALSE to disable the check.
max_line_width
The maximum line width accepted. Set (preferably via set_cleanr_options) to NULL or FALSE to disable the check.
check_return
Set (preferably via set_cleanr_options) to NULL or FALSE to disable the check.

Value

invisible(TRUE), but see Details.

Warning

check_return just greps for a for a line starting with a return statement (ah, see the code for the real thing). This does not ensure that all return paths from the function are explicit and it may miss a return path after a semicolon. It just checks if you use return at all.

Details

In case of a fail all function_checks throw a condition of class c("cleanr", "error", "condition").

Examples

Run this code
print(check_num_arguments(check_num_arguments))
print(check_nesting_depth(check_nesting_depth))
print(check_num_lines(check_num_lines))
print(check_num_lines_of_code(check_num_lines_of_code))
print(check_return(check_return))
# R reformats functions on import (see
# help(get_function_body, package = "cleanr")), so we need 90 characters:
print(check_line_width(check_line_width, max_line_width = 90))

Run the code above in your browser using DataLab