File: wstring.R

package info (click to toggle)
r-cran-r.rsp 0.46.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,472 kB
  • sloc: javascript: 612; tcl: 304; sh: 18; makefile: 16
file content (36 lines) | stat: -rw-r--r-- 952 bytes parent folder | download
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
setMethodS3("wstring", "default", function(..., sep="", envir=parent.frame()) {
  s <- paste(..., sep=sep)

  # Nothing to do?
  if (length(s) == 0L) return(s)
  if (length(s) > 1L) {
    Recall <- sys.function();  # base::Recall() does not work with *apply()
    return(sapply(s, FUN=Recall, envir=envir))
  }

  # Nothing to do?
  if (regexpr("{{", s, fixed=TRUE) == -1L) {
    return(s)
  }

  bfr <- NULL
  pattern <- "{{(.*?)}}"
  while ((pos <- regexpr(pattern, s, perl=TRUE)) != -1L) {
    # Parse
    len <- attr(pos, "match.length")
    head <- substring(s, first=1L, last=pos-1L)
    code <- substring(s, first=pos+2L, last=pos+len-3L)
    s <- substring(s, first=pos+len)

    # Evaluate
    expr <- base::parse(text=code)
    value <- eval(expr, envir = envir, enclos = baseenv())
    value <- as.character(value)

    bfr <- c(bfr, head, value)
  }
  bfr <- c(bfr, s)
  bfr <- paste(bfr, collapse="")

  bfr
}, protected=TRUE) # wstring()