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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
strmacro <- function(..., expr, strexpr)
{
if(!missing(expr))
strexpr <- deparse(substitute(expr))
a <- substitute(list(...))[-1]
nn <- names(a)
if (is.null(nn))
nn <- rep("", length(a))
for(i in 1:length(a))
{
if (nn[i] == "")
{
nn[i] <- paste(a[[i]])
msg <- paste(a[[i]], "not supplied")
a[[i]] <- substitute(stop(foo),
list(foo = msg))
}
else
{
a[[i]] <- a[[i]]
}
}
names(a) <- nn
a <- as.list(a)
## this is where the work is done
ff <-
function(...)
{
## build replacement list
reptab <- a # copy defaults first
reptab$"..." <- NULL
args <- match.call(expand.dots=TRUE)[-1]
for(item in names(args))
reptab[[item]] <- args[[item]]
## do the replacements
body <- strexpr
for(i in 1:length(reptab))
{
pattern <- paste("\\b",
names(reptab)[i],
"\\b",sep='')
value <- reptab[[i]]
if(missing(value))
value <- ""
body <- gsub(pattern,
value,
body)
}
fun <- parse(text=body)
eval(fun, parent.frame())
}
## add the argument list
formals(ff) <- a
## create a fake source attribute
mm <- match.call()
mm$expr <- NULL
mm[[1]] <- as.name("macro")
attr(ff, "source") <- c(deparse(mm), strexpr)
## return the 'macro'
ff
}
|