https://github.com/r-lib/generics
Common generic methods
https://github.com/r-lib/generics
Last synced: over 1 year ago
JSON representation
Common generic methods
- Host: GitHub
- URL: https://github.com/r-lib/generics
- Owner: r-lib
- License: other
- Created: 2018-06-12T15:55:26.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2023-10-30T21:46:34.000Z (over 2 years ago)
- Last Synced: 2025-03-31T15:25:36.286Z (over 1 year ago)
- Language: R
- Homepage: https://generics.r-lib.org/
- Size: 1.54 MB
- Stars: 60
- Watchers: 4
- Forks: 13
- Open Issues: 9
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
---
output: github_document
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
```
# generics
[](https://github.com/r-lib/generics/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/r-lib/generics?branch=main)
`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:
```{r, eval = FALSE}
install.packages("generics")
```
To install the development version, use:
```{r, eval = FALSE}
# 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:
```{r}
library(generics)
fit
tidy
```
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:
``` {r, eval = FALSE}
#' @importFrom generics explain
#' @export
generics::explain
```
As an example, the [recipes](https://github.com/tidymodels/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.