#' @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<-" "; lt_txt="<"; 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))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.