Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fmt prop bug #61

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions R/formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fmt_corr <- function(x, digits, output = NULL) {

#' @export
#' @rdname formatting
fmt_prop <- function(x, digits, fmt_small = TRUE, keep_zero = FALSE) {
fmt_prop <- function(x, digits, fmt_small = TRUE, keep_zero = FALSE, output) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output should have a default value. Probably output = NULL like in the fmt_corr() and fmt_minus() functions.

Should also check output using check_output()

x <- check_bound_real(x, name = "x", lb = 0, ub = 1)
digits <- check_pos_int(digits, name = "digits")

Expand All @@ -181,13 +181,15 @@ fmt_prop <- function(x, digits, fmt_small = TRUE, keep_zero = FALSE) {
small_text <- small %>%
fmt_digits(digits) %>%
fmt_leading_zero() %>%
paste0_after(.first = "<")
only_if(output == "latex")(paste0_after)(.first = "<") %>%
only_if(output == "html")(paste0_after)(.first = "$\\lt$")

large <- 1 - small
large_text <- large %>%
fmt_digits(digits) %>%
fmt_leading_zero() %>%
paste0_after(.first = ">")
only_if(output == "latex")(paste0_after)(.first = ">") %>%
only_if(output == "html")(paste0_after)(.first = "$\\gt$")

x_chr[x < small] <- small_text
x_chr[x > large] <- large_text
Expand All @@ -202,7 +204,7 @@ fmt_prop <- function(x, digits, fmt_small = TRUE, keep_zero = FALSE) {

#' @export
#' @rdname formatting
fmt_prop_pct <- function(x, digits = 0, fmt_small = TRUE) {
fmt_prop_pct <- function(x, digits = 0, fmt_small = TRUE, output) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provide default value, check provided value.

x <- check_bound_real(x, name = "x", lb = 0, ub = 1)
digits <- check_0_int(digits, name = "digits")

Expand All @@ -213,12 +215,14 @@ fmt_prop_pct <- function(x, digits = 0, fmt_small = TRUE) {
small <- 1 / (10 ^ digits)
small_text <- small %>%
fmt_digits(digits) %>%
paste0_after(.first = "<")
only_if(output == "latex")(paste0_after)(.first = "<") %>%
only_if(output == "html")(paste0_after)(.first = "$\\lt$")

large <- 100 - small
large_text <- large %>%
fmt_digits(digits) %>%
paste0_after(.first = ">")
only_if(output == "latex")(paste0_after)(.first = ">") %>%
only_if(output == "html")(paste0_after)(.first = "$\\gt$")

x_chr[round(x * 100, digits = digits) < small] <- small_text
x_chr[round(x * 100, digits = digits) > large] <- large_text
Expand Down
30 changes: 22 additions & 8 deletions R/table-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,32 @@ pad_prop <- function(x, digits, fmt_small = TRUE, keep_zero = FALSE,
digits <- check_pos_int(digits)
output <- check_output(output)
new_x <- fmt_prop(x, digits = digits, fmt_small = fmt_small,
keep_zero = keep_zero)
keep_zero = keep_zero, output = output)
new_x[is.na(new_x)] <- "NA"

if (any(stringr::str_detect(new_x, "^<|^>")) &
!all(stringr::str_detect(new_x, "^<|^>"))) {
pad <- ifelse(output == "latex", 4, 3)
new_x <- dplyr::case_when(stringr::str_detect(new_x, "^<|^>") ~
paste0(new_x, paste(rep("\\ ", pad),
collapse = "")),
TRUE ~ new_x)
if(is_html_output()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use is_html_output() here? In the function definition we allow the user to specify and output format. Shouldn't we respect that specification here?

if ((any(stringr::str_detect(new_x, "lt")) |
any(stringr::str_detect(new_x, "gt"))) &
!(all(stringr::str_detect(new_x, "lt") |
stringr::str_detect(new_x, "gt")))) {
Comment on lines +160 to +163
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we compress this? e.g.,

any(stringr::str_detect(new_x, "lt|gt") &
  !all(stringr::str_detect(new_x, "lt|gt")

pad <- ifelse(output == "latex", 4, 3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently in an if (is_html_output()), so this should only be for HTML, no?

I would recommend moving this line of code outside of the HTML/LaTeX if statements, then you just have the pad variable defined when you go into the if.

new_x <- dplyr::case_when(stringr::str_detect(new_x, "^<|^>") ~
paste0(new_x, paste(rep("\\ ", pad),
collapse = "")),
TRUE ~ new_x)
}
} else if (is_latex_output()) {
if (any(stringr::str_detect(new_x, "^<|^>")) &
!all(stringr::str_detect(new_x, "^<|^>"))) {
pad <- ifelse(output == "latex", 4, 3)
new_x <- dplyr::case_when(stringr::str_detect(new_x, "^<|^>") ~
paste0(new_x, paste(rep("\\ ", pad),
collapse = "")),
TRUE ~ new_x)
}
}


if (any(x == 1, na.rm = TRUE)) {
new_x <- dplyr::case_when(stringr::str_detect(new_x, "^1\\.") ~ new_x,
TRUE ~ paste0(paste(rep("\\ ", 2), collapse = ""),
Expand Down
57 changes: 48 additions & 9 deletions tests/testthat/test-formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,31 @@ test_that("fmt_prop_pct", {
expect_match(err$message, "between 0 and 1")

rand <- runif(5)
expect_equal(fmt_prop_pct(rand, fmt_small = FALSE),
expect_equal(fmt_prop_pct(rand, fmt_small = FALSE, output = "latex"),
sprintf("%0.0f", rand * 100))
expect_equal(fmt_prop_pct(rand, digits = 2, fmt_small = FALSE),
expect_equal(fmt_prop_pct(rand, fmt_small = FALSE, output = "html"),
sprintf("%0.0f", rand * 100))
expect_equal(fmt_prop_pct(rand, digits = 2, fmt_small = FALSE,
output = "latex"),
sprintf("%0.2f", rand * 100))
expect_equal(fmt_prop_pct(rand, digits = 2, fmt_small = FALSE,
output = "html"),
sprintf("%0.2f", rand * 100))

expect_equal(fmt_prop_pct(c(0.012, 0.009, 0.004, 0.989, 0.994, 0.997)),
expect_equal(fmt_prop_pct(c(0.012, 0.009, 0.004, 0.989, 0.994, 0.997),
output = "latex"),
c("1", "1", "<1", "99", "99", ">99"))
expect_equal(fmt_prop_pct(c(0.012, 0.009, 0.004, 0.989, 0.994, 0.997),
output = "html"),
c("1", "1", "$\\lt$1", "99", "99", "$\\gt$99"))
expect_equal(fmt_prop_pct(c(0.829, 0.080, NA_real_, 0.313, 0.002, 0.0004,
0.998, 0.9999), digits = 1),
0.998, 0.9999), digits = 1, output = "latex"),
c("82.9", "8.0", NA_character_, "31.3", "0.2", "<0.1", "99.8",
">99.9"))
expect_equal(fmt_prop_pct(c(0.829, 0.080, NA_real_, 0.313, 0.002, 0.0004,
0.998, 0.9999), digits = 1, output = "html"),
c("82.9", "8.0", NA_character_, "31.3", "0.2", "$\\lt$0.1",
"99.8", "$\\gt$99.9"))
})

test_that("fmt_leading_zero", {
Expand Down Expand Up @@ -145,27 +159,52 @@ test_that("fmt_prop", {
expect_match(err$message, "greater than zero")

check1 <- test %>%
fmt_prop(digits = 3)
fmt_prop(digits = 3, output = "latex")
expect_equal(check1, c(".853", ">.999", ".855", NA, ".690",
".001", ".129", ".869", ".169", "<.001"))

check1_2 <- test %>%
fmt_prop(digits = 3, fmt_small = FALSE)
fmt_prop(digits = 3, fmt_small = FALSE, output = "latex")
expect_equal(check1_2, c(".853", "1.000", ".855", NA, ".690",
".001", ".129", ".869", ".169", ".000"))

check1_3 <- test %>%
fmt_prop(digits = 3, output = "html")
expect_equal(check1_3, c(".853", "$\\gt$.999", ".855", NA, ".690",
".001", ".129", ".869", ".169", "$\\lt$.001"))

check1_4 <- test %>%
fmt_prop(digits = 3, fmt_small = FALSE, output = "html")
expect_equal(check1_4, c(".853", "1.000", ".855", NA, ".690",
".001", ".129", ".869", ".169", ".000"))

check2 <- test %>%
fmt_prop(digits = 2)
fmt_prop(digits = 2, output = "latex")
expect_equal(check2, c(".85", ">.99", ".85", NA, ".69",
"<.01", ".13", ".87", ".17", "<.01"))

check2_2 <- test %>%
fmt_prop(digits = 2, fmt_small = FALSE)
fmt_prop(digits = 2, fmt_small = FALSE, output = "latex")
expect_equal(check2_2, c(".85", "1.00", ".85", NA, ".69",
".00", ".13", ".87", ".17", ".00"))

check2_3 <- test %>%
fmt_prop(digits = 2, output = "html")
expect_equal(check2_3, c(".85", "$\\gt$.99", ".85", NA, ".69",
"$\\lt$.01", ".13", ".87", ".17", "$\\lt$.01"))

check2_4 <- test %>%
fmt_prop(digits = 2, fmt_small = FALSE, output = "html")
expect_equal(check2_4, c(".85", "1.00", ".85", NA, ".69",
".00", ".13", ".87", ".17", ".00"))

check3 <- test %>%
fmt_prop(digits = 2, fmt_small = FALSE)
fmt_prop(digits = 2, fmt_small = FALSE, output = "latex")
expect_equal(check3, c(".85", "1.00", ".85", NA, ".69",
".00", ".13", ".87", ".17", ".00"))

check3_2 <- test %>%
fmt_prop(digits = 2, fmt_small = FALSE, output = "html")
expect_equal(check3, c(".85", "1.00", ".85", NA, ".69",
".00", ".13", ".87", ".17", ".00"))
})