From murdoch.duncan at gmail.com  Fri Dec 13 02:27:59 2013
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Thu, 12 Dec 2013 20:27:59 -0500
Subject: [Rd] Proper way to drop 'srcref' from an expression created via
 substitute(function() ...)?
In-Reply-To: <CAFDcVCRe7SF5bb_mWkOgXD4jB4iZ7sYUzCKvYqUBb0qYmM=wOA@mail.gmail.com>
References: <CAFDcVCRe7SF5bb_mWkOgXD4jB4iZ7sYUzCKvYqUBb0qYmM=wOA@mail.gmail.com>
Message-ID: <52AA629F.3080500@gmail.com>

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
>



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