Hi,
I'm writing a function that needs the input names (as characterstrings) as
part of the output.
With deparse(substitute( ) ) that works fine, until I replace all zeros with
0.001 (log is calculated at some time):
tf <- function(input) { input[input==0] <- 0.001 ;
deparse(substitute(input)) }
myguess <- 42
tf(myguess) # not "myguess", but "42"
Now when I extract the input names before replacing the zeros, this works:
tf <- function(input) { out <- deparse(substitute(input)) ;
input[input==0] <- 0.001 ; out }
tf(myguess) # correct: "myguess"
myguess <- 0 ; tf(myguess) # ditto
While I did find a workaround, I'm still wondering why this happens.
Any hints on where to start reading?
Thanks ahead,
Berry
PS: R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Windows 7 - Platform: x86_64-pc-mingw32/x64 (64-bit)
[[alternative HTML version deleted]]
On 13-01-09 5:03 AM, Berry Boessenkool wrote:> > > Hi, > > I'm writing a function that needs the input names (as characterstrings) as part of the output. > With deparse(substitute( ) ) that works fine, until I replace all zeros with 0.001 (log is calculated at some time): > > tf <- function(input) { input[input==0] <- 0.001 ; deparse(substitute(input)) } > myguess <- 42 > tf(myguess) # not "myguess", but "42" > > > Now when I extract the input names before replacing the zeros, this works: > > tf <- function(input) { out <- deparse(substitute(input)) ; input[input==0] <- 0.001 ; out } > tf(myguess) # correct: "myguess" > myguess <- 0 ; tf(myguess) # ditto > > > While I did find a workaround, I'm still wondering why this happens. > Any hints on where to start reading?Probably the R Language definition, section 2.1.8. The basic explanation for the behaviour you see is that deparse(substitute(input)) acts on the "input" promise object, looking at its expression slot. It's not doing any magic examination of the context in which it was originally defined. Once you modify it, it is no longer a promise, and so it has no expression slot. Duncan Murdoch
Possibly Parallel Threads
- How are R version types named ? Any convention (like Hurricanes etc)
- make check fails two tests on RHEL 6 build
- [BUG?] utils::prompt(name=f)
- Windows 7 R (32/64bit) running under cygwin: package not found
- problem with vignettes when S4 classes in packages overlap