Type: Package
Title: A Data Specification Format and Interface
Version: 0.1.9
Author: Tim Bergsma
Maintainer: Tim Bergsma <bergsmat@gmail.com>
Description: Creates a data specification that describes the columns of a table (data.frame). Provides methods to read, write, and update the specification. Checks whether a table matches its specification. See specification.data.frame(),read.spec(), write.spec(), as.csv.spec(), respecify.character(), and %matches%.data.frame().
Imports: encode, csv, magrittr, utils
Depends: R (≥ 2.10)
License: GPL-3
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
NeedsCompilation: no
Packaged: 2024-02-18 03:51:44 UTC; tim.bergsma
Repository: CRAN
Date/Publication: 2024-02-18 05:30:07 UTC

Check Whether x and y Match

Description

Checks Whether x an y match.

Usage

x %matches% y

Arguments

x

object

y

object

See Also

Other matches: %matches%.character(), %matches%.data.frame(), %matches%.spec()


Check Whether Character matches y

Description

Checks whether character matches y, treating x as filepath.

Usage

## S3 method for class 'character'
x %matches% y, ...

Arguments

x

character

y

object

...

passed arguments

See Also

Other matches: %matches%.data.frame(), %matches%.spec(), %matches%()

Examples

data(drug)
file <- tempfile()
spec <- specification(drug, tol = 3)
library(csv)
as.csv(drug, file)
file %matches% spec

Check Whether Data Frame matches Spec

Description

Checks whether data.frame matches spec. Column names, count, and order are enforced. Encodings are enforced (all non-missing values must be valid codes). Integer and numeric ranges are enforced. Values of required are parsed and evaluated in data context: Where TRUE, the corresponding data value for column cannot be missing.

Usage

## S3 method for class 'data.frame'
x %matches% y, ...

Arguments

x

spec

y

coerced to spec (spec object or filepath for spec file).

...

passed arguments

Value

logical; TRUE if all checks above are enforceable.

See Also

Other matches: %matches%.character(), %matches%.spec(), %matches%()

Examples

data(drug)
file <- tempfile()
spec <- specification(drug, tol = 3)
write.spec(spec, file = file)
drug %matches% spec

Check Whether Spec matches Dataset

Description

Checks whether spec matches dataset by checking whether dataset matches spec.

Usage

## S3 method for class 'spec'
x %matches% y, ...

Arguments

x

spec

y

object

...

passed arguments

See Also

Other matches: %matches%.character(), %matches%.data.frame(), %matches%()

Examples

data(drug)
file <- tempfile()
spec <- specification(drug, tol = 3)
write.spec(spec, file = file)
spec %matches% drug

Make Guide

Description

Makes Guide

Usage

.guide(x, ...)

Arguments

x

object

...

dots

See Also

Other .guide: .guide.default(), .guide.factor(), .guide.integer(), .guide.numeric()


Make Default Guide

Description

Makes default guide.

Usage

## Default S3 method:
.guide(x, tol = 10, ...)

Arguments

x

object

tol

integer

...

dots

See Also

Other .guide: .guide.factor(), .guide.integer(), .guide.numeric(), .guide()


Make Factor Guide

Description

Makes Factor guide.

Usage

## S3 method for class 'factor'
.guide(x, ...)

Arguments

x

object

...

dots

See Also

Other .guide: .guide.default(), .guide.integer(), .guide.numeric(), .guide()


Make Integer Guide

Description

Makes integer guide.

Usage

## S3 method for class 'integer'
.guide(x, tol = 10, ...)

Arguments

x

object

tol

integer

...

dots

See Also

Other .guide: .guide.default(), .guide.factor(), .guide.numeric(), .guide()


Make Numeric Guide

Description

Makes numeric guide.

Usage

## S3 method for class 'numeric'
.guide(x, digits = 20, ...)

Arguments

x

object

digits

integer

...

dots

See Also

Other .guide: .guide.default(), .guide.factor(), .guide.integer(), .guide()


Make Label

Description

Makes label.

Usage

.label(x, ...)

Arguments

x

object

...

dots

See Also

Other .label: .label.default()


Make Default Label

Description

Makes default label.

Usage

## Default S3 method:
.label(x, ...)

Arguments

x

object

...

dots

See Also

Other .label: .label()


Make Required

Description

Makes required.

Usage

.required(x, ...)

Arguments

x

object

...

dots

See Also

Other .required: .required.default()


Make Default Required

Description

Makes default required.

Usage

## Default S3 method:
.required(x, ...)

Arguments

x

object

...

dots

See Also

Other .required: .required()


Make Type

Description

Makes type

Usage

.type(x, ...)

See Also

Other .type: .type.default(), .type.numeric(), .type.timepoint()


Make Default Type

Description

Makes default type.

Usage

## Default S3 method:
.type(x, ...)

Arguments

x

object

...

dots

See Also

Other .type: .type.numeric(), .type.timepoint(), .type()


Make Numeric Type

Description

Makes numeric type.

Usage

## S3 method for class 'numeric'
.type(x, ...)

Arguments

x

object

...

dots

See Also

Other .type: .type.default(), .type.timepoint(), .type()


Make Timepoint Type

Description

Makes timepoint type.

Usage

## S3 method for class 'timepoint'
.type(x, ...)

Arguments

x

object

...

dots

See Also

Other .type: .type.default(), .type.numeric(), .type()


Coerce to Spec

Description

Coerces to class spec, a specification object

Usage

as.spec(x, ...)

Arguments

x

object

...

passed arguments

See Also

Other as.spec: as.spec.character(), as.spec.data.frame(), read.spec(), write.spec()


Coerce to Specification from Character

Description

Coerces to specification from character (length-one filepath).

Usage

## S3 method for class 'character'
as.spec(x, ...)

Arguments

x

character path to spec-formatted file

...

passed arguments

Value

spec

See Also

Other as.spec: as.spec.data.frame(), as.spec(), read.spec(), write.spec()

Examples

data(drug)
file <- tempfile()
spec <- specification(drug, tol = 3)
write.spec(spec, file = file)
as.spec(file)

Coerce to Spec from Data Frame

Description

Coerces to spec from data.frame already having basic properties.

Usage

## S3 method for class 'data.frame'
as.spec(x, ...)

Arguments

x

data.frame

...

passed arguments

Value

spec

See Also

Other as.spec: as.spec.character(), as.spec(), read.spec(), write.spec()

Examples

data(drug)
as.spec(specification(drug, tol = 3))

Coerce to Vector from Spec.

Description

Coerces to vector from spec by returning column.

Usage

## S3 method for class 'spec'
as.vector(x, mode = "any")

Arguments

x

spec

mode

to match generic


Extract Codes from Specification

Description

Extracts codes from specification guide column.

Usage

## S3 method for class 'spec'
codes(x, column = x$column, ...)

Arguments

x

spec

column

character: items in column to check

...

passed arguments

Value

list


Extract Decodes from Specification

Description

Extracts decodes from specification guide column.

Usage

## S3 method for class 'spec'
decodes(x, column = x$column, ...)

Arguments

x

spec

column

character: items in column to check

...

passed arguments

Value

list


Simulated Pharmacometric Data

Description

A fictitious dataset giving doses and pharmacometric samples for multiple subjects in an imaginary Phase * drug trial.

Usage

drug

Format

A data frame with 600 rows and 24 variables:

C

a comment flag, typically NA but 'C' for records that should be ignored

ID

integer subject identifier

TIME

relative time (h)

SEQ

sequence identifier to break ties when sorting

EVID

event type identifier, 0: pk sample, 1: dose

AMT

drug amount (mg)

DV

plasma drug concentration (ng/mL)

SUBJ

subject identifier

HOUR

nominal hour (h)

HEIGHT

height (cm)

WEIGHT

weight (kg)

SEX

sex, 0: female, 1: male

AGE

age (y)

DOSE

dose group (mg)

FED

prandial state, 0: fasted, 1: fed

SMK

smoker status, 0: non, 1: smoker

DS

disease state, 0: no disease

CRCN

normalized creatinine clearance (mL/min)

TAFD

time since first dose (h)

TAD

time since most recent dose (h)

LDOS

amount of most recent dose (mg)

MDV

missing dependent value, 0: not missing, 1: missing

predose

predose flag, 0: record not predose, 1: record is predose

zerodv

zero DV flag, 0, DV not zero, 1: DV is zero


Checks whether a Specification is Encoded

Description

Checks whether a specification is encoded with respect to the guide column.

Usage

## S3 method for class 'spec'
encoded(x, column = x$column, ...)

Arguments

x

spec

column

character: items in column to check

...

passed arguments

Value

logical with same length as column argument


Extract Text from Guide

Description

Extracts text from guide.

Usage

guidetext(x, ...)

Arguments

x

object

...

passed arguments

See Also

Other guidetext: guidetext.spec()


Extract Text from Guide Column of Specification

Description

Extracts text from guide column of specification, ignoring number ranges if any.

Usage

## S3 method for class 'spec'
guidetext(x, column = x$column, ...)

Arguments

x

object

column

character: items in column to check

...

passed arguments

Value

character

See Also

Other guidetext: guidetext()

Examples

data(drug)
guidetext(specification(drug, tol = 3)) # NA, must be supplied manually

Extract Labels from Specification

Description

Extracts labels from specification label column.

Usage

## S3 method for class 'spec'
labels(object, column = object$column, ...)

Arguments

object

spec

column

character: items in column to check

...

passed arguments

Value

character

Examples

data(drug)
labels(specification(drug, tol = 3))

Print Spec

Description

Prints spec. Specifically, shortens display of encoded items that are above limit.

Usage

## S3 method for class 'spec'
print(x, limit = 8, ...)

Arguments

x

spec

limit

number of characters to allow without intervention

...

passed arguments

Value

character


Read Specification from File

Description

Reads specification from file. If first line contains tab characters, assumes format is tab-delimited text. Otherwise, assumes format is comma-separated variable (csv).

Usage

read.spec(x, clean = TRUE, ...)

Arguments

x

character (file path)

clean

whether to strip balanced double quotes and outer white space from character values

...

passed arguments (ignored)

Value

spec

See Also

Other as.spec: as.spec.character(), as.spec.data.frame(), as.spec(), write.spec()

Examples

data(drug)
file <- tempfile()
spec <- specification(drug, tol = 3)
write.spec(spec, file = file)
read.spec(file)

Respecify Something

Description

Respecify something.

Usage

respecify(x, ...)

Arguments

x

object

...

passed arguments

See Also

Other respecify: respecify.character(), respecify.spec()


Respecify Character

Description

Respecify specification, supplied as filepath. Updates numeric ranges. Useful if these have changed and spec no longer matches.

Usage

## S3 method for class 'character'
respecify(x, data = sub("spec$", "csv", x), file = x, ...)

Arguments

x

character filepath for a spec file (*.spec)

data

character filepath for a dataset

file

where to write the result (over-write source, by default)

...

passed arguments

See Also

Other respecify: respecify.spec(), respecify()


Respecify Specification

Description

Respecify specification. Updates numeric ranges. Useful if these have changed and spec no longer matches.

Usage

## S3 method for class 'spec'
respecify(x, data, file = NULL, ...)

Arguments

x

spec

data

a data.frame or path to csv file

file

where to write the result (default: do not write)

...

passed arguments

See Also

Other respecify: respecify.character(), respecify()

Examples

data(drug)
file <- tempfile()
spec <- specification(drug,tol = 3)
write.spec(spec, file = file)
drug %matches% spec
drug %matches% file
max <- max(drug$DV,na.rm=TRUE)
drug$DV[!is.na(drug$DV) & drug$DV == max] <- max + 1
drug %matches% file
respecify(file, drug)
drug %matches% file

Make a Specification

Description

Makes a specification.

Usage

specification(x, ...)

Arguments

x

object

...

passed arguments

See Also

Other specification: specification.comment(), specification.data.frame(), specification.default()

Examples

data(drug)
specification(drug, tol = 3)

Make a Specification for a Comment

Description

Makes a specification for a comment

Usage

## S3 method for class 'comment'
specification(x, ...)

Arguments

x

object

...

passed arguments

See Also

Other specification: specification.data.frame(), specification.default(), specification()


Make a Specification for a Data Frame

Description

Makes a specification for data.frame. Creates a template based on the data.frame. Uses column names for labels where columns do not have a label attribute. Factors will be encoded. numerics will be rounded to digits and like integers will be expressed as ranges in guide column. Integers and character with less than or exactly tol unique values will be encoded.

Usage

## S3 method for class 'data.frame'
specification(x, tol = 10, digits = 20, ...)

Arguments

x

object

tol

integer

digits

integer

...

passed arguments

Value

spec data.frame with columns as follows.

column

Column name.

label

A descriptive label. Save and edit as necessary using external tool.

guide

A guide to interpretation. NA for arbitrary character; range [low:high] for integer and numeric; an encoding e.g. //0/no//1/yes// for factor-like items ... save and edit factor labels as necessary using external tool.

For numeric ranges you can add text, such as units. E.g. if default guide is '[0:100]' you can edit to give 'mg [0:100]'. Or you can just substitute 'mg'. guidetext extracts just the character portion, and matches enforces the numeric range.

required

An R expression that can be coerced to logical. TRUE means item cannot be NA.

comment

Arbitrary comment, e.g. derivation of the item given by column.

See Also

link{read.spec} write.spec respecify.character write.spec matches

Other specification: specification.comment(), specification.default(), specification()

Examples

data(drug)
file <- tempfile()
spec <- specification(drug, tol = 3)

Make a Specification by Default

Description

Makes a specification by default method, i.e. returns input unchanged.

Usage

## Default S3 method:
specification(x, ...)

Arguments

x

object

...

passed arguments

See Also

Other specification: specification.comment(), specification.data.frame(), specification()


Specify Something

Description

Specify something.

Usage

specify(x, ...)

Arguments

x

object

...

passed arguments

See Also

Other specify: specify.character(), specify.data.frame()


Specify Character

Description

Attach specifics to a data.frame, supplied as csv filepath.

Usage

## S3 method for class 'character'
specify(x, file = sub("csv$", "spec", x), spec = read.spec(file), ...)

Arguments

x

character filepath for a csv file

file

character filepath for a matching spec file (ignored if spec provided)

spec

a data specification (spec)

...

passed arguments

See Also

Other specify: specify.data.frame(), specify()


Specify Data Frame

Description

Attach specifics to a data.frame as attributes, including label and guide.

Usage

## S3 method for class 'data.frame'
specify(x, spec, na.rm = TRUE, empty.rm = TRUE, ...)

Arguments

x

data.frame

spec

a data spec (or corresponding filepath) to use as source of attributes

na.rm

if TRUE, don't assign NA where encountered

empty.rm

if TRUE, don't assign empty string where encountered

...

passed arguments

See Also

Other specify: specify.character(), specify()

Examples

data(drug)
spec <- specification(drug,tol = 3)
drug %matches% spec
drug <- specify(drug,spec)
attributes(drug$HEIGHT)

Write Specification to Storage

Description

Writes specification to storage in tab-delimited format. Use as.csv() for CSV format.

Usage

write.spec(x, file, ...)

Arguments

x

spec

file

character filepath for storage location

...

passed arguments

See Also

Other as.spec: as.spec.character(), as.spec.data.frame(), as.spec(), read.spec()

Examples

data(drug)
file <- tempfile()
spec <- specification(drug, tol = 3)
write.spec(spec, file = file)