-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdev-example.r
57 lines (51 loc) · 1.7 KB
/
dev-example.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#' Run a examples for an in-development function.
#'
#' `dev_example` is a replacement for `example`. `run_example`
#' is a low-level function that takes a path to an Rd file.
#'
#' @inheritParams run_examples
#' @param topic Name or topic (or name of Rd) file to run examples for
#' @param quiet If `TRUE`, does not echo code to console.
#' @export
#' @family example functions
#' @examples
#' \dontrun{
#' # Runs installed example:
#' library("ggplot2")
#' example("ggplot")
#'
#' # Runs develoment example:
#' dev_example("ggplot")
#' }
dev_example <- function(topic, quiet = FALSE) {
topic <- dev_help(topic)
load_all(topic$pkg, quiet = quiet)
run_example(topic$path, quiet = quiet)
}
#' @rdname dev_example
#' @export
#' @param path Path to `.Rd` file
#' @param test if `TRUE`, code in \code{\\donttest{}} will be commented
#' out. If `FALSE`, code in \code{\\testonly{}} will be commented out. This
#' parameter is only used in R 3.2 and greater.
#' @param run if `TRUE`, code in \code{\\dontrun{}} will be commented
#' out.
#' @param env Environment in which code will be run.
run_example <- function(path, test = FALSE, run = FALSE,
env = new.env(parent = globalenv()),
quiet = FALSE) {
if (!file.exists(path)) {
stop("'", path, "' does not exist", call. = FALSE)
}
tmp <- tempfile(fileext = ".R")
if (getRversion() < "3.2") {
# R 3.1 and earlier did not have commentDonttest
tools::Rd2ex(path, out = tmp, commentDontrun = !run)
} else {
tools::Rd2ex(path, out = tmp, commentDontrun = !run, commentDonttest = !test)
}
if (file.exists(tmp)) {
source(tmp, echo = !quiet, local = env, max.deparse.length = Inf)
}
invisible(env)
}