Learn R Programming

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

180

Version

0.4.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Robert Challen

Last Published

December 15th, 2025

Functions in interfacer (0.4.0)

ivalidate

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

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

Test dataframe conformance to an interface specification.
is.iface

Check if an object is an interface specification
check_integer

Checks a set of variables can be coerced to integer and coerces them
format.iface

Format an iface specification for printing
get_iface

Extract the interfacer specification for a function
igroup_process

Handle unexpected additional grouping structure
as.list.iface

Cast an iface to a plain list.
check_character

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

Format an iface specification for printing
print.iface

Format an iface specification for printing
is_col_present

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

Strictly recycle function parameters
imapper

Specify mappings that can make dataframes compatible with an iface specification
demo_idispatch

Test the interfacer idispatch functions
type.date

Coerce to a Date.
type.factor

Coerce to a factor.
type.enum

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

Coerce to a complete set of values.
roxy_tag_parse.roxy_tag_ireturn

Parser for @ireturn tags
roxy_tag_rd.roxy_tag_iparam

Support for @iparam tags
demo_iparam_tag

An example function
imissing

Test if an iface parameter was supplied to a function
idocument

Document an interface contract for inserting into roxygen2
interfacer-package

interfacer: Define and Enforce Contracts for Dataframes as Function Parameters
idispatch

Dispatch to a named function based on the characteristics of a dataframe
type.logical

Coerce to a logical
iproto

Generate a zero length dataframe conforming to an iface specification
type.anything

Coerce to an unspecified type
switch_pipeline

Branch a dplyr pipeline based on a set of conditions
type.character

Coerce to a character.
ireturn

Validate and return a value from a function
roxy_tag_rd.roxy_tag_ireturn

Support for @ireturn tags
resolve_missing

Resolve missing values in function parameters and check consistency
demo_ireturn_tag

Another example function
.should_run_checks

Determine whether context is in-development or deployed.
type.unique_id

A globally unique ids.
type.proportion

Coerce to a number between 0 and 1
use_dataframe

Use a dataframe in a package including structure based documentation
use_iface

Generate interfacer code for a dataframe
%>%

Pipe operator
type.not_missing

Check for missing values
roxy_tag_parse.roxy_tag_iparam

Parser for @iparam tags
type.positive_double

Coerce to a positive double.
type.numeric

Coerce to a numeric.
type.of_type

Check for a given class
type.positive_integer

Coerce to a positive integer.
type.group_unique

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

Check for non-finite values
type.double

Coerce to a double.
type.default

Set a default value for a column
type.integer

Coerce to integer
type.in_range

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

Check function parameters conform to a set of rules
check_numeric

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

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

Checks a set of variables are all of length one
iconvert

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

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

Construct an interface specification
iclip

Create an iface specification from an example dataframe