Learn R Programming

generics

generics is designed to help package authors reduce dependencies by providing a set of generic methods that can be imported. For example, if a package author wanted to include a tidy method for their object, they would have to import the broom package to do so. This would work but would potentially increase the number of package dependencies required to install and/or test the package.

Installation

To install generics from CRAN, use:

install.packages("generics")

To install the development version, use:

# install.packages("pak")
pak::pak("r-lib/generics")

Usage

generics is a simple, lightweight package that contains S3 generics to be used by other packages. Some examples are:

library(generics)
#> 
#> Attaching package: 'generics'
#> The following objects are masked from 'package:base':
#> 
#>     as.difftime, as.factor, as.ordered, intersect, is.element, setdiff,
#>     setequal, union

fit
#> function(object, ...) {
#>   UseMethod("fit")
#> }
#> <bytecode: 0x107df2b38>
#> <environment: namespace:generics>

tidy
#> function(x, ...) {
#>   UseMethod("tidy")
#> }
#> <bytecode: 0x107e583a8>
#> <environment: namespace:generics>

To use generics with your package, we recommend that you import and re-export the generic(s) of interest. For example, if you want to provide a method for the S3 explain() method, you’d using the following roxygen2 code:

#' @importFrom generics explain
#' @export
generics::explain

As an example, the recipes package defines a number of tidy() S3 methods by importing this package (whereas it previously depended on broom).

Documentation

When searching for help on a method that is exported from generics by one or more packages, using ?method will show entries for all exported methods. If the version from generics is selected, the Methods section dynamically lists all specific methods exported by any loaded packages.

Copy Link

Version

Install

install.packages('generics')

Monthly Downloads

1,091,450

Version

0.1.4

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Hadley Wickham

Last Published

May 9th, 2025

Functions in generics (0.1.4)

glance

Glance at an object
explore

Create an interactive visualization appropriate to a particular object type
setops

Set operations
specify

Specify variables or other quantities.
tidy

Turn an object into a tidy tibble
interpolate

Interpolate missing values
forecast

Forecasting from an object
tune_args

Determine arguments tagged for tuning
generate

Generate values based on inputs
min_grid

Determine the minimum set of model fits
learn

Estimate model parameters.
tunable

Declare tunable parameters
refit

Refitting models
fit

Estimate model parameters.
visualize

Visualize a data set or object.
fit_xy

Estimate model parameters.
required_pkgs

Determine packages required by objects
prune

Prune or reduce an object
rank_results

Compute relative rankings of a collection of objects
varying_args

Find any arguments that are not fully specified.
var_imp

Calculation of variable importance
calculate

Calculate statistics.
components

Extract components
compile

Configure an object
coercion-time-difference

Time difference coercion
equation

Model equations
coercion-factor

Factor coercion
estfun

Extracting the estimating functions of a fitted model.
augment

Augment data with information from an object
evaluate

Evaluate an object.
accuracy

Accuracy measures for a model
generics-package

generics: common S3 generics
hypothesize

Construct hypotheses.
explain

Explain details of an object
train

Estimate model parameters.