Skip to content

Commit

Permalink
inline_hook() [resolves #19]
Browse files Browse the repository at this point in the history
  • Loading branch information
kelly-sovacool committed Sep 29, 2020
1 parent f35d508 commit b606461
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions R/rmd_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,31 @@ paste_oxford_list <- function(x) {
}
return(prose)
}

#' Inline hook for knitr to paste human-readable numbers
#'
#' Pastes rounded `x` if numeric, otherwise `x` unmodified.
#'
#' @param x inline code
#'
#' @return rounded `x` if numeric, otherwise `x` unmodified.
#' @export
#' @author Pat Schloss \email{pschloss@@umich.edu}
#' @author Kelly Sovacool \email{sovacool@@umich.edu}
#'
#' @examples
#' inline_hook(0.02)
#' inline_hook(.Machine$double.eps^0.5)
#' inline_hook('this is a string')
inline_hook <- function(x){
print(x)
if(is.numeric(x)) {
if(abs(x - round(x)) < .Machine$double.eps^0.5){
paste(format(x,big.mark=',', digits=0, scientific=FALSE))
} else {
paste(format(x,big.mark=',', digits=2, nsmall=2, scientific=FALSE))
}
} else {
paste(x)
}
}

0 comments on commit b606461

Please sign in to comment.