Skip to content

Commit

Permalink
Break out inline_hook into simpler function
Browse files Browse the repository at this point in the history
  • Loading branch information
kelly-sovacool committed May 19, 2021
1 parent 7eb9ed5 commit f55e436
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
36 changes: 26 additions & 10 deletions R/rmd_helpers.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
#' Inline hook for knitr to paste human-readable numbers and nice lists.
#'
#' @param x just about anything
#'
#' @return a string where each element in `x` is separated by a comma and numbers
#' are in a human-readable format.
#' @export
#' @author Pat Schloss \email{pschloss@@umich.edu}
#' @author Kelly Sovacool \email{sovacool@@umich.edu}
#'
#' @examples
#' inline_hook(c(1.2993992, 0.03, 1000))
#' inline_hook(c('cats', 'dogs'))
inline_hook <- function(x) {
x_formatted <- purrr::map_chr(x, format_number)
return(paste_oxford_list(x_formatted))
}

#' Create a prose string from a list or vector
#'
#' The word 'and' is inserted before the last element and an Oxford comma is used.
#' Numerics are processed with `inline_hook()` for rounding.
#' Numerics are processed with `format_number()` for rounding.
#'
#' @param x a list or vector
#'
Expand All @@ -15,7 +32,6 @@
#' paste_oxford_list(1:3)
#' paste_oxford_list(c("cats", "dogs", "turtles"))
paste_oxford_list <- function(x) {
x <- purrr::map_chr(x, inline_hook)
if (length(x) < 2) {
prose <- as.character(x)
} else if (length(x) == 2) {
Expand Down Expand Up @@ -43,7 +59,7 @@ is_nearly_whole <- function(x) {
abs(x - round(x)) < .Machine$double.eps^0.5
}

#' Inline hook for knitr to paste human-readable numbers.
#' Format human-readable numbers.
#'
#' Pastes formatted `x` if numeric, otherwise `x` unmodified.
#' Circumvents R's automatic scientific notation.
Expand All @@ -64,12 +80,12 @@ is_nearly_whole <- function(x) {
#' @author Kelly Sovacool \email{sovacool@@umich.edu}
#'
#' @examples
#' inline_hook(0.0256)
#' inline_hook(.Machine$double.eps^0.5)
#' inline_hook(100000.08)
#' inline_hook(1.00000000000000000001)
#' inline_hook("this is a string")
inline_hook <- function(x, nsmall = 1, signif_precise = 2) {
#' format_number(0.0256)
#' format_number(.Machine$double.eps^0.5)
#' format_number(100000.08)
#' format_number(1.00000000000000000001)
#' format_number("this is a string")
format_number <- function(x, nsmall = 1, signif_precise = 2) {
if (is.list(x)) {
x <- unlist(x)
}
Expand Down Expand Up @@ -109,5 +125,5 @@ set_knitr_opts <- function() {
warning = FALSE,
cache = FALSE
)
knitr::knit_hooks$set(inline = paste_oxford_list)
knitr::knit_hooks$set(inline = inline_hook)
}
26 changes: 15 additions & 11 deletions tests/testthat/test-rmd_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ test_that("paste_oxford_list() works for vectors & lists", {
expect_equal(paste_oxford_list(as.list(1:3)), "1, 2, and 3")
expect_equal(paste_oxford_list(1:2), "1 and 2")
expect_equal(paste_oxford_list(1), "1")
expect_equal(
paste_oxford_list(seq(0, 3 * .Machine$double.eps^0.5, .Machine$double.eps^0.5)),
"0, 0.000000015, 0.00000003, and 0.000000045"
)
})

test_that("is_nearly_whole() works", {
Expand All @@ -17,14 +13,22 @@ test_that("is_nearly_whole() works", {
expect_false(is_nearly_whole(2100.05))
})

test_that("inline_hook() works for small numbers & strings", {
expect_equal(inline_hook(0.02), "0.02")
expect_equal(inline_hook(.Machine$double.eps^0.5), "0.000000015")
expect_equal(inline_hook(1000), "1,000")
expect_equal(inline_hook(1000.06), "1,000.1")
expect_equal(inline_hook(TRUE), "TRUE")
expect_equal(inline_hook("this is a string"), "this is a string")
test_that("format_number() works for numbers & strings", {
expect_equal(format_number(0.02), "0.02")
expect_equal(format_number(.Machine$double.eps^0.5), "0.000000015")
expect_equal(format_number(1000), "1,000")
expect_equal(format_number(1000.06), "1,000.1")
expect_equal(format_number(TRUE), "TRUE")
expect_equal(format_number("this is a string"), "this is a string")
})

test_that("inline_hook() uses paste_oxford_list() and format_number() correctly", {
expect_equal(
inline_hook(seq(0, 3 * .Machine$double.eps^0.5, .Machine$double.eps^0.5)),
"0, 0.000000015, 0.00000003, and 0.000000045"
)
})

test_that("set_knitr_opts() works", {
chunk <- list(
eval = TRUE, echo = FALSE, results = "markup", tidy = TRUE,
Expand Down

0 comments on commit f55e436

Please sign in to comment.