Hi, Is it possible to modify an object in the parent.env (as opposed to re-bind)? Here is what I tried:> x = 1:3# try to modify the first element of x from within a new environment> local(get("x", parent.env(environment()))[1] <- NA)Error in eval(expr, envir, enclos) : Target of assignment expands to non-language object # On the other hand retrieval works just fine> local(get("x", parent.env(environment()))[1])[1] 1 Thanks, Vadim
This isn't an environment problem. Assigning something to a get call doesn't make any sense. Use assign.> a <- 5 > get("a") <- 10Error: couldn't find function "get<-" And from the ?assign help page, you can pick what environment you want to make the assignment. Just pick the parent environment. -----Original Message----- From: Vadim Ogranovich [mailto:vograno at evafunds.com] Sent: Tuesday, March 08, 2005 6:36 PM To: r-help at stat.math.ethz.ch Subject: [R] how modify object in parent.env Hi, Is it possible to modify an object in the parent.env (as opposed to re-bind)? Here is what I tried:> x = 1:3# try to modify the first element of x from within a new environment> local(get("x", parent.env(environment()))[1] <- NA)Error in eval(expr, envir, enclos) : Target of assignment expands to non-language object # On the other hand retrieval works just fine> local(get("x", parent.env(environment()))[1])[1] 1 Thanks, Vadim ______________________________________________ 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
Assign() re-binds the value, not modifies it (the latter is what I needed)> -----Original Message----- > From: McGehee, Robert [mailto:Robert.McGehee at geodecapital.com] > Sent: Tuesday, March 08, 2005 3:48 PM > To: Vadim Ogranovich; r-help at stat.math.ethz.ch > Subject: RE: [R] how modify object in parent.env > > This isn't an environment problem. Assigning something to a > get call doesn't make any sense. Use assign. > > > a <- 5 > > get("a") <- 10 > Error: couldn't find function "get<-" > > And from the ?assign help page, you can pick what environment > you want to make the assignment. Just pick the parent environment. > > > -----Original Message----- > From: Vadim Ogranovich [mailto:vograno at evafunds.com] > Sent: Tuesday, March 08, 2005 6:36 PM > To: r-help at stat.math.ethz.ch > Subject: [R] how modify object in parent.env > > > Hi, > > Is it possible to modify an object in the parent.env (as opposed to > re-bind)? Here is what I tried: > > > x = 1:3 > # try to modify the first element of x from within a new environment > > local(get("x", parent.env(environment()))[1] <- NA) > Error in eval(expr, envir, enclos) : Target of assignment expands to > non-language object > > # On the other hand retrieval works just fine > > local(get("x", parent.env(environment()))[1]) > [1] 1 > > Thanks, > Vadim > > ______________________________________________ > 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 >
On Tue, 2005-03-08 at 15:36 -0800, Vadim Ogranovich wrote:> Hi, > > Is it possible to modify an object in the parent.env (as opposed to > re-bind)? Here is what I tried: > > > x = 1:3 > # try to modify the first element of x from within a new environment > > local(get("x", parent.env(environment()))[1] <- NA) > Error in eval(expr, envir, enclos) : Target of assignment expands to > non-language object > > # On the other hand retrieval works just fine > > local(get("x", parent.env(environment()))[1]) > [1] 1You could try this:> x <- 1:3mod.x <- function(x) { eval.parent(substitute(x[1] <- NA)) }> x[1] 1 2 3> mod.x(x)> x[1] NA 2 3 See ?eval.parent for more information and variations on this. HTH, Marc Schwartz
Thank you to Gabor and Mark Schwartz for the answers. Both of them
solved the problem I posted, but my actual problem, as I now see, is a
little bit more involved. Let me try again.
I have a vector 'x'. I want to compute its entries in a loop (yes, I
know...). Say
x = seq(3)
for (i in seq(length(x)) {
x0 = someValue
x[i] = x0
}
There are two problems with the above code:
1. x0 pollutes the global envirnoment (not to mention possible
over-write of an existing x0). Therefore I thought I'd wrap it with
local().
2. x0 is not a good name from a readability perspective. I'd rather call
it x to emphasize it's an entry in an outer vector 'x'. (In this
small
example it doesn't really matter, but I have much more involved scripts
where consistent naming is important)
Gabor's solution solves 1 but not 2. Maybe there is a simple way around
this restriction?
Thanks,
Vadim
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gabor
> Grothendieck
> Sent: Tuesday, March 08, 2005 4:06 PM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] how modify object in parent.env
>
>
> You can use "<<-" like this:
>
> x <- 1:3
> local(x[1] <<- x[1]+1)
>
> Vadim Ogranovich <vograno <at> evafunds.com> writes:
>
> :
> : Assign() re-binds the value, not modifies it (the latter is what I
> : needed)
> :
> : > -----Original Message-----
> : > From: McGehee, Robert [mailto:Robert.McGehee <at>
> geodecapital.com]
> : > Sent: Tuesday, March 08, 2005 3:48 PM
> : > To: Vadim Ogranovich; r-help <at> stat.math.ethz.ch
> : > Subject: RE: [R] how modify object in parent.env
> : >
> : > This isn't an environment problem. Assigning something to a
> : > get call doesn't make any sense. Use assign.
> : >
> : > > a <- 5
> : > > get("a") <- 10
> : > Error: couldn't find function "get<-"
> : >
> : > And from the ?assign help page, you can pick what environment
> : > you want to make the assignment. Just pick the parent environment.
> : >
> : >
> : > -----Original Message-----
> : > From: Vadim Ogranovich [mailto:vograno <at> evafunds.com]
> : > Sent: Tuesday, March 08, 2005 6:36 PM
> : > To: r-help <at> stat.math.ethz.ch
> : > Subject: [R] how modify object in parent.env
> : >
> : >
> : > Hi,
> : >
> : > Is it possible to modify an object in the parent.env (as
> opposed to
> : > re-bind)? Here is what I tried:
> : >
> : > > x = 1:3
> : > # try to modify the first element of x from within a new
> environment
> : > > local(get("x", parent.env(environment()))[1] <-
NA)
> : > Error in eval(expr, envir, enclos) : Target of assignment
> expands to
> : > non-language object
> : >
> : > # On the other hand retrieval works just fine
> : > > local(get("x", parent.env(environment()))[1])
> : > [1] 1
> : >
> : > Thanks,
> : > Vadim
> : >
> : > ______________________________________________
> : > 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
> : >
> :
> : ______________________________________________
> : 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
> :
> :
>
> ______________________________________________
> 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
>