#' @title Set up output directories
#'
#' @description Set up \code{bookdown} manuscript
#' and non-manuscript figure and table output directories.
#' Creates directories, and saves objects specifying these directories
#' into global environment. See \code{prefix} for details
#' of objects saved.
#'
#' @param dir_base character. Path to base directory where objects
#' are to be saved, relative to \code{here::here()},
#' i.e. the base directory is effectively \code{here::here(dir_base)}.
#' Default is \code{"_book"}.
#' @param prefix character. Prefix for naming
#' objects specifying paths to particular directories.
#' Default is \code{dir_}. This results in objects
#' \code{dir_manu}, \code{dir_manu_n},
#' \code{dir_manu_fig}, \code{dir_manu_tbl},
#' \code{dir_manu_n_fig} and \code{dir_manu_n_tbl},
#' which provide the absolute paths.
#' Corresponding objects ending in "_rel" (e.g.
#' \code{dir_manu_rel}) specify the
#' relative paths (relative to \code{here::here()}).
#'
#' @return \code{invisible(TRUE)}. Side effects are
#' the creation of directories and saving
#' of objects specifying those directories
#' to global environment.
#'
#' @export
#'
#' @aliases set_up_output_dir
setup_output_dir <- function(dir_base = "_book",
prefix = "dir_") {
# auto-install here package if require
if (!requireNamespace("here", quietly = TRUE)) {
install.packages("here", quiet = TRUE)
}
# directories
# -------------
for (x in c("manu", "manu_n")) {
for (y in c("fig", "tbl")) {
dir_curr <- file.path(here::here(dir_base), x, y)
dir_curr <- normalizePath(dir_curr, mustWork = FALSE, winslash = "/")
if (!dir.exists(dir_curr)) dir.create(dir_curr, recursive = TRUE)
nm <- paste0(prefix, x, "_", y)
assign(
x = nm, value = dir_curr, envir = .GlobalEnv
)
len_dir_root <- nchar(normalizePath(here::here(), winslash = "/"))
dir_curr_rel <- substr(
dir_curr, start = len_dir_root + 2, stop = nchar(dir_curr)
)
nm <- paste0(prefix, x, "_", y, "_rel")
assign(
x = nm, value = dir_curr_rel, envir = .GlobalEnv
)
}
}
invisible(TRUE)
}
#' @rdname setup_output_dir
#' @export
set_up_output_dir <- setup_output_dir
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.