vignettes/_common.R

# R6P ---------------------------------------------------------------------
suppressPackageStartupMessages(
    withr::with_dir(
        usethis::proj_get(),
        pkgload::load_all(export_all = !FALSE, helpers = FALSE, quiet = TRUE, warn_conflicts = FALSE)
    )
)

# global options ----------------------------------------------------------
options(tidyverse.quiet = TRUE)

# knitr -------------------------------------------------------------------
knitr::opts_knit$set(
    root.dir = usethis::proj_get()
)

knitr::opts_chunk$set(
    collapse = TRUE,
    out.width = '100%',
    echo = FALSE,
    results = "markup",
    message = FALSE,
    warning = FALSE,
    cache = !TRUE,
    comment = "#>",
    fig.retina = 0.8, # figures are either vectors or 300 dpi diagrams
    dpi = 300,
    out.width = "70%",
    fig.align = 'center',
    fig.width = 6,
    fig.asp = 0.618,  # 1 / phi
    fig.show = "hold",
    eval.after = 'fig.cap' # so captions can use link to demos
)

knitr::knit_hooks$set(
    error = function(x, options) {
        paste('\n\n<div class="alert alert-danger">',
              x |>
                  stringr::str_replace_all('^.*:', '**Caution:**') |>
                  stringr::str_replace_all('#> ', '\n'),
              '</div>', sep = '\n')
    },
    warning = function(x, options) {
        paste('\n\n<div class="alert alert-warning">',
              x |>
                  stringr::str_replace_all('##', '\n') |>
                  stringr::str_replace_all('^#>\ Warning:', '**Note:**') |>
                  stringr::str_remove_all("#>"),
              '</div>', sep = '\n')
    },
    message = function(x, options) {
        paste('\n\n<div class="alert alert-info">',
              gsub('##|#>', '\n', paste("**Tip:**", x)),
              '</div>', sep = '\n')
    }
)

# rmarkdown ---------------------------------------------------------------
kable <- knitr::kable


# regex -------------------------------------------------------------------
discard_comments <- function(string) return(string[!stringr::str_detect(string, "^#")])
discard_null <- function(string) string[!stringr::str_detect(string, "^NULL")]
discard_empty_lines <- function(string) string[nchar(string)>0]


# events ------------------------------------------------------------------
events <- new.env()
events$stop_not_useful <- function(name) stop(paste(name,
"is designed for demonstration purposes. Instead of directly using the design pattern as it appears in the package, you'd have to adjust the source code to the problem you are trying to solve."
))
tidylab/R6P documentation built on Dec. 23, 2024, 9:22 a.m.