R/examples.R

Defines functions run_example

Documented in run_example

#' Run a bundled example app
#'
#' @param name Example name. Call `run_example()` without arguments to list
#'   the available examples.
#' @return The example names (invisibly when an example was run).
#' @export
#' @examples
#' run_example()
run_example <- function(name = NULL) {
  dir <- system.file("examples", package = "termr")
  available <- sub("\\.R$", "", list.files(dir, pattern = "\\.R$"))
  if (is.null(name)) return(available)
  if (!(name %in% available)) {
    stop(sprintf("Unknown example \"%s\". Available: %s.", name, paste(available, collapse = ", ")),
         call. = FALSE)
  }
  source(file.path(dir, paste0(name, ".R")), local = new.env(parent = globalenv()))
  invisible(available)
}

Try the termr package in your browser

Any scripts or data that you put into this service are public.

termr documentation built on Oct. 21, 2026, 5:13 p.m.