Create a gist now

Instantly share code, notes, and snippets.

@klmr /rs.r
Last active Dec 17, 2017

What would you like to do?
A versatile re-source file function for R
#' (Re-)source parts of a file
#' \code{rs} loads, parses and executes parts of a file as if entered into the R
#' console directly (but without implicit echoing).
#' @param filename character string of the filename to read from. If missing,
#' use the last-read filename.
#' @param from first line to parse.
#' @param to last line to parse.
#' @return the value of the last evaluated expression in the source file.
#' @details If both \code{from} and \code{to} are missing, the default is to
#' read the whole file.
rs = local({
last_file = NULL
function (filename, from, to = if (missing(from)) -1 else from) {
if (missing(filename)) filename = last_file
stopifnot(! is.null(filename))
if (missing(from)) from = 1
source_lines = scan(
what = character(),
sep = '\n',
skip = from - 1, n = to - from + 1,
encoding = 'UTF-8',
quiet = TRUE,
blank.lines.skip = FALSE
result = withVisible(eval.parent(parse(text = source_lines)))
last_file <<- filename # Only save filename once successfully sourced.
if (result$visible) result$value else invisible(result$value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment