R/txtRound.R

Defines functions txtRound

Documented in txtRound

#' @title A Rounding Function
#'
#' This function rounds a number and returns a string. 
#' @param html breaks and special characters given in html (default TRUE). 
#' @param p adds in a 'p' before number (default FALSE)
#' @keywords round string html
#' @export
#' @examples
#' txtRound(c(0.002, 2.3049))
#' ## [1] "0.002" "2.305"
#' 
#' txtRound(c(0.000002, 2.3049), p=T)
#' ## [1] "p < 0.001" "p = 2.305" 
#' 
#' txtRound(c(0.000002, 2.3049), p=T, html=F)
#' ## [1] "p < 0.001" "p = 2.305"

txtRound <- function(x, digits=3, html=T, pt=F, sci=F){
  tout <- if(sci){
    prettyNum(x, digits=digits)
  } else {
    formatC(x, digits=digits, format="f")
  }
  p_txt="p"; ps_txt<-s_txt<-"&nbsp;"; lt_txt="&lt;"; eq_txt="=";
  if(!html){
    ps_txt<-s_txt<-" "; lt_txt="<";
  }
  if(!pt) p_txt<-ps_txt<-eq_txt<-""; 
  ifelse(!sci & abs(x) < (1/10^digits), 
         paste0(p_txt, ps_txt, lt_txt, s_txt, 1/10^digits), 
         paste0(p_txt, ps_txt, eq_txt, ps_txt, tout))
}
jachan1/repFxns documentation built on July 20, 2021, 10:51 p.m.