[R] Problem with environments
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Nov 23 05:09:14 CET 2007
Is this what you want?
i <- 3
f <- function() get("i", parent.frame())
f() # 3
On Nov 22, 2007 10:38 PM, Thomas L Jones, PhD <jones3745 at verizon.net> wrote:
> I have a numeric vector of length 1. I am trying to use it inside
> a function just by giving its name, rather than specifying it as
> an argument to the function. I am aware that there is an attach
> function which you need to call. The attach function will accept a
> list. However, I don't seem to be able to create the list properly. (Or
> should I use a frame instead?)
>
> Anyway, here is the output, starting with the load operation;
>
> ----------------------------------------------------------------------------
>
> > # function bar_driver
> >
> > free_driver <- function (){
> + i <- 25
> + attach_list <- list (i = i)
> + print1 ("attach_list = ", attach_list)
> + attach (attach_list)
> + free_test ()
> +
> + }
> > free_test <- function (){
> + print ("entering free_test")
> + print1 ("i")
> + return ()
> +
> + }
> >
>
> [1] "attach_list = "
> $i
> [1] 25
>
>
> The following object(s) are masked _by_ .GlobalEnv :
>
> i
>
> [1] "entering free_test"
> [1] "i"
> Error in print(value) : argument "value" is missing, with no default
> >
> ---------------------------------------------------------------------
>
> >From Tom:
>
> Question 1: Is the idea of using such a variable viable at all? In some
> communities, it would be called a "free variable."
> Question 2: Is the list correct? If not, how does one build it correctly?
> Question 3: How do you tell it NOT to mask the variable i by .GlobalEnv,
> whatever that is?
>
> Your advice?
>
> Tom Jones
> Thomas L. Jones, PhD, Computer Science
>
> ______________________________________________
> R-help at r-project.org 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.
>
More information about the R-help
mailing list