Skip to content

Commit c3ee6dc

Browse files
authored
Merge pull request #945 from tidyverse/docs-913-pillar-vignettes
- Move `vignette("digits")`, `vignette("numbers")`, `?num` and `?char` from the pillar package here (#913).
2 parents f344af5 + 84f5423 commit c3ee6dc

File tree

20 files changed

+1139
-22
lines changed

20 files changed

+1139
-22
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Suggests:
6464
pkgload,
6565
purrr,
6666
rmarkdown,
67+
stringi,
6768
testthat (>= 3.0.2),
6869
tidyr,
6970
withr

NAMESPACE

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,8 @@ import(lifecycle)
7474
import(rlang)
7575
importFrom(magrittr,"%>%")
7676
importFrom(methods,setOldClass)
77-
importFrom(pillar,char)
7877
importFrom(pillar,dim_desc)
7978
importFrom(pillar,glimpse)
80-
importFrom(pillar,num)
8179
importFrom(pillar,obj_sum)
8280
importFrom(pillar,size_sum)
8381
importFrom(pillar,style_subtle)

R/pillar.R

Lines changed: 165 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,168 @@
1-
#' @importFrom pillar num
1+
#' Format a numeric vector
2+
#'
3+
#' @description
4+
#' `r lifecycle::badge("experimental")`
5+
#'
6+
#' Constructs a numeric vector that can be formatted with predefined
7+
#' significant digits, or with a maximum or fixed number of digits
8+
#' after the decimal point.
9+
#' Scaling is supported, as well as forcing a decimal, scientific
10+
#' or engineering notation.
11+
#' If a label is given, it is shown in the header of a column.
12+
#'
13+
#' The formatting is applied when the vector is printed or formatted,
14+
#' and also in a tibble column.
15+
#' The formatting annotation and the class survives most arithmetic transformations,
16+
#' the most notable exceptions are [var()] and [sd()].
17+
#'
18+
#' @family vector classes
19+
#' @inheritParams ellipsis::dots_empty
20+
#' @param x A numeric vector.
21+
#' @param sigfig Define the number of significant digits to show. Must be one or greater.
22+
#' The `"pillar.sigfig"` [option][pillar::pillar_options] is not consulted.
23+
#' Can't be combined with `digits`.
24+
#' @param digits Number of digits after the decimal points to show.
25+
#' Positive numbers specify the exact number of digits to show.
26+
#' Negative numbers specify (after negation) the maximum number of digits to show.
27+
#' With `digits = 2`, the numbers 1.2 and 1.234 are printed as 1.20 and 1.23,
28+
#' with `digits = -2` as 1.2 and 1.23, respectively.
29+
#' Can't be combined with `sigfig`.
30+
#' @param label A label to show instead of the type description.
31+
#' @param scale Multiplier to apply to the data before showing.
32+
#' Useful for displaying e.g. percentages.
33+
#' Must be combined with `label`.
34+
#' @param notation One of `"fit"`, `"dec"`, `"sci"`, `"eng"`, or `"si"`.
35+
#' - `"fit"`: Use decimal notation if it fits and if it consumes 13 digits or less,
36+
#' otherwise use scientific notation. (The default for numeric pillars.)
37+
#' - `"dec"`: Use decimal notation, regardless of width.
38+
#' - `"sci"`: Use scientific notation.
39+
#' - `"eng"`: Use engineering notation, i.e. scientific notation
40+
#' using exponents that are a multiple of three.
41+
#' - `"si"`: Use SI notation, prefixes between `1e-24` and `1e24` are supported.
42+
#' @param fixed_exponent
43+
#' Use the same exponent for all numbers in scientific, engineering or SI notation.
44+
#' `-Inf` uses the smallest, `+Inf` the largest fixed_exponent present in the data.
45+
#' The default is to use varying exponents.
46+
#' @param extra_sigfig
47+
#' If `TRUE`, increase the number of significant digits if the data consists of
48+
#' numbers of the same magnitude with subtle differences.
249
#' @export
3-
pillar::num
50+
#' @examples
51+
#' # Display as a vector
52+
#' num(9:11 * 100 + 0.5)
53+
#' @examples
54+
#'
55+
#' # Significant figures
56+
#' tibble(
57+
#' x3 = num(9:11 * 100 + 0.5, sigfig = 3),
58+
#' x4 = num(9:11 * 100 + 0.5, sigfig = 4),
59+
#' x5 = num(9:11 * 100 + 0.5, sigfig = 5),
60+
#' )
61+
#'
62+
#' # Maximum digits after the decimal points
63+
#' tibble(
64+
#' x0 = num(9:11 * 100 + 0.5, digits = 0),
65+
#' x1 = num(9:11 * 100 + 0.5, digits = -1),
66+
#' x2 = num(9:11 * 100 + 0.5, digits = -2),
67+
#' )
68+
#'
69+
#' # Use fixed digits and a currency label
70+
#' tibble(
71+
#' usd = num(9:11 * 100 + 0.5, digits = 2, label = "USD"),
72+
#' gbp = num(9:11 * 100 + 0.5, digits = 2, label = "£"),
73+
#' chf = num(9:11 * 100 + 0.5, digits = 2, label = "SFr")
74+
#' )
75+
#'
76+
#' # Scale
77+
#' tibble(
78+
#' small = num(9:11 / 1000 + 0.00005, label = "%", scale = 100),
79+
#' medium = num(9:11 / 100 + 0.0005 , label = "%", scale = 100),
80+
#' large = num(9:11 / 10 + 0.005 , label = "%", scale = 100)
81+
#' )
82+
#'
83+
#' # Notation
84+
#' tibble(
85+
#' sci = num(10^(-13:6), notation = "sci"),
86+
#' eng = num(10^(-13:6), notation = "eng"),
87+
#' si = num(10^(-13:6), notation = "si"),
88+
#' dec = num(10^(-13:6), notation = "dec")
89+
#' )
90+
#'
91+
#' # Fixed exponent
92+
#' tibble(
93+
#' scimin = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = -Inf),
94+
#' engmin = num(10^(-7:6) * 123, notation = "eng", fixed_exponent = -Inf),
95+
#' simin = num(10^(-7:6) * 123, notation = "si", fixed_exponent = -Inf)
96+
#' )
97+
#'
98+
#' tibble(
99+
#' scismall = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = -3),
100+
#' scilarge = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = 3),
101+
#' scimax = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = Inf)
102+
#' )
103+
#'
104+
#' #' Extra significant digits
105+
#' tibble(
106+
#' default = num(100 + 1:3 * 0.001),
107+
#' extra1 = num(100 + 1:3 * 0.001, extra_sigfig = TRUE),
108+
#' extra2 = num(100 + 1:3 * 0.0001, extra_sigfig = TRUE),
109+
#' extra3 = num(10000 + 1:3 * 0.00001, extra_sigfig = TRUE)
110+
#' )
111+
# Assigned in .onLoad()
112+
num <- NULL
4113

5-
#' @importFrom pillar char
114+
#' Format a character vector
115+
#'
116+
#' @description
117+
#' `r lifecycle::badge("experimental")`
118+
#'
119+
#' Constructs a character vector that can be formatted with predefined minimum width
120+
#' or without width restrictions, and where the abbreviation style can be configured.
121+
#'
122+
#' The formatting is applied when the vector is printed or formatted,
123+
#' and also in a tibble column.
124+
#'
125+
#' @family vector classes
126+
#' @inheritParams ellipsis::dots_empty
127+
#' @param x A character vector.
128+
#' @param min_chars The minimum width to allocate to this column, defaults to 15.
129+
#' The `"pillar.min_chars"` [option][pillar::pillar_options] is not consulted.
130+
#' @param shorten How to abbreviate the data if necessary:
131+
#' - `"back"` (default): add an ellipsis at the end
132+
#' - `"front"`: add an ellipsis at the front
133+
#' - `"mid"`: add an ellipsis in the middle
134+
#' - `"abbreviate"`: use [abbreviate()]
6135
#' @export
7-
pillar::char
136+
#' @examples
137+
#' # Display as a vector:
138+
#' char(letters[1:3])
139+
#' @examplesIf { set.seed(20210331); rlang::is_installed("stringi") }
140+
#' # Space constraints:
141+
#' rand_strings <- stringi::stri_rand_strings(10, seq(40, 22, by = -2))
142+
#'
143+
#' # Plain character vectors get truncated if space is limited:
144+
#' data_with_id <- function(id) {
145+
#' tibble(
146+
#' id,
147+
#' some_number_1 = 1, some_number_2 = 2, some_number_3 = 3,
148+
#' some_number_4 = 4, some_number_5 = 5, some_number_6 = 6,
149+
#' some_number_7 = 7, some_number_8 = 8, some_number_9 = 9
150+
#' )
151+
#' }
152+
#' data_with_id(rand_strings)
153+
#'
154+
#' # Use char() to avoid or control truncation
155+
#' data_with_id(char(rand_strings, min_chars = 24))
156+
#' data_with_id(char(rand_strings, min_chars = Inf))
157+
#' data_with_id(char(rand_strings, min_chars = 24, shorten = "mid"))
158+
#'
159+
#' # Lorem Ipsum, one sentence per row.
160+
#' lipsum <- unlist(strsplit(stringi::stri_rand_lipsum(1), "(?<=[.]) +", perl = TRUE))
161+
#' tibble(
162+
#' back = char(lipsum, shorten = "back"),
163+
#' front = char(lipsum, shorten = "front"),
164+
#' mid = char(lipsum, shorten = "mid")
165+
#' )
166+
#' tibble(abbr = char(lipsum, shorten = "abbreviate"))
167+
# Assigned in .onLoad()
168+
char <- NULL

R/print.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
#' Printing can be tweaked for a one-off call by calling `print()` explicitly
1414
#' and setting arguments like `n` and `width`. More persistent control is
1515
#' available by setting the options described in [pillar::pillar_options].
16-
#' See also `vignette("digits", package = "pillar")` for a comparison to base options,
17-
#' and [num()] and [char()] for creating columns with custom formatting options.
16+
#' See also `vignette("digits")` for a comparison to base options,
17+
#' and `vignette("numbers")` that showcases [num()] and [char()]
18+
#' for creating columns with custom formatting options.
1819
#'
1920
#' As of tibble 3.1.0, printing is handled entirely by the \pkg{pillar} package.
2021
#' If you implement a package that extends tibble,

R/zzz.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
} else {
66
safe_match <<- safe_match_default
77
}
8+
9+
num <<- pillar::num
10+
char <<- pillar::char
811
}
912

1013
safe_match_3_0 <- function(x, table) {

man/char.Rd

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/formatting.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)