format_number <- function(x, prefix = NULL) {
x_sign <- signif(x, digits = 3)
x_fmt <- character(length = length(x))
x_fmt[x == 0] <- 0
x_fmt[abs(x) > 1e-2 & x != 0] <- prettyNum(
x = x_sign[abs(x) > 1e-2 & x != 0],
scientific = FALSE,
justify = "none",
digits = 3,
nsmall = 3,
trim = TRUE,
width = 3
)
x_fmt[abs(x) <= 1e-2 & x != 0] <- gsub(
"^(.*)e[+]*([-]*)0*(.*)$",
"\\1 %*% 10^\\2\\3",
prettyNum(
x = x_sign[abs(x) <= 1e-2 & x != 0],
scientific = TRUE,
justify = "none",
digits = 3,
nsmall = 3,
trim = TRUE,
width = 3
)
)
if (is.null(prefix)) {
gsub("^1 \\%\\*\\% ", "", x_fmt)
} else {
paste(prefix, "==", gsub("^1 \\%\\*\\% ", "", x_fmt))
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.