#' @title Install new packages
#'
#' @description Installs new packages to be used into the project.
#' @details Similar to library but if the package is not yet loaded will ask user whether or not they want to load the package.
#'
#' @param pkg package name
#' @export
#'
#' @examples \dontrun{use(ggplot2)}
use <- function(pkg) {
pkg <- as.character(substitute(pkg))
if (!suppressWarnings(require(pkg, character.only = TRUE))) {
msg <- paste("Install package", pkg, "(y/n)? ")
answer <- readline(msg)
answer <- substr(tolower(trimws(answer)), 1, 1)
if (answer == "y") {
install.packages(pkg)
require(pkg, character.only = TRUE)
} else {
msg <- paste("Package", pkg, "not loaded.")
message(msg)
}
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.