Field: Field class

FieldR Documentation

Field class

Description

Class representing a field in the schema.

Data values can be cast to native R types. Casting a value checks whether it is of the expected type, in the correct format, and compliant with any constraints imposed by the schema.

Usage

# Field$new(descriptor, missingValues = list(""))

Arguments

descriptor

Schema field descriptor.

missingValues

A list with vector strings representing missing values.

Format

R6Class object.

Details

A field descriptor MUST be a JSON object that describes a single field. The descriptor provides both human-readable documentation and machine-readable validation rules. It may also guide user interface generation for data entry.

The descriptor object MAY include additional custom properties. Of these, only the name property is REQUIRED.

name

The descriptor MUST contain a name property, typically corresponding to the column name in the data file. This name SHOULD be unique, and while not case-sensitive for uniqueness, preserving case is advisable.

title

A human-readable label for the field.

description

A text description of the field, e.g., "The recipient of the funds".

Value

Object of class R6Class.

Methods

Field$new(descriptor, missingValues = list(""))

Constructor to instantiate the Field class. Accepts the following arguments:

descriptor

Schema field descriptor.

missingValues

A list of strings representing missing values.

TableSchemaError

Raised if an error occurs during instantiation.

Field

Returns a Field class instance.

cast_value(value, constraints = TRUE)

Casts a given value according to the field's type and format.

value

Value to cast.

constraints

Logical or list of constraints to apply.

errors$TableSchemaError

Raised if casting fails due to a constraint violation.

any

Returns the cast value.

testValue(value, constraints = TRUE)

Tests if a value complies with the field definition.

value

Value to test.

constraints

Constraints configuration.

Boolean

Returns TRUE if the value is compliant.

Properties

name

Returns the field name.

type

Returns the field type.

format

Returns the field format.

required

Returns TRUE if the field is required.

constraints

Returns a list of field constraints.

descriptor

Returns the field descriptor.

Language

The key words MUST, MUST NOT, REQUIRED, SHALL, SHALL NOT, SHOULD, SHOULD NOT, RECOMMENDED, MAY, and OPTIONAL in this documentation are to be interpreted as described in RFC 2119.

See Also

Field Descriptors Specifications

Examples

DESCRIPTOR = list(name = "height", type = "number")

field <- Field$new(descriptor = DESCRIPTOR)

# get correct instance
field$name
field$format
field$type

# return true on test
field$testValue(1)

# cast value
field$cast_value(1)

# expand descriptor by defaults
field <- Field$new(descriptor = list(name = "name"))

field$descriptor


# parse descriptor with "enum" constraint
field <- Field$new(descriptor = list(name = "status", type = "string", 
                   constraints = list(enum = list('active', 'inactive'))))

field$testValue('active')
field$testValue('inactive')
field$testValue('activia')
field$cast_value('active')


# parse descriptor with "minimum" constraint'
field <- Field$new(descriptor = list(name = "length", type = "integer", 
                   constraints = list(minimum = 100)))

field$testValue(200)
field$testValue(50)


# parse descriptor with "maximum" constraint'
field <- Field$new(descriptor = list(name = "length", type = "integer", 
                   constraints = list(maximum = 100)))

field$testValue(50)
field$testValue(200)


frictionlessdata/tableschema-r documentation built on April 13, 2025, 3:51 p.m.