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

Duncan Murdoch murdoch.duncan at gmail.com
Fri Dec 13 02:27:59 CET 2013


On 13-12-12 7:57 PM, Henrik Bengtsson wrote:
> 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
>

Function definitions get srcrefs.

>
> Second, what is the proper way to drop that 'srcref' element in
> 'exprA'?  I can think of either
>
> exprC <- exprA
> exprC[[4L]] <- NULL

That should be best.

Duncan Murdoch

>
> 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
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list