design icon indicating copy to clipboard operation
design copied to clipboard

Pattern: serve internal data in a humane way

Open jennybc opened this issue 6 years ago • 4 comments

Placeholder for another concrete pattern that recurs in tidyverse/r-lib packages, like #53 Pattern: Cross that Bridge When you Come To It


Sometimes internal data is also useful to users. Write a helper to give it to them in usable way.

Examples:

  • readr::readr_example(), readxl::readxl_example() for accessing example files that ship with the packages
  • googledrive::drive_endpoints(), googledrive::drive_mime_type() for accessing pre-processed knowledge about the Drive API

NULL input means "show me everything"

character or integer input is for selective access

jennybc avatar Mar 11 '19 20:03 jennybc

I'm not sure if I like the NULL input = "show me everything" — doesn't that violate type stability?

hadley avatar Mar 25 '19 13:03 hadley

It depends on what sort of type stability you mean. All of these functions always return something of fixed type (an atomic vector or a list). But yes it is true that the NULL input has length zero and the return value does not.

In googledrive, I created an expose() sentinel for this, inspired by ggplot2::waiver(), but I think you eventually indicated you didn't like that either.

Do you have other ideas for conveying "show me the whole object"?

jennybc avatar Mar 25 '19 21:03 jennybc

Maybe a plural? e.g. readr::readr_example(name) vs readr::readr_examples()?

hadley avatar Mar 25 '19 21:03 hadley

I like use the plural, it also aids discoverability and you can document them together.

I will do that in vroom.

jimhester avatar Apr 26 '19 13:04 jimhester