Henrik Bengtsson
2013-Dec-13 00:57 UTC
[Rd] Proper way to drop 'srcref' from an expression created via substitute(function() ...)?
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
Duncan Murdoch
2013-Dec-13 01:27 UTC
[Rd] Proper way to drop 'srcref' from an expression created via substitute(function() ...)?
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]] <- NULLThat 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 >
Maybe Matching Threads
- Why is srcref of length 6 and not 4 ?
- Extracting srcref for S4 methods
- sys.call() 's srcref doesn't match the language
- incomplete results from as.character.srcref() in some cases involving quote()
- incomplete results from as.character.srcref() in some cases involving quote()