Benilton Carvalho
2007-Jun-06 12:54 UTC
[R] name of the variable that will contain the result of a function
Hi everyone, say I have a function called 'foo', which takes the argument arg1. Is there any mechanism that I can use to "learn" about the variable where foo(arg1) is going to be stored? For example: x <- foo(arg1) so, inside foo() I'd like to be able to get the string "x". if, foo(arg1) was used insted, I'd like to get NA. thank you very much, b -- Benilton Carvalho PhD Candidate Department of Biostatistics Bloomberg School of Public Health Johns Hopkins University bcarvalh at jhsph.edu
Thomas Lumley
2007-Jun-06 14:31 UTC
[R] name of the variable that will contain the result of a function
On Wed, 6 Jun 2007, Benilton Carvalho wrote:> Hi everyone, > > say I have a function called 'foo', which takes the argument arg1. > > Is there any mechanism that I can use to "learn" about the variable > where foo(arg1) is going to be stored?No. This information isn't available explicitly even at the C level.> For example: > > x <- foo(arg1) > > so, inside foo() I'd like to be able to get the string "x". > > if, > > foo(arg1) > > was used insted, I'd like to get NA.It could be much worse that this, for example, x[[7]][y][[4]] <- foo(arg1) w <- foo(arg2)+1 names(x)[foo(arg3)] <- foo(arg4) -thomas
Gabor Grothendieck
2007-Jun-07 01:34 UTC
[R] name of the variable that will contain the result of a function
Don't think you can do that but you could respecify your function
so that the assigned variable must appear as the first argument:
foo <- function(y, arg) {
y <- substitute(y)
if (is.name(y)) assign(deparse(y), arg+1, parent.frame())
else cat("not assigned\n")
invisible()
}
if (exists("zz")) rm(zz)
foo(zz, 3)
zz
foo(zz, 4)
zz
xx <- foo(zz, 99)
xx
zz
foo(0, 99)
foo(x+1, 99)
On 6/6/07, Benilton Carvalho <bcarvalh at jhsph.edu>
wrote:> Hi everyone,
>
> say I have a function called 'foo', which takes the argument arg1.
>
> Is there any mechanism that I can use to "learn" about the
variable
> where foo(arg1) is going to be stored?
>
> For example:
>
> x <- foo(arg1)
>
> so, inside foo() I'd like to be able to get the string "x".
>
> if,
>
> foo(arg1)
>
> was used insted, I'd like to get NA.
>
> thank you very much,
>
> b
>
>
>
>
>
>
> --
> Benilton Carvalho
> PhD Candidate
> Department of Biostatistics
> Bloomberg School of Public Health
> Johns Hopkins University
> bcarvalh at jhsph.edu
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>