Giter Site home page Giter Site logo

Update S3 method advice about r-pkgs HOT 1 OPEN

hadley avatar hadley commented on August 12, 2024
Update S3 method advice

from r-pkgs.

Comments (1)

jennybc avatar jennybc commented on August 12, 2024

Specifically, we're talking about this passage:

The last and trickiest case is when your package offers a method for a generic "owned" by a package you've listed in `Suggests`.
The basic idea is that you want to register the availability of your S3 method conditionally, when your package is being loaded.
If the suggested package is present, your S3 method should be registered, but otherwise it should not.
We'll illustrate this with an example.
Within the tidyverse, the glue package is managed as a low-level package that should have minimal dependencies (@sec-dependencies-tidyverse).
Glue functions generally return a character vector that also has the `"glue"` S3 class.
```{r}
library(glue)
name <- "Betty"
(ret <- glue('My name is {name}.'))
class(ret)
```
The motivation for this is that it allows glue to offer special methods for `print()`, the `+` operator, and subsetting via `[` and `[[`.
One downside, though, is that this class attribute complicates string comparisons:
```{r}
identical(ret, "My name is Betty.")
all.equal(ret, "My name is Betty.")
```
Therefore, for testing, it is helpful if glue offers a method for `testthat::compare()`, which explains why this expectation succeeds:
```{r}
testthat::expect_equal(ret, "My name is Betty.")
```
But glue can't list testthat in `Imports`!
It must go in `Suggests`.
The solution is to register the method conditionally when glue is loaded.
Here is a redacted version of glue's `.onLoad()` function, where you'll see that it conditionally registers some other methods as well:
```{r}
#| eval: false
.onLoad <- function(...) {
s3_register("testthat::compare", "glue")
s3_register("waldo::compare_proxy", "glue")
s3_register("vctrs::vec_ptype2", "glue.glue")
...
invisible()
}
```
The `s3_register()` function comes from the vctrs package.
If you don't have an organic need to depend on vctrs, it is common (and encouraged) to simply inline [the `s3_register()` source](https://github.com/r-lib/vctrs/blob/main/R/register-s3.R) into your own package.
You can't always copy code from other people's packages and paste it into yours, but you can in this case.
This usage is specifically allowed by the license of the source code of `s3_register()`.
This provides a great segue into @sec-license, which is all about licensing.

from r-pkgs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.