Skip to content

Commit

Permalink
ci: Keep a copy of the vignettes inside the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Jun 26, 2024
1 parent 3057559 commit 3d60bd1
Show file tree
Hide file tree
Showing 10 changed files with 1,720 additions and 5 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
^src/Makevars\.debug$
^\.vscode$
^\.gitpod\.yml$
^tests/testthat/helper-sync\.R$
6 changes: 1 addition & 5 deletions R/galley.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ render_galley <- function(name, md_name) {
installed <- galley_use_installed()
# stopifnot(!installed)

if (installed) {
input_path <- system.file("doc", name, package = pkg)
} else {
input_path <- system.file("vignettes", name, package = pkg)
}
input_path <- file.path("vignettes", name)

# Need fixed file name for stability
output_dir <- tempdir()
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/helper-sync.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dir.create("vignettes", showWarnings = FALSE)
file.copy(dir("../../vignettes", pattern = "[.]Rmd$", full.names = TRUE), "vignettes", overwrite = TRUE)
137 changes: 137 additions & 0 deletions tests/testthat/vignettes/digits.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
---
title: "Comparing display with data frames"
output: html_vignette
vignette: >
%\VignetteIndexEntry{Comparing display with data frames}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
library(tibble)
set.seed(1014)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```

```{r}
library(tibble)
```

Base R offers the `"digits"` and `"scipen"` options to control the number of significant digits and the switch to scientific notation.
For tibble, the options `"pillar.sigfig"` and `"pillar.max_dec_width"` fulfill a similar purpose.
This vignette showcases similarities and differences.
See `?pillar::pillar_options` and `?tibble_options` for an overview over all options.

## Digits

### Basic differences

The default for `getOption("digits")` is 7, whereas the `"pillar.sigfig"` option defaults to 3.
In the default setting, pillar prints the first three digits only (i.e. the digits that represent > 99.9% of the value of the number).
Another difference is that pillar will show at most the specified number of significant digits, even if space is available.
The rationale is to allow a quick glance over the most significant digits of a number, without spending too much horizontal space and without distraction from insignificant digits.

```{r}
options(digits = 3)
c(1.2345, 12.345, 123.45, 1234.5, 12345)
tibble(x = c(1.2345, 12.345, 123.45, 1234.5, 12345))
```

### Terminal zeros

Terminal zeros are only shown in pillar if there is a nonzero value past the significant digits shown.
This is in contrast to base R where terminal zeros are always shown if there is space, but hidden if the value is too insignificant:

```{r}
c(1, 1.00001)
tibble(x = c(1, 1.00001))
```

### Trailing dot

A trailing decimal separator is shown if there is a fractional part but the integer part already exceeds the significant digits.
The presence of the decimal separator does **not** indicate that the number is larger, only that there exists a nonzero fractional part:

```{r}
c(123, 123.45, 567.89)
tibble(x = c(123, 123.45, 567.89))
```

### Showing more digits

To show more significant digits, set the `"pillar.sigfig"` option to a larger value:

```{r}
options(digits = 7)
options(pillar.sigfig = 7)
c(1.2345, 12.345, 123.45, 1234.5, 12345)
tibble(x = c(1.2345, 12.345, 123.45, 1234.5, 12345))
```

Setting `"pillar.sigfig"` to a larger value will not enhance the display with digits deemed insignificant:

```{r}
options(digits = 7)
options(pillar.sigfig = 7)
c(1.2345, 12.3456, 123.4567, 1234.5678, 12345.6789)
tibble(x = c(1.2345, 12.3456, 123.4567, 1234.5678, 12345.6789))
```

### Fixed number of digits

To show a fixed number of decimal digits, use `num()` with a `digits` argument:

```{r}
num(c(1.2345, 12.345, 123.45, 1234.5, 12345), digits = 2)
```

See `vignette("numbers")` for details.

## Scientific notation

### When is it used?

Both base R and pillar switch to scientific notation when the decimal representation becomes too wide.
The larger `getOption("scipen")`, the stronger the resistance to switching to scientific notation.
The default `0` seems to be anchored at 13 digits for the integer part.

```{r}
123456789012
123456789012.3
1234567890123
1234567890123.4
options(scipen = 1)
1234567890123
12345678901234
12345678901234.5
```

The `"pillar.max_dec_width"` option is similar, it indicates the width that must be exceeded for a switch to scientific notation to happen.
This width includes the decimal separator.

```{r}
tibble(x = 123456789012)
tibble(x = 123456789012.3)
tibble(x = 1234567890123)
tibble(x = 1234567890123.4)
options(pillar.max_dec_width = 14)
tibble(x = 1234567890123)
tibble(x = 12345678901234)
tibble(x = 12345678901234.5)
```

### Enforce notation

To avoid switching to scientific notation, set the `"pillar.max_dec_width"` option to a large value.
Note that if the required width is not available to show the column, it will not be shown at all in this case.
The `notation` argument to `num()` offers more options:

```{r}
num(12345678901234567, notation = "dec")
num(12345678901234567, notation = "sci")
num(12345678901234567, notation = "eng")
num(12345678901234567, notation = "si")
```
79 changes: 79 additions & 0 deletions tests/testthat/vignettes/extending.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
title: "Extending tibble"
author: "Kirill Müller, Hadley Wickham"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Extending tibble}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

Tibbles are S3 objects of class `c("tbl_df", "tbl", "data.frame")`.
This means that they inherit their behavior from the `"data.frame"` class and add two subclasses `"tbl"` and `"tbl_df"`.
The pillar and tibble packages provide methods for `"tbl"` and `"tbl_df"`, respectively.
Package authors and programmers can implement subclasses that extend either `"tbl_df"` (and its subclasses) or only `"tbl"` to provide custom behavior for tibble-like objects.
In addition, vectors classes can be implemented from scratch or on top of existing classes, to be used as columns.
This article provides links to the various customization points that help you avoiding reimplement existing behavior, and describes the difference between `"tbl"` and `"tbl_df"`.

Read more in [the second edition of Advanced R](https://adv-r.hadley.nz/index.html):

- about the internal representation of data frames and tibbles in the [Lists](https://adv-r.hadley.nz/vectors-chap.html#lists) and [Data frames and tibbles](https://adv-r.hadley.nz/vectors-chap.html#tibble) sections of the [Vectors chapter](https://adv-r.hadley.nz/vectors-chap.html),
- about R's S3 object-oriented system in the [S3 chapter](https://adv-r.hadley.nz/s3.html).

## Topics documented elsewhere

- Change or tweak the way a tibble prints: `vignette("extending", package = "pillar")`

- Change or tweak the way a column type is printed in a tibble: `vignette("pillar", package = "vctrs")`

- Implement a new column data type: `vignette("s3-vector", package = "vctrs")`

- Making your tibble subclass work well with dplyr: `?dplyr::dplyr_extending`

## Data frame subclasses

```{r}
library(tibble)
```

For tibble >= 3.0.0, the `"tbl"` class is responsible for printing, while the `"tbl_df"` class adds tibble's sturdy subsetting and subset assignment behavior (see `vignette("invariants")` for details).
This means that a data frame class that would like to (mostly) print like a tibble but keep the behavior from base R's data frames can inherit from `c("tbl", "data.frame")` or just from `"tbl"`.
An example is the `"tbl_sql"` class in the dbplyr package that is used to implement lazy database tables.

### Tibble example

```{r}
my_tbl_df <- new_tibble(
list(a = 1:3, b = 2:4),
class = "my_tbl_df"
)
tbl_sum.my_tbl_df <- function(x, ...) {
c(
"My custom tibble" = "Some info about it",
NextMethod()
)
}
my_tbl_df
my_tbl_df[, "a"]
```

### Data frame example

```{r}
my_tbl <- vctrs::new_data_frame(
list(a = 1:3, b = 2:4),
class = c("my_tbl", "tbl")
)
tbl_sum.my_tbl <- function(x, ...) {
c(
"My custom data frame" = "Some info about it",
NextMethod()
)
}
my_tbl
my_tbl[, "a"]
```
Loading

0 comments on commit 3d60bd1

Please sign in to comment.