From hb at biostat.ucsf.edu  Fri Dec 13 02:30:21 2013
From: hb at biostat.ucsf.edu (Henrik Bengtsson)
Date: Thu, 12 Dec 2013 17:30:21 -0800
Subject: [Rd] Proper way to drop 'srcref' from an expression created via
 substitute(function() ...)?
In-Reply-To: <52AA629F.3080500@gmail.com>
References: <CAFDcVCRe7SF5bb_mWkOgXD4jB4iZ7sYUzCKvYqUBb0qYmM=wOA@mail.gmail.com>
	<52AA629F.3080500@gmail.com>
Message-ID: <CAFDcVCT+0_UDMcorteNJPv=0DSH2+W-dh8DqPNA2DMfU-p8Dzw@mail.gmail.com>

Thanks. /Henrik

On Thu, Dec 12, 2013 at 5:27 PM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
> 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() ...)?
Henrik Bengtsson