load_packages <- function(char, quietly = FALSE) {
# Load packages passed in as names and install if not yet installed.
#
# Arguments:
# char {char} -- character vector containing names of packages to load
#
# Keyword Arguments:
# quietly {logical} -- if TRUE, suppress package startup messages and any warnings
#
# Returns:
# nothing
stopifnot(is.character(char))
# Load all specified packages
if (quietly) {
success = invisible(
suppressWarnings(
sapply(char, function(pkg) {
suppressPackageStartupMessages(require(pkg, character.only = TRUE))
})
)
)
} else {
success = invisible(
suppressWarnings(
sapply(char, function(pkg) {
require(pkg, character.only = TRUE)
})
)
)
}
# Install any packages that were not successfully loaded
if (any(!success)) {
install.packages(names(success)[!success])
# Load any packages that were just installed
if (quietly) {
invisible(
success = suppressWarnings(
sapply(names(success)[!success], function(pkg) {
suppressPackageStartupMessages(require(pkg, character.only = TRUE))
})
)
)
} else {
invisible(
success = suppressWarnings(
sapply(names(success)[!success], function(pkg) {
require(pkg, character.only = TRUE)
})
)
)
}
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.