#' Dynamic examples with roxygen
#'
#' Create dynamic manual page examples, that are commented out if some
#' condition does not hold, e.g. a dependent package is not installed, or
#' the computer is offline.
#'
#' @docType package
#' @name dynex
"_PACKAGE"
#' @export
#' @importFrom roxygen2 roxy_tag_parse tag_examples
roxy_tag_parse.roxy_tag_dynex <- function(x) {
x <- tag_examples(x)
mch <- regexpr("^([^\n]*)", x$val, perl = TRUE)
cond <- substr(x$val, 1, attr(mch,"capture.length"))
rest <- substr(x$val, attr(mch,"capture.length") + 2L, nchar(x$val))
restt <- sub("\n+$", "", rest)
x$val[] <- paste0(
"\\dontshow{ if (", cond, ") \\{ }\n",
restt,
"\\dontshow{ \\} }"
)
x
}
#' @export
#' @importFrom roxygen2 roxy_tag_rd rd_section
roxy_tag_rd.roxy_tag_dynex <- function(x, base_path, env) {
rd_section("examples", x$val)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.