Skip to content

Commit 39551a8

Browse files
MoohanCopilot
andcommitted
Replace nested ifelse with casewhen
Update Master RMarkdown Document & Render Code/Global Script.R Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent e00060d commit 39551a8

1 file changed

Lines changed: 7 additions & 13 deletions

File tree

Master RMarkdown Document & Render Code/Global Script.R

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,13 @@ palette <- phsstyles::phs_colours(c(
4848
## Function for formatting a value for R Markdown text
4949
# First determines how many dps the value should have
5050
# Then adds a comma for numbers over 1000 (becomes "1,000")
51-
52-
format_number_for_text <- function(x) {
53-
x <- ifelse(
54-
abs(x) < 1,
55-
round_half_up(x, 2), # if x < 1 then show 2dp
56-
ifelse(
57-
abs(x) < 100,
58-
round_half_up(x, 1), # if 1 =< x < 100 then 1dp
59-
round_half_up(x)
60-
)
61-
) # if 10 =< x then no decimal places
62-
63-
format(x, big.mark = ",")
51+
format_number_for_text <- function(number) {
52+
case_when(
53+
abs(number) < 1 ~ round_half_up(number, 2),
54+
abs(number) < 100 ~ round_half_up(number, 1),
55+
.default = round_half_up(number, 0)
56+
) |>
57+
format(big.mark = ",")
6458
}
6559

6660
# This will return the correct article depending on the (max 2-digit) number supplied

0 commit comments

Comments
 (0)