#' nice printing of a string in an R notebook
#' use `with.gt = FALSE` when you want to nicely print multiple lines
#' ('gt' is the '>' symbol)
#' @export
pretty.print.string = function(string, with.gt = TRUE) {
if (with.gt)
cat(paste0("> ", string))
else
cat(string)
}
#' prints a bold string only when `html.output` is enabled. Otherwise,
#' prints a normal string
#' @export
pretty.print.bold.string =
function(string, with.gt = TRUE, html.output = TRUE) {
if (html.output) {
bold.string = paste0("<b>", string, "</b>")
if (with.gt)
cat(paste0("> ", bold.string))
else
cat(bold.string)
} else {
pretty.print.string(string, with.gt = with.gt)
}
}
#' @export
print.empty.line = function(html.output = FALSE) {
if (html.output)
cat("<br/>")
else
cat("\n")
}
#' `vector.names.str` tell us what `names(vec)` actually is, to put it on
#' the print message
#' @export
pretty.print.vector.names = function(vec, vector.names.str = "nodes",
seperator = ", ", with.gt = TRUE) {
if (length(vec) == 1) {
vector.names.str = substr(vector.names.str, start = 1,
stop = nchar(vector.names.str) - 1)
}
pretty.print.string(paste0(length(vec), " ", vector.names.str, ": ",
paste0(names(vec), collapse = seperator)), with.gt)
}
#' `vector.values.str` tell us what the `vec` values are, to put it on
#' the print message
#' @export
pretty.print.vector.values = function(vec, vector.values.str = "nodes",
seperator = ", ", with.gt = TRUE) {
if (length(vec) == 1) {
vector.values.str = substr(vector.values.str, start = 1,
stop = nchar(vector.values.str) - 1)
}
pretty.print.string(paste0(length(vec), " ", vector.values.str, ": ",
paste0(vec, collapse = seperator)), with.gt)
}
#' vector `vec` has to have `names(vec)` non-empty
#' `n` is the (integer) number of elements you want to print
#' @export
pretty.print.vector.names.and.values = function(vec, n = -1) {
len = length(vec)
stopifnot(len > 0)
# print all elements by default
if (n == -1) n = len
vec.names = names(vec)
if (len == 1) {
pretty.print.name.and.value(vec.names, vec, with.gt = TRUE, with.comma = FALSE)
} else {
# limit elements to show
if (n >= 1 & n < len)
last.index = n
else
last.index = len
for (index in 1:last.index) {
name = vec.names[index]
value = vec[index]
if (index == 1 & index != last.index)
pretty.print.name.and.value(name, value, with.gt = TRUE, with.comma = TRUE)
if (index == 1 & index == last.index)
pretty.print.name.and.value(name, value, with.gt = TRUE, with.comma = FALSE)
if (index != 1 & index != last.index)
pretty.print.name.and.value(name, value)
if (index != 1 & index == last.index)
pretty.print.name.and.value(name, value, with.comma = FALSE)
}
}
}
#' @export
pretty.print.name.and.value =
function(name, value, with.gt = FALSE, with.comma = TRUE) {
if (with.comma) {
pretty.print.string(string = paste0(name, ": ", value, ", "),
with.gt = with.gt)
}
else
pretty.print.string(string = paste0(name, ": ", value), with.gt = with.gt)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.