-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathformats.R
345 lines (309 loc) · 9.69 KB
/
formats.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#' Additional Formatting Functions
#'
#' @description `r lifecycle::badge("stable")`
#'
#' This summarizes the additional Formatting Functions to work with `rtables`.
#'
#' @family formatting functions
#' @name formatting_functions
NULL
#' Formatting Fraction and Percentage
#'
#' @description `r lifecycle::badge("stable")`
#'
#' Formats a fraction together with ratio in percent.
#'
#' @param x (`integer`)\cr with elements `num` and `denom`.
#' @param ... required for `rtables` interface.
#' @return a string in the format `num / denom (ratio %)`. If `num` is 0 the format is `num / denom`.
#'
#' @family formatting functions
#' @export
#'
#' @examples
#' format_fraction(x = c(num = 2L, denom = 3L))
#' format_fraction(x = c(num = 0L, denom = 3L))
format_fraction <- function(x, ...) {
attr(x, "label") <- NULL
checkmate::assert_vector(x)
checkmate::assert_count(x["num"])
checkmate::assert_count(x["denom"])
result <- if (x["num"] == 0) {
paste0(x["num"], "/", x["denom"])
} else {
paste0(
x["num"], "/", x["denom"],
" (", round(x["num"] / x["denom"] * 100, 1), "%)"
)
}
return(result)
}
#' Formatting Fraction and Percentage with Fixed Single Decimal Place
#'
#' @description `r lifecycle::badge("stable")`
#'
#' Formats a fraction together with ratio in percent with fixed single decimal place.
#' Includes trailing zero in case of whole number percentages to always keep one decimal place.
#'
#' @param x (`integer`)\cr with elements `num` and `denom`.
#' @param ... required for `rtables` interface.
#' @return a string in the format `num / denom (ratio %)`. If `num` is 0 the format is `num / denom`.
#'
#' @family formatting functions
#' @export
#'
#' @examples
#' format_fraction_fixed_dp(x = c(num = 1L, denom = 2L))
#' format_fraction_fixed_dp(x = c(num = 1L, denom = 4L))
#' format_fraction_fixed_dp(x = c(num = 0L, denom = 3L))
format_fraction_fixed_dp <- function(x, ...) {
attr(x, "label") <- NULL
checkmate::assert_vector(x)
checkmate::assert_count(x["num"])
checkmate::assert_count(x["denom"])
result <- if (x["num"] == 0) {
paste0(x["num"], "/", x["denom"])
} else {
paste0(
x["num"], "/", x["denom"],
" (", sprintf("%.1f", round(x["num"] / x["denom"] * 100, 1)), "%)"
)
}
return(result)
}
#' Formatting Count and Fraction
#'
#' @description `r lifecycle::badge("stable")`
#'
#' Formats a count together with fraction with special consideration when count is `0`.
#'
#' @param x (`integer`)\cr vector of length 2, count and fraction.
#' @param ... required for `rtables` interface.
#' @return a string in the format `count (fraction %)`. If `count` is 0 the format is `0`.
#'
#' @family formatting functions
#' @export
#'
#' @examples
#' format_count_fraction(x = c(2, 0.6667))
#' format_count_fraction(x = c(0, 0))
format_count_fraction <- function(x, ...) {
attr(x, "label") <- NULL
if (any(is.na(x))) {
return("NA")
}
checkmate::assert_vector(x)
checkmate::assert_integerish(x[1])
assert_proportion_value(x[2], include_boundaries = TRUE)
result <- if (x[1] == 0) {
"0"
} else {
paste0(x[1], " (", round(x[2] * 100, 1), "%)")
}
return(result)
}
#' Formatting Count and Percentage with Fixed Single Decimal Place
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' Formats a count together with fraction with special consideration when count is `0`.
#'
#' @param x (`integer`)\cr vector of length 2, count and fraction.
#' @param ... required for `rtables` interface.
#' @return a string in the format `count (fraction %)`. If `count` is 0 the format is `0`.
#'
#' @family formatting functions
#' @export
#'
#' @examples
#' format_count_fraction_fixed_dp(x = c(2, 0.6667))
#' format_count_fraction_fixed_dp(x = c(2, 0.5))
#' format_count_fraction_fixed_dp(x = c(0, 0))
format_count_fraction_fixed_dp <- function(x, ...) {
attr(x, "label") <- NULL
if (any(is.na(x))) {
return("NA")
}
checkmate::assert_vector(x)
checkmate::assert_integerish(x[1])
assert_proportion_value(x[2], include_boundaries = TRUE)
result <- if (x[1] == 0) {
"0"
} else if (x[2] == 1) {
sprintf("%d (100%%)", x[1])
} else {
sprintf("%d (%.1f%%)", x[1], x[2] * 100)
}
return(result)
}
#' Formatting: XX as Formatting Function
#'
#' Translate a string where x and dots are interpreted as number place
#' holders, and others as formatting elements.
#'
#' @param str (`string`)\cr template.
#'
#' @return A `rtables` formatting function.
#' @examples
#' test <- list(c(1.658, 0.5761), c(1e1, 785.6))
#'
#' # Internal function - format_xx
#' \dontrun{
#' z <- format_xx("xx (xx.x)")
#' sapply(test, z)
#'
#' z <- format_xx("xx.x - xx.x")
#' sapply(test, z)
#'
#' z <- format_xx("xx.x, incl. xx.x% NE")
#' sapply(test, z)
#' }
#'
#' @keywords internal
format_xx <- function(str) {
# Find position in the string.
positions <- gregexpr(pattern = "x+\\.x+|x+", text = str, perl = TRUE)
x_positions <- regmatches(x = str, m = positions)[[1]]
# Roundings depends on the number of x behind [.].
roundings <- lapply(
X = x_positions,
function(x) {
y <- strsplit(split = "\\.", x = x)[[1]]
rounding <- function(x) {
round(x, digits = ifelse(length(y) > 1, nchar(y[2]), 0))
}
return(rounding)
}
)
rtable_format <- function(x, output) {
values <- Map(y = x, fun = roundings, function(y, fun) fun(y))
regmatches(x = str, m = positions)[[1]] <- values
return(str)
}
return(rtable_format)
}
#' Formatting Fraction with Lower Threshold
#'
#' @description `r lifecycle::badge("stable")`
#'
#' Formats a fraction when the second element of the input `x` is the fraction. It applies
#' a lower threshold, below which it is just stated that the fraction is smaller than that.
#'
#' @param threshold (`proportion`)\cr lower threshold.
#' @return An `rtables` Formatting Function that takes numeric input `x` where the second
#' element is the fraction that is formatted. If the fraction is above or equal to the threshold,
#' then it is displayed in percentage. If it is positive but below the threshold, it returns
#' "<1" e.g. if the threshold is `0.01`. If it is zero, then just "0" is returned.
#'
#' @family formatting functions
#' @export
#'
#' @examples
#' format_fun <- format_fraction_threshold(0.05)
#' format_fun(x = c(20, 0.1))
#' format_fun(x = c(2, 0.01))
#' format_fun(x = c(0, 0))
format_fraction_threshold <- function(threshold) {
assert_proportion_value(threshold)
string_below_threshold <- paste0("<", round(threshold * 100))
function(x, ...) {
assert_proportion_value(x[2], include_boundaries = TRUE)
ifelse(
x[2] > 0.01,
round(x[2] * 100),
ifelse(
x[2] == 0,
"0",
string_below_threshold
)
)
}
}
#' Formatting Extreme Values
#'
#' @description `r lifecycle::badge("stable")`
#'
#' `Rtables` Formatting Functions that handle extreme values.
#'
#' @details For each input, apply a format to the specified number of `digits`. If the value is
#' below a threshold, it returns "<0.01" e.g. if the number of `digits` is 2. If the value is
#' above a threshold, it returns ">999.99" e.g. if the number of `digits` is 2.
#' If it is zero, then returns "0.00".
#' @param digits (`integer`)\cr number of decimal places to display.
#' @family formatting functions
#' @name extreme_format
#' @order 1
#'
NULL
#' @describeIn extreme_format Internal helper function to calculate the threshold and create formatted strings
#' used in Formatting Functions. Returns a list with elements `threshold` and `format_string`.
#' @export
#' @examples
#'
#' h_get_format_threshold(2L)
h_get_format_threshold <- function(digits = 2L) {
checkmate::assert_integer(digits)
low_threshold <- 1 / (10 ^ digits) # styler: off
high_threshold <- 1000 - (1 / (10 ^ digits)) # styler: off
string_below_threshold <- paste0("<", low_threshold)
string_above_threshold <- paste0(">", high_threshold)
list(
"threshold" = c(low = low_threshold, high = high_threshold),
"format_string" = c(low = string_below_threshold, high = string_above_threshold)
)
}
#' @describeIn extreme_format Internal helper function to apply a threshold format to a value.
#' Creates a formatted string to be used in Formatting Functions.
#'
#' @param x (`number`)\cr value to format.
#' @export
#' @examples
#'
#' h_format_threshold(0.001)
#' h_format_threshold(1000)
h_format_threshold <- function(x, digits = 2L) {
if (is.na(x)) {
return(x)
}
checkmate::assert_numeric(x, lower = 0)
l_fmt <- h_get_format_threshold(digits)
result <- if (x < l_fmt$threshold["low"] && 0 < x) {
l_fmt$format_string["low"]
} else if (x > l_fmt$threshold["high"]) {
l_fmt$format_string["high"]
} else {
sprintf(fmt = paste0("%.", digits, "f"), x)
}
unname(result)
}
#' @describeIn extreme_format Create Formatting Function for a single extreme value.
#' @export
#' @examples
#'
#' format_fun <- format_extreme_values(2L)
#' format_fun(x = 0.127)
#' format_fun(x = Inf)
#' format_fun(x = 0)
#' format_fun(x = 0.009)
format_extreme_values <- function(digits = 2L) {
function(x, ...) {
checkmate::assert_scalar(x, na.ok = TRUE)
h_format_threshold(x = x, digits = digits)
}
}
#' @describeIn extreme_format Create Formatting Function for extreme values part of a confidence interval. Values
#' are formatted as e.g. "(xx.xx, xx.xx)" if if the number of `digits` is 2.
#' @export
#' @examples
#'
#' format_fun <- format_extreme_values_ci(2L)
#' format_fun(x = c(0.127, Inf))
#' format_fun(x = c(0, 0.009))
format_extreme_values_ci <- function(digits = 2L) {
function(x, ...) {
checkmate::assert_vector(x, len = 2)
l_result <- h_format_threshold(x = x[1], digits = digits)
h_result <- h_format_threshold(x = x[2], digits = digits)
paste0("(", l_result, ", ", h_result, ")")
}
}