Debian Codegraph @

R/options.R

raw
1 #' Package options
2 #'
3 #' Options that affect display of tibble-like output.
4 #'
5 #' These options can be set via [options()] and queried via [getOption()].
6 #'
7 #' @usage NULL
8 #' @format NULL
9 #'
10 #' @examplesIf rlang::is_installed("tibble")
11 #' df <- tibble::tibble(x = c(1.234567, NA, 5:10))
12 #' df
13 #'
14 #' # Change for the duration of the session:
15 #' old <- options(
16 #' pillar.sigfig = 6,
17 #' pillar.print_max = 5,
18 #' pillar.print_min = 5,
19 #' pillar.advice = FALSE
20 #' )
21 #' df
22 #'
23 #' # Change back to the original value:
24 #' options(old)
25 #' df
26 #'
27 #' @section Options for the pillar package:
28 #'
29 #' - `width`: The width option controls the output width.
30 #' Setting `options(pillar.width = )` to a larger value
31 #' will lead to printing in multiple tiers (stacks).
32 #'
33 pillar_options <- list2(
34 #' - `pillar.print_max`: Maximum number of rows printed, default: `20`.
35 #' Set to \code{Inf} to always print all rows.
36 #' For compatibility reasons, `getOption("tibble.print_max")` and
37 #' `getOption("dplyr.print_max")` are also consulted,
38 #' this will be soft-deprecated in pillar v2.0.0.
39 print_max = make_option_impl(
40 getOption("pillar.print_max", default = tibble_opt("print_max", 20L))
41 ),
42 #' - `pillar.print_min`: Number of rows printed if the table has more than
43 #' `print_max` rows, default: `10`.
44 #' For compatibility reasons, `getOption("tibble.print_min")` and
45 #' `getOption("dplyr.print_min")` are also consulted,
46 #' this will be soft-deprecated in pillar v2.0.0.
47 print_min = make_option_impl(
48 getOption("pillar.print_min", default = tibble_opt("print_min", 10L))
49 ),
50 #' - `pillar.width`: Output width. Default: `NULL`
51 #' (use `getOption("width")`).
52 #' This can be larger than `getOption("width")`, in this case the output
53 #' of the table's body is distributed over multiple tiers for wide tibbles.
54 #' For compatibility reasons, `getOption("tibble.width")` and
55 #' `getOption("dplyr.width")` are also consulted,
56 #' this will be soft-deprecated in pillar v2.0.0.
57 width = make_option_impl(
58 getOption("pillar.width", default = tibble_opt("width", getOption("width")))
59 ),
60 #' - `pillar.max_footer_lines`: The maximum number of lines in the footer,
61 #' default: `7`. Set to `Inf` to turn off truncation of footer lines.
62 #' The `max_extra_cols` option still limits
63 #' the number of columns printed.
64 max_footer_lines = make_option_impl(
65 getOption("pillar.max_footer_lines", default = 7L)
66 ),
67 #' - `pillar.max_extra_cols`: The maximum number of columns printed in the footer,
68 #' default: `100`. Set to `Inf` to show all columns.
69 #' Set the more predictable `max_footer_lines` to control the number
70 #' of footer lines instead.
71 max_extra_cols = make_option_impl(
72 getOption("pillar.max_extra_cols", default = tibble_opt("max_extra_cols", 100L))
73 ),
74 #' - `pillar.bold`: Use bold font, e.g. for column headers? This currently
75 #' defaults to `FALSE`, because many terminal fonts have poor support for
76 #' bold fonts.
77 bold = make_option_impl(
78 getOption("pillar.bold", default = FALSE)
79 ),
80 #' - `pillar.subtle`: Use subtle style, e.g. for row numbers and data types?
81 #' Default: `TRUE`.
82 subtle = make_option_impl(
83 getOption("pillar.subtle", default = TRUE)
84 ),
85 #' - `pillar.subtle_num`: Use subtle style for insignificant digits? Default:
86 #' `FALSE`, is also affected by the `subtle` option.
87 subtle_num = make_option_impl(
88 getOption("pillar.subtle_num", default = FALSE)
89 ),
90 #' - `pillar.neg`: Highlight negative numbers? Default: `TRUE`.
91 neg = make_option_impl(
92 getOption("pillar.neg", default = TRUE)
93 ),
94 #' - `pillar.sigfig`: The number of significant digits that will be printed and
95 #' highlighted, default: `3`. Set the `subtle` option to `FALSE` to
96 #' turn off highlighting of significant digits.
97 sigfig = make_option_impl(option_name = "pillar.sigfig", {
98 sigfig <- getOption("pillar.sigfig", default = 3L)
99 if (!is.numeric(sigfig) || length(sigfig) != 1 || sigfig < 1L) {
100 inform("Option pillar.sigfig must be a positive number greater or equal 1. Resetting to 3.")
101 sigfig <- 3L
102 set_pillar_option_sigfig(sigfig)
103 }
104 sigfig
105 }),
106 #' - `pillar.min_title_chars`: The minimum number of characters for the column
107 #' title, default: `20`. Column titles may be truncated up to that width to
108 #' save horizontal space. Set to `Inf` to turn off truncation of column
109 #' titles.
110 min_title_chars = make_option_impl(
111 getOption("pillar.min_title_chars", default = 20L)
112 ),
113 #' - `pillar.min_chars`: The minimum number of characters wide to
114 #' display character columns, default: `3`. Character columns may be
115 #' truncated up to that width to save horizontal space. Set to `Inf` to
116 #' turn off truncation of character columns.
117 min_chars = make_option_impl(option_name = "pillar.min_chars", {
118 min_chars <- getOption("pillar.min_chars", default = 3L)
119 if (!is.numeric(min_chars) || length(min_chars) != 1 || min_chars < 3L) {
120 inform("Option pillar.min_chars must be a nonnegative number greater or equal 3. Resetting to 3.")
121 min_chars <- 3L
122 set_pillar_option_min_chars(min_chars)
123 }
124 min_chars
125 }),
126 #' - `pillar.max_dec_width`: The maximum allowed width for decimal notation,
127 #' default: `13`.
128 max_dec_width = make_option_impl(
129 getOption("pillar.max_dec_width", default = 13L)
130 ),
131 #' - `pillar.bidi`: Set to `TRUE` for experimental support for bidirectional scripts.
132 #' Default: `FALSE`. When this option is set, "left right override"
133 #' and "first strong isolate"
134 #' [Unicode controls](https://www.w3.org/International/questions/qa-bidi-unicode-controls)
135 #' are inserted to ensure that text appears in its intended direction
136 #' and that the column headings correspond to the correct columns.
137 bidi = make_option_impl(
138 getOption("pillar.bidi", default = FALSE)
139 ),
140 #' - `pillar.superdigit_sep`: The string inserted between superscript digits
141 #' and column names in the footnote. Defaults to a `"\u200b"`, a zero-width
142 #' space, on UTF-8 platforms, and to `": "` on non-UTF-8 platforms.
143 superdigit_sep = make_option_impl(
144 getOption("pillar.superdigit_sep", default = superdigit_sep_default())
145 ),
146 #' - `pillar.advice`: Should advice be displayed in the footer when columns or rows
147 #' are missing from the output? Defaults to `TRUE` for interactive sessions,
148 #' and to `FALSE` otherwise.
149 advice = make_option_impl(
150 getOption("pillar.advice", default = is_interactive())
151 ),
152 )
153
154 tibble_opt <- function(x, default) {
155 x_tibble <- paste0("tibble.", x)
156 res <- getOption(x_tibble)
157 if (!is.null(res)) {
158 return(res)
159 }
160
161 x_dplyr <- paste0("dplyr.", x)
162 res <- getOption(x_dplyr)
163 if (!is.null(res)) {
164 return(res)
165 }
166
167 default
168 }
169
170 get_width_print <- function(width) {
171 if (!is.null(width)) {
172 return(width)
173 }
174
175 get_pillar_option_width()
176 }
177
178 get_width_glimpse <- function(width) {
179 width <- get_width_print(width)
180
181 if (is.finite(width)) {
182 width
183 } else {
184 getOption("width")
185 }
186 }
187
188 get_n_print <- function(n, rows) {
189 if (!is.null(n) && n >= 0) {
190 return(n)
191 }
192
193 if (is.na(rows)) {
194 structure(get_pillar_option_print_min(), max = get_pillar_option_print_max())
195 } else if (rows > get_pillar_option_print_max()) {
196 get_pillar_option_print_min()
197 } else {
198 rows
199 }
200 }
201
202 get_max_extra_cols <- function(max_extra_cols) {
203 if (!is.null(max_extra_cols) && max_extra_cols >= 0) {
204 return(max_extra_cols)
205 }
206
207 get_pillar_option_max_extra_cols()
208 }
209
210 get_max_footer_lines <- function(max_footer_lines) {
211 if (!is.null(max_footer_lines) && max_footer_lines >= 0) {
212 return(max_footer_lines)
213 }
214
215 get_pillar_option_max_footer_lines()
216 }