1
setMethodS3("sourceWithTrim", "default", function(code, echo=TRUE, prompt=getOption('prompt'), ..., capture=TRUE, trim=TRUE) {
2 0
  con <- textConnection(code)
3 0
  on.exit({
4 0
    close(con)
5 0
  }, add=TRUE)
6

7 0
  file <- rawConnection(raw(0L), open="w")
8 0
  on.exit({
9 0
    if (!is.null(file)) close(file)
10 0
  }, add=TRUE)
11 0
  magicPrompt <- "<RSP-MAGIC-RSP>"
12 0
  capture.output(source(con, echo=echo, prompt.echo=magicPrompt), file=file)
13 0
  bfr <- rawToChar(rawConnectionValue(file))
14 0
  close(file); file <- NULL
15 0
  bfr <- unlist(strsplit(bfr, split="\n", fixed=TRUE), use.names=FALSE)
16

17 0
  if (trim) {
18 0
    pattern <- sprintf("^%s", magicPrompt)
19 0
    idxs <- grep(pattern, bfr)
20 0
    if (length(idxs) > 0L) {
21 0
      drop <- (idxs-1L)[nchar(bfr[idxs-1L]) == 0L]
22 0
      if (length(drop) > 0L) bfr <- bfr[-drop]
23 0
      bfr <- gsub(pattern, prompt, bfr)
24
    }
25
  }
26

27 0
  if (!capture) {
28 0
    cat(bfr, sep="\n")
29
  }
30

31 0
  invisible(bfr)
32
}) # sourceWithTrim()

Read our documentation on viewing source code .

Loading