Unlimited learning, half price | 50% off

Last chance! 50% off unlimited learning

Sale ends in


interfacer

interfacer is primarily aimed at R package developers. It provides a framework for specifying the structure of dataframes as parameters for user functions and checking that user supplied dataframes conform to expectations. Missing columns or incorrectly typed columns can be identified and useful error messages returned. Specifying structure is part of the function definition and can be automatically included in roxygen2 documentation.

Installation

You can install the released version of interfacer from CRAN with:

install.packages("interfacer")

Most likely though you will be including this in another package via a DESCRIPTION file:

...
Imports: 
    tidyverse,
    interfacer
Suggests: 
    knitr,
    rmarkdown
...

This development versions of the package are hosted in the AI4CI r-universe. Installation from there is as follows:

options(repos = c(
  "ai4ci" = 'https://ai4ci.r-universe.dev/',
  CRAN = 'https://cloud.r-project.org'))

# Download and install interfacer in R
install.packages("interfacer")

Or via a DESCRIPTION file:

...
Imports: 
    tidyverse,
    interfacer
Remotes: github::ai4ci/interfacer
Suggests: 
    knitr,
    rmarkdown
...

You can also install the development version of interfacer from GitHub with:

# install.packages("devtools")
devtools::install_github("ai4ci/interfacer")

Example

interfacer is used within a function definition in a package to constrain the input of a function to be a particular shape. The @iparam annotation will generate documentation which explains the expected dataframe format.

#' An example function
#'
#' @iparam mydata a test dataframe input parameter
#' @param another an example other input parameter  
#' @param ... not used
#'
#' @return ... something not yet defined ...
#' @export
example_fn = function(
  
  # this parameter will be a dataframe with id and test columns
  # id will be a unique integer, and test a logical value
  mydata = interfacer::iface(
    id = integer + group_unique ~ "an integer ID",
    test = logical + default(FALSE) ~ "the test result"
  ),
  
  another = "value",
  ...
  
) {
  
  # this line enforces the `iface` rules for the dataframe, coercing columns
  # if possible and throwing helpful errors if not.
  mydata = interfacer::ivalidate(mydata, ...)
  
  # rest of function body can use `mydata` in the certain knowledge that
  # id is a unique integer and test is a logical value...
}

When calling this function, column name, data type and grouping structure checks are made on the input and informative errors thrown if the input is incorrectly specified.

interfacer also includes tools to help developers adopt iface specifications by generating them from example data, and for documenting dataframes bundled in a package.

Funding

The authors gratefully acknowledge the support of the UK Research and Innovation AI programme of the Engineering and Physical Sciences Research Council EPSRC grant EP/Y028392/1.

Copy Link

Version

Install

install.packages('interfacer')

Monthly Downloads

252

Version

0.3.3

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Robert Challen

Last Published

February 3rd, 2025

Functions in interfacer (0.3.3)

if_col_present

Execute a function or return a value if a column in present in a dataframe
check_single

Checks a set of variables are all of length one
roxy_tag_rd.roxy_tag_iparam

Support for @iparam tags
roxy_tag_parse.roxy_tag_iparam

Parser for @iparam tags
is.iface

Check if an object is an interface specification
is_col_present

Check for existence of a set of columns in a dataframe
ireturn

Check a return parameter from a function
switch_pipeline

Branch a dplyr pipeline based on a set of conditions
iface

Construct an interface specification
itest

Test dataframe conformance to an interface specification.
check_logical

Checks a set of variables can be coerced to a logical and coerces them
type.character

Coerce to a character.
iconvert

Convert a dataframe to a format compatible with an interface specification
idispatch

Dispatch to a named function based on the characteristics of a dataframe
%>%

Pipe operator
imapper

Specify mappings that can make dataframes compatible with an iface specification
print.iface

Format an iface specification for printing
type.anything

Coerce to an unspecified type
knit_print.iface

Format an iface specification for printing
ivalidate

Perform interface checks on dataframe inputs using enclosing function formal parameter definitions
igroup_process

Handle unexpected additional grouping structure
type.integer

Coerce to integer
type.logical

Coerce to a logical
iproto

Generate a zero length dataframe conforming to an iface specification
resolve_missing

Resolve missing values in function parameters and check consistency
recycle

Strictly recycle function parameters
type.factor

Coerce to a factor.
type.finite

Check for non-finite values
type.date

Coerce to a Date.
type.default

Set a default value for a column
type.double

Coerce to a double.
type.not_missing

Check for missing values
type.enum

Define a conformance rule to match a factor with specific levels.
type.numeric

Coerce to a numeric.
type.complete

Coerce to a complete set of values.
type.of_type

Check for a given class
type.positive_integer

Coerce to a positive integer.
type.positive_double

Coerce to a positive double.
use_iface

Generate interfacer code for a dataframe
type.proportion

Coerce to a number between 0 and 1
type.group_unique

Coerce to a unique value within the current grouping structure.
type.unique_id

A globally unique ids.
type.in_range

Define a conformance rule to confirm that a numeric is in a set range
use_dataframe

Use a dataframe in a package including structure based documentation
check_integer

Checks a set of variables can be coerced to integer and coerces them
check_numeric

Checks a set of variables can be coerced to numeric and coerces them
idocument

Document an interface contract for inserting into roxygen2
format.iface

Format an iface specification for printing
iclip

Create an iface specification from an example dataframe
as.list.iface

Cast an iface to a plain list.
check_consistent

Check function parameters conform to a set of rules
check_date

Checks a set of variables can be coerced to a date and coerces them
check_character

Checks a set of variables can be coerced to a character and coerces them