Dear all, I have a function "fn" with its own environment, i.e. env <- environment(fn) and env is not .GlobalEnv. And another function getValue <- function(x) environment(x)$mylist which returns the list object "mylist" which is in "env". If I want to modify "mylist", I could write 'getValue<-' <- function(x, value) { environment(x)$mylist <- value} which gives not the desired result, e.g. getValue(fn)[[1]] <- 3 will set the first list entry of "mylist" to 3. But then "fn" will also be 3! environment(fn)$mylist[[1]] <- 3 does set the list entry correctly while keeping "fn". What's the difference? Cheers, Daniel
On 07/02/2016 8:08 AM, Daniel Kaschek wrote:> Dear all, > > I have a function "fn" with its own environment, i.e. > > env <- environment(fn) > > and env is not .GlobalEnv. And another function > > getValue <- function(x) environment(x)$mylist > > which returns the list object "mylist" which is in "env". If I want to > modify "mylist", I could write > > 'getValue<-' <- function(x, value) { environment(x)$mylist <- value} > > which gives not the desired result, e.g. > > getValue(fn)[[1]] <- 3 > > will set the first list entry of "mylist" to 3. But then "fn" will also > be 3! > > environment(fn)$mylist[[1]] <- 3 > > does set the list entry correctly while keeping "fn". What's the > difference?An assignment function should return the modified value. So it looks like you would need 'getValue<-' <- function(x, value) { environment(x)$mylist <- value x } but in fact, this doesn't work: getValue(fn)[[1]] <- 3 Error in getValue(fn)[[1]] <- 3 : could not find function "getValue" I suspect this is a parser problem. However, it does suggest a simple workaround for you: Just define getValue <- function(x) environment(x)$mylist and then your expression works as expected. Duncan Murdoch
> On 07 Feb 2016, at 14:46 , Duncan Murdoch <murdoch.duncan at gmail.com> wrot8[e: >[snippage]> > but in fact, this doesn't work: > > getValue(fn)[[1]] <- 3 > Error in getValue(fn)[[1]] <- 3 : could not find function "getValue" > > I suspect this is a parser problem.Umm, no... The canonical semantics are that foo(x)[[....]] <- bar is internally converted to *tmp* <- foo(x) *tmp*[[....]] <- bar x <- `foo<-`(x, *tmp*) so both foo() and `foo<-` are required. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
On So, Feb 7, 2016 at 2:46 , Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> An assignment function should return the modified value. So it looks > like you would need > > > 'getValue<-' <- function(x, value) { > environment(x)$mylist <- value > x > }Thanks. I have not thought of a return value. That makes the difference.> > but in fact, this doesn't work: > > getValue(fn)[[1]] <- 3 > Error in getValue(fn)[[1]] <- 3 : could not find function "getValue"I have a "getValue" function anyway. It was even the other way round. I wanted to assign values without a function "getValue<-" and was rebuked by R that this function was missing. Thanks to both of you, Peter and Duncan. Best regards, Daniel