[Rd] Proper way to drop 'srcref' from an expression created via substitute(function() ...)?

Henrik Bengtsson hb at biostat.ucsf.edu
Fri Dec 13 01:57:34 CET 2013


First, why does this expression have a 'srcref' element:

> exprA <- substitute(function(x) a*x, list(a=2))
> print(exprA)
function(x) 2 * x
> str(as.list(exprA))
List of 4
 $ : symbol function
 $ :Dotted pair list of 1
  ..$ x: symbol
 $ : language 2 * x
 $ :Class 'srcref'  atomic [1:8] 1 20 1 34 20 34 1 1
  .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile'
<environment: 0x00000000111feaf8>

whereas this does not:

> exprB <- substitute(a*x, list(a=2))
> print(exprB)
2 * x
> str(as.list(exprB))
List of 3
 $ : symbol *
 $ : num 2
 $ : symbol x


Second, what is the proper way to drop that 'srcref' element in
'exprA'?  I can think of either

exprC <- exprA
exprC[[4L]] <- NULL

or

exprC <- parse(text=deparse(exprA))

Anything better/safer?


BACKGROUND:
The reason for this is that I wish to create a function dynamically
via variable substitution such that when printed, the function
displays the substituted values, e.g.

> fcnA <- eval(exprA)
> print(fcnA)
function(x) a*x

versus

> fcnC <- eval(exprC)
> print(fcnC)
function(x) 2 * x

Thanks,

Henrik



More information about the R-devel mailing list